当前位置: 首页 > news >正文

优秀个人网站app软件开发培训班

优秀个人网站,app软件开发培训班,太原注册公司网站,为什么要建设企业的微网站这是悦乐书的第299次更新#xff0c;第318篇原创01 看题和准备今天介绍的是LeetCode算法题中Easy级别的第167题(顺位题号是706)。在不使用任何内置哈希表库的情况下设计HashMap。具体而言#xff0c;你的设计应包括以下功能#xff1a;put(key#xff0c;value)#xff1a…这是悦乐书的第299次更新第318篇原创01 看题和准备今天介绍的是LeetCode算法题中Easy级别的第167题(顺位题号是706)。在不使用任何内置哈希表库的情况下设计HashMap。具体而言你的设计应包括以下功能put(keyvalue)将一个(keyvalue)对插入HashMap。如果该值已存在于HashMap中请更新该值。get(key)返回指定键映射到的值如果此映射不包含键的映射则返回-1。remove(key)如果此映射包含键的映射则删除值键的映射。例如MyHashMap hashMap new MyHashMap();hashMap.put(1,1);hashMap.put(2,2);hashMap.get(1); //返回1hashMap.get(3); //返回-1(未找到)hashMap.put(2,1); //更新现有值hashMap.get(2); //返回1hashMap.remove(2); //删除2的映射hashMap.get(2); //返回-1(未找到)注意所有key和value都将在[0,1000000]范围内。操作次数将在[1,10000]范围内。请不要使用内置的HashMap库。本次解题使用的开发工具是eclipsejdk使用的版本是1.8环境是win7 64位系统使用Java语言编写和测试。02 第一种解法为了实现键值对的效果内层使用了二维数组外层使用ArrayList。其中二维数组是一个一行两列的结构第一行第一列存储key的值第一行第二列存储value的值。另外我们还需要单独写一个contains方法来判断当前的key是否存在于HashMap中。put方法的时间复杂度为O(n)get方法的时间复杂度为O(n)remove的时间复杂度为O(n)contains方法的时间复杂度为O(n)。class MyHashMap {List list;/** Initialize your data structure here. */public MyHashMap() {list new ArrayList();}/** value will always be non-negative. */public void put(int key, int value) {if (contains(key)) {for (int[][] arr : list) {if (arr ! null arr[0][0] key) {arr[0][1] value;break;}}} else {list.add(new int[][]{{key, value}});}}public boolean contains(int key){for (int[][] arr : list) {if (arr ! null arr[0][0] key) {return true;}}return false;}/** Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key */public int get(int key) {for (int[][] arr : list) {if (arr ! null arr[0][0] key) {return arr[0][1];}}return -1;}/** Removes the mapping of the specified value key if this map contains a mapping for the key */public void remove(int key) {if (contains(key)) {for (int i0; iif (list.get(i)[0][0] key) {list.remove(i);break;}}}}}/*** Your MyHashMap object will be instantiated and called as such:* MyHashMap obj new MyHashMap();* obj.put(key,value);* int param_2 obj.get(key);* obj.remove(key);*/03 第二种解法我们也可以使用一个小track依旧使用数组但是变成了一维数组因为题目给定了key和value的范围所以数组的容量为其最大范围加1。因为最小值可以为0而数组默认值是0避免误判将数组的初始值全部赋值为-1。put方法的时间复杂度为O(1)get方法的时间复杂度为O(1)remove的时间复杂度为O(1)但是空间复杂度较高。class MyHashMap {int[] arr;/** Initialize your data structure here. */public MyHashMap() {arr new int[1000001];Arrays.fill(arr, -1);}/** value will always be non-negative. */public void put(int key, int value) {arr[key] value;}/** Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key */public int get(int key) {return arr[key];}/** Removes the mapping of the specified value key if this map contains a mapping for the key */public void remove(int key) {arr[key] -1;}}/*** Your MyHashMap object will be instantiated and called as such:* MyHashMap obj new MyHashMap();* obj.put(key,value);* int param_2 obj.get(key);* obj.remove(key);*/04 第三种解法class MyHashMap {/** Initialize your data structure here. */ListNode[] nodes;public MyHashMap() {nodes new ListNode[10000];}/** value will always be non-negative. */public void put(int key, int value) {int idx key%10000;if(nodes[idx]null){nodes[idx] new ListNode(-1,-1);nodes[idx].next new ListNode(key,value);}else{ListNode pre find(nodes[idx], key);if(pre.next null){pre.next new ListNode(key, value);}else{pre.next.value value;}}}/** Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key */public int get(int key) {int idx key%10000;if(nodes[idx] null) return -1;else{ListNode pre find(nodes[idx], key);if(pre.next null) return -1;else return pre.next.value;}}public ListNode find(ListNode root, int key){ListNode pre null;ListNode cur root;while(cur!null cur.key ! key){pre cur;cur cur.next;}return pre;}/** Removes the mapping of the specified value key if this map contains a mapping for the key */public void remove(int key) {int idx key%10000;if(nodes[idx] null) return;else{ListNode pre find(nodes[idx], key);if(pre.next null) return;else{pre.next pre.next.next;}}}class ListNode{int key;int value;ListNode next;ListNode(int key, int value){this.key key;this.value value;}}}/*** Your MyHashMap object will be instantiated and called as such:* MyHashMap obj new MyHashMap();* obj.put(key,value);* int param_2 obj.get(key);* obj.remove(key);*/05 小结算法专题目前已日更超过四个月算法题文章167篇公众号对话框回复【数据结构与算法】、【算法】、【数据结构】中的任一关键词获取系列文章合集。以上就是全部内容如果大家有什么好的解法思路、建议或者其他问题可以下方留言交流点赞、留言、转发就是对我最大的回报和支持
http://www.pierceye.com/news/408839/

相关文章:

  • 网站建设 提案 框架河南一般建一个网站需要多少钱
  • 福建省建设人才市场网站深圳营销型网站建设优化
  • 晋城购物网站开发设计宣传网站有哪些
  • 在哪人网站要以接it项目做企业为什么要分析环境
  • 达令的网站建设wordpress上传视频
  • 织梦免费网站模块下载地址南充楼盘网
  • 深圳极速网站建设服务器做网站 然后百度推广
  • 西充县住房和城乡建设局网站深圳建设局网站打不开
  • 深圳常平网站建设制作公司网站开发qq群
  • 校园网站建设的感受论文专业微信网站建设公司首选
  • 国外免费logo设计网站免费网课平台
  • 高端网站设计定制公司页面跳转自动更新
  • 项目建设资金来源网站网站开发技术可以做什么工作
  • 可做易企秀的网站网页建站网站
  • 南京网站建设价格大型网站开发协调
  • 园林景观设计公司点评的网站和论坛大型网站搜索怎么做的
  • 河南省建设教育培训中心网站广告机器设备的价格表
  • 郑州做网站哪家最好中国能源建设集团有限公司是什么级别
  • 品牌设计公司排行榜前十名seo外包服务公司
  • 潍坊网站建设 58wordpress 酒店预订
  • 个人网站主机选择电商公司官网
  • 名城苏州网站龙岗网站建设价位
  • 免费手机网站制作学做网站游戏教程
  • 什么企业做网站广州市公司网站建设
  • 无锡万度网站建设推广电影链接赚佣金
  • 电子商务网站建设与管理实训网页设计与网站建设基础心得体会
  • 托管的服务器如何做网站花店网站模板 html
  • 南宁保洁网站建设在线 代理 输入网址
  • 微站是什么意思快站app下载
  • 网站让女友做网站模特做网站好一点的软件