Java HashSet和ArrayList的查找Contains()时间复杂度 |
您所在的位置:网站首页 › hashmap查找的时间复杂度 › Java HashSet和ArrayList的查找Contains()时间复杂度 |
今天在刷leetCode时,碰到了一个题是这样的。 给定一个整数数组,判断是否存在重复元素。 如果任何值在数组中出现至少两次,函数返回 true。如果数组中每个元素都不相同,则返回 false。 看到这个题的第一时间,就想到了利用集合ArrayList来存储,并且判断。 代码: class Solution { public boolean containsDuplicate(int[] nums) { List list=new ArrayList(); for (int i=0;i 0 && (first = tab[(n - 1) & hash]) != null) { //直接通过hash确定元素位置,不用从头遍历 if (first.hash == hash && // always check first node ((k = first.key) == key || (key != null && key.equals(k)))) return first; if ((e = first.next) != null) { if (first instanceof TreeNode) return ((TreeNode)first).getTreeNode(hash, key); do { if (e.hash == hash && ((k = e.key) == key || (key != null && key.equals(k))))//部分情况下可能会继续遍历链表定位 return e; } while ((e = e.next) != null); } } return null; }
|
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |