[LeetCode] 217번 - Contains Duplicate [Java][C++]
[LeetCode] 217번 - Contains Duplicate [Java][C++]
1. 아이디어
정수 배열 nums에 대해 겹치는 원소가 하나라도 있으면 true, 없으면 false를 반환하는 문제로 집합을 활용하면 간단하게 해결할 수 있다. nums의 모든 원소를 담은 집합의 크기가 nums의 크기와 같으면 겹치는 원소가 없는 것이고 작다면 겹치는 원소가 하나라도 존재하는 것이다.
2. 복잡도
| 시간복잡도 | 공간복잡도 |
|---|---|
| $O(N)$ | $O(N)$ |
3. 코드
풀이 [Java][C++]
1
2
3
4
5
6
7
8
9
10
11
12
import java.util.*;
class Solution {
public boolean containsDuplicate(int[] nums) {
Set<Integer> set = new HashSet<>();
for (int x : nums) {
set.add(x);
}
return set.size() != nums.length;
}
}
1
2
3
4
5
6
7
8
9
10
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
unordered_set<int> st(nums.begin(), nums.end());
return st.size() != nums.size();
}
};
This post is licensed under CC BY 4.0 by the author.