[leedcode 217] Contains Duplicate
2015-08-06 22:12
435 查看
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.
public class Solution { public boolean containsDuplicate(int[] nums) { //用一个set保存数组中的值,如果发现当前值已经在set中存在,则返回true。注意不存在时需要add到set Set<Integer> set=new HashSet<Integer>(); for(int i=0;i<nums.length;i++){ if(!set.isEmpty()&&set.contains(nums[i])){ return true; } set.add(nums[i]); } return false; } }
相关文章推荐
- 2015 Multi-University Training Contest 6 -- 1001 Average
- Container With Most Water
- Container With Most Water
- 2015 HUAS Summer Trainning #4~D
- Doing Homework again SDUT 2076
- 2015 Multi-University Training Contest 2
- DELL R710 服务器更换硬盘后,重建RAID1 重装操作系统 无法启动
- POJ 1804 Brainman
- HDOJ1789Doing Homework again(贪心)
- 2015 Multi-University Training Contest 6 hdu 5360 Hiking
- HDU-- 2015 Multi-University Training Contest 6 Cake
- hdu 5360 Hiking(2015 Multi-University Training Contest 6)
- hdu 4750——Count The Pairs
- HDOJ 题目4251The Famous ICPC Team Again(划分树)
- hdu 5353 Average(2015 Multi-University Training Contest 6)
- HDU 5360 Hiking(优先队列)2015 Multi-University Training Contest 6
- POJ 3414 Paid Roads(状态压缩最短路)
- chain
- Codeforces Gym 100342C Problem C. Painting Cottages 暴力
- Codeforces A. Lineland Mail