217. Contains Duplicate
2016-04-21 10:42
489 查看
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.
思路:将array中数放入set中,如果array的长度跟set一样,则没有重复的,否则就有重复的
代码如下(已通过leetcode)
public class Solution {
public boolean containsDuplicate(int[] nums) {
int lengthsize = nums.length;
if (lengthsize < 2)
return false;
Set<Integer> num = new HashSet<Integer>();
for (int i = 0; i < lengthsize; i++) {
num.add(nums[i]);
}
if (num.size() == lengthsize)
return false;
else
return true;
}
}
twice in the array, and it should return false if every element is distinct.
思路:将array中数放入set中,如果array的长度跟set一样,则没有重复的,否则就有重复的
代码如下(已通过leetcode)
public class Solution {
public boolean containsDuplicate(int[] nums) {
int lengthsize = nums.length;
if (lengthsize < 2)
return false;
Set<Integer> num = new HashSet<Integer>();
for (int i = 0; i < lengthsize; i++) {
num.add(nums[i]);
}
if (num.size() == lengthsize)
return false;
else
return true;
}
}
相关文章推荐
- pthread_cond_wait()函数说明
- Saiku+Kylin多维分析平台探索
- LeetCode 172. Factorial Trailing Zeroes
- Domain Model(领域模型)
- System and Device Programming------Main Memory
- AIDL
- 人工智能60年:中国人工智能方阵发出第一轮AI冲击波
- 对Run-Time Check Failure #3 - The variable 'a' is being used without being initialized.的处理以及理解
- Paint和Canvas
- poj 2709 Painter
- 严重: WSSERVLET11: failed to parse runtime descriptor: The serviceName cannot be retrieved from an int
- 使用AirDrop分享
- 在其他class或者view中获取MainActivity实例,以便调用其函数的方法:
- zoj-3923- In the Rain
- hdu1867 A + B for you again(kmp)
- 【卡特兰数+高精度】HDU1023Train Problem II
- POJ 3735 Training little cats(矩阵快速幂)
- POJ 3735 Training little cats(矩阵快速幂)
- LightOj 1076 - Get the Containers (折半枚举好题)
- Codeforces Beta Round #29 (Div. 2, Codeforces format) C. Mail Stamps 拓扑排序