博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
leetcode总结
阅读量:4594 次
发布时间:2019-06-09

本文共 1272 字,大约阅读时间需要 4 分钟。

1hash table部分

HashSet内部维护一个HashMap对象。分析HashMap源码:

                                                           

                                     final Entry<K,V> getEntry(Object key) {

                                                    if (size == 0) {
                                                             return null;
                                                    }

                                                    int hash = (key == null) ? 0 : hash(key);

                                                   for (Entry<K,V> e = table[indexFor(hash, table.length)];
                                                   e != null;
                                                   e = e.next) {
                                                       Object k;
                                                    if (e.hash == hash &&
                                                                  ((k = e.key) == key || (key != null && key.equals(k))))
                                                       return e;
                                                   }
                                                  return null;
                                     }

HashMap在查找一个key的对象内容时,通过将Key哈希,,然后比较列表中对象元素的hash值与key的hash是否相等,以及列表中的对像与key是否相等或者equals。

HashMap是线程不安全的,并且当Hash冲突时采用的方法是chaining(链表的方式解决)。

应用HashTable例子:

A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.

Return a deep copy of the list.

                                           

                        public RandomListNode copyRandomList(RandomListNode head) {

                                                  if(head==null)return null;
                                                 RandomListNode oldHead=head;
                                                 HashMap<RandomListNode,RandomListNode> map=new HashMap<>();
                                                 RandomListNode node=new RandomListNode(head.label);
                                                 RandomListNode res=node;
                                                 map.put(head,node);
                                                 while(head.next!=null)
                                                 {
                                                      node.next=new RandomListNode(head.next.label);
                                                      map.put(head.next,node.next);
                                                      head=head.next;
                                                      node=node.next;
                                                 }
                                                 head=oldHead;
                                                 node=res;
                                                 while(head!=null)
                                                {
                                                 node.random=map.get(head.random);
                                                 head=head.next;
                                                 node=node.next;
                                                }
                                               return res;
                               }

 

                                            

转载于:https://www.cnblogs.com/xiaomacgrady/p/5142623.html

你可能感兴趣的文章
EFcore笔记之创建模型
查看>>
[Android][Android Studio] Gradle项目中加入JNI生成文件(.so文件)
查看>>
JMeter基础知识
查看>>
组合数据类型练习,英文词频统计实例上
查看>>
python入门知识
查看>>
为什么我在博客园开始写博客
查看>>
ES6数组的扩展
查看>>
xshell不能输入中文,显示为??
查看>>
[NGUI]NGUI图集Atlas制作
查看>>
vue的坑
查看>>
【原创】大数据基础之Airflow(2)生产环境部署airflow研究
查看>>
传说中的滑雪,巨丑勿拍(poj1088/tyvj1004)
查看>>
webpack——图片的路径与打包
查看>>
.net4.0注册到IIS ,重新注册IIS ,iis注册
查看>>
winform中devexpress bindcommand无效的解决方法
查看>>
SVN使用教程总结
查看>>
神经网络与机器学习 第一讲(1)——为什么需要机器学习
查看>>
算法入门
查看>>
SQL Server 2012 中 SSAS 多维数据浏览器已经废除
查看>>
codeforces 251 div2 C. Devu and Partitioning of the Array 模拟
查看>>