0217-存在重复元素

Raphael Liu Lv10

给你一个整数数组 nums 。如果任一值在数组中出现 至少两次 ,返回 true ;如果数组中每个元素互不相同,返回 false

示例 1:

**输入:** nums = [1,2,3,1]
**输出:** true

示例 2:

**输入:** nums = [1,2,3,4]
**输出:** false

示例 3:

**输入:** nums = [1,1,1,3,3,4,3,2,4,2]
**输出:** true

提示:

  • 1 <= nums.length <= 105
  • -109 <= nums[i] <= 109

方法一:排序

在对数字从小到大排序之后,数组的重复元素一定出现在相邻位置中。因此,我们可以扫描已排序的数组,每次判断相邻的两个元素是否相等,如果相等则说明存在重复的元素。

[sol1-C++]
1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
sort(nums.begin(), nums.end());
int n = nums.size();
for (int i = 0; i < n - 1; i++) {
if (nums[i] == nums[i + 1]) {
return true;
}
}
return false;
}
};
[sol1-Java]
1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public boolean containsDuplicate(int[] nums) {
Arrays.sort(nums);
int n = nums.length;
for (int i = 0; i < n - 1; i++) {
if (nums[i] == nums[i + 1]) {
return true;
}
}
return false;
}
}
[sol1-Golang]
1
2
3
4
5
6
7
8
9
func containsDuplicate(nums []int) bool {
sort.Ints(nums)
for i := 1; i < len(nums); i++ {
if nums[i] == nums[i-1] {
return true
}
}
return false
}
[sol1-C]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int cmp(const void* _a, const void* _b) {
int a = *(int*)_a, b = *(int*)_b;
return a - b;
}

bool containsDuplicate(int* nums, int numsSize) {
qsort(nums, numsSize, sizeof(int), cmp);
for (int i = 0; i < numsSize - 1; i++) {
if (nums[i] == nums[i + 1]) {
return true;
}
}
return false;
}
[sol1-JavaScript]
1
2
3
4
5
6
7
8
9
10
var containsDuplicate = function(nums) {
nums.sort((a, b) => a - b);
const n = nums.length;
for (let i = 0; i < n - 1; i++) {
if (nums[i] === nums[i + 1]) {
return true;
}
}
return false;
};

复杂度分析

  • 时间复杂度:$O(N\log N)$,其中 $N$ 为数组的长度。需要对数组进行排序。

  • 空间复杂度:$O(\log N)$,其中 $N$ 为数组的长度。注意我们在这里应当考虑递归调用栈的深度。

方法二:哈希表

对于数组中每个元素,我们将它插入到哈希表中。如果插入一个元素时发现该元素已经存在于哈希表中,则说明存在重复的元素。

[sol2-C++]
1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
unordered_set<int> s;
for (int x: nums) {
if (s.find(x) != s.end()) {
return true;
}
s.insert(x);
}
return false;
}
};
[sol2-Java]
1
2
3
4
5
6
7
8
9
10
11
class Solution {
public boolean containsDuplicate(int[] nums) {
Set<Integer> set = new HashSet<Integer>();
for (int x : nums) {
if (!set.add(x)) {
return true;
}
}
return false;
}
}
[sol2-Golang]
1
2
3
4
5
6
7
8
9
10
func containsDuplicate(nums []int) bool {
set := map[int]struct{}{}
for _, v := range nums {
if _, has := set[v]; has {
return true
}
set[v] = struct{}{}
}
return false
}
[sol2-C]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
struct hashTable {
int key;
UT_hash_handle hh;
};

bool containsDuplicate(int* nums, int numsSize) {
struct hashTable* set = NULL;
for (int i = 0; i < numsSize; i++) {
struct hashTable* tmp;
HASH_FIND_INT(set, nums + i, tmp);
if (tmp == NULL) {
tmp = malloc(sizeof(struct hashTable));
tmp->key = nums[i];
HASH_ADD_INT(set, key, tmp);
} else {
return true;
}
}
return false;
}
[sol2-JavaScript]
1
2
3
4
5
6
7
8
9
10
var containsDuplicate = function(nums) {
const set = new Set();
for (const x of nums) {
if (set.has(x)) {
return true;
}
set.add(x);
}
return false;
};

复杂度分析

  • 时间复杂度:$O(N)$,其中 $N$ 为数组的长度。

  • 空间复杂度:$O(N)$,其中 $N$ 为数组的长度。

 Comments
On this page
0217-存在重复元素