leetcode 217:Contains Duplicate
2015-07-05 12:13
330 查看
题目:
Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.
分析:
给定一个数组,判断数组中是否有重复元素。用hashset实现,时间复杂度为O(n).
代码:
Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.
分析:
给定一个数组,判断数组中是否有重复元素。用hashset实现,时间复杂度为O(n).
代码:
public class Solution { public boolean containsDuplicate(int[] nums) { if(nums==null || nums.length<2) return false; Set<Integer> set=new HashSet<Integer>(); for(int i=0; i<nums.length; i++) { if(set.contains(nums[i])){ return true; }else{ set.add(nums[i]); } } return false; } }
相关文章推荐
- TIME_WAIT状态
- 【原创】CancellableWait
- leetcode 219:Contains Duplicate II
- 微软小冰发威,招摇人工智能
- select document library from certain list
- unity, trail renderer gone black on iOS
- rails 中使用association 生成select 下拉框的一些小技巧
- 图灵 数理逻辑 人工智能 图灵机与计算问题 论文
- LeetCode Contains Duplicate (判断重复元素)
- Leetcode#11 Container With Most Water
- 【Leetcode】Factorial Trailing Zeroes
- 人工智能的篱笆
- 单文档的BCG程序,为什么在CMainFrame::OnCreate最后设置工具条的标题,为什么无效?
- 在Maven的Web项目中用Main函数测试Hibernate
- failed to sync branch You might need to open a shell and debug the state of this repo.
- 172_Factorial Trailing Zeroes
- Filter及FilterChain的使用详解
- MongoDB的学习--explain()和hint()
- Leetcode:Container With Most Water
- errorC2440:CMainFrame 无法从 NMTOOLBARA 转换为 NMHDR