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.
}
1
2
3
4
5
public class Solution {
public boolean containsDuplicate(int[] nums) {
<span class="o">}</span>
1
排序,然后遍历查找。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
public boolean containsDuplicate(int[] nums) {
if(nums == null || nums.length <= 1) return false;
heapSort(nums);
for(int i = 0; i < nums.length-1;i++){
if(nums[i] == nums[i+1]) return true;
}
return false;
}
private void heapSort(int[] nums){
int n = nums.length - 1;
for(int i=n/2; i>=0;i--){
sink(nums,i,n);
}
while(n > 0){
swap(nums,0,n--);
sink(nums,0,n);
}
}
private void sink(int[] nums,int i,int n){
int tmp;
while((tmp = 2 * i + 1) <= n){
int j = tmp;
if(j < n && less(nums,j,j+1)) j++;
if(!less(nums,i,j)) break;
swap(nums,i,j);
i = j;
}
}
private boolean less(int[] a, int i, int j){
return a[i] < a[j];
}
<span class="kd">private</span> <span class="kt">void</span> <span class="nf">swap</span><span class="o">(</span><span class="kt">int</span><span class="o">[]</span> <span class="n">nums</span><span class="o">,</span><span class="kt">int</span> <span class="n">i</span><span class="o">,</span> <span class="kt">int</span> <span class="n">j</span><span class="o">){</span>
<span class="n">nums</span><span class="o">[</span><span class="n">i</span><span class="o">]</span> <span class="o">^=</span> <span class="n">nums</span><span class="o">[</span><span class="n">j</span><span class="o">];</span>
<span class="n">nums</span><span class="o">[</span><span class="n">j</span><span class="o">]</span> <span class="o">^=</span> <span class="n">nums</span><span class="o">[</span><span class="n">i</span><span class="o">];</span>
<span class="n">nums</span><span class="o">[</span><span class="n">i</span><span class="o">]</span> <span class="o">^=</span> <span class="n">nums</span><span class="o">[</span><span class="n">j</span><span class="o">];</span>
<span class="o">}</span><span class="w">
2 most votes
以下的代码都用到了java的本身的语言特性。set集中不存在重复元素。
- by riskycheng
1
2
3
4
5
6
7
public boolean containsDuplicate(int[] nums) {
Set<Integer> set = new HashSet<Integer>();
for(int i : nums)
if(!set.add(i))// if there is same
return true;
return false;
}
- by jmnarloch
}
1
2
3
4
5
6
7
8
9
10
11
public boolean containsDuplicate(int[] nums) {
<span class="kd">final</span> <span class="n">Set</span><span class="o"><</span><span class="n">Integer</span><span class="o">></span> <span class="n">distinct</span> <span class="o">=</span> <span class="k">new</span> <span class="n">HashSet</span><span class="o"><</span><span class="n">Integer</span><span class="o">>();</span>
<span class="k">for</span><span class="o">(</span><span class="kt">int</span> <span class="n">num</span> <span class="o">:</span> <span class="n">nums</span><span class="o">)</span> <span class="o">{</span>
<span class="k">if</span><span class="o">(</span><span class="n">distinct</span><span class="o">.</span><span class="na">contains</span><span class="o">(</span><span class="n">num</span><span class="o">))</span> <span class="o">{</span>
<span class="k">return</span> <span class="kc">true</span><span class="o">;</span>
<span class="o">}</span>
<span class="n">distinct</span><span class="o">.</span><span class="na">add</span><span class="o">(</span><span class="n">num</span><span class="o">);</span>
<span class="o">}</span>
<span class="k">return</span> <span class="kc">false</span><span class="o">;</span>