Skip to content

Commit ce295ce

Browse files
committed
Solve contains-duplicate
1 parent 0e5ffe5 commit ce295ce

1 file changed

Lines changed: 145 additions & 0 deletions

File tree

contains-duplicate/OstenHun.cpp

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/*
2+
Problem:
3+
Given an integer array nums,
4+
return true if any value appears at least twice in the array,
5+
and return false if every element is distinct.
6+
7+
Input: nums = [1,2,3,1]
8+
Output: true
9+
10+
Constraints:
11+
1 <= nums.length <= 10^5
12+
-10^9 <= nums[i] <= 10^9
13+
*/
14+
15+
// 처음 생각한 코드
16+
/*
17+
#include <iostream>
18+
#include <vector>
19+
using namespace std;
20+
21+
class Solution {
22+
public:
23+
bool containsDuplicate(vector<int>& nums) {
24+
for (int i = 0; i < nums.size(); i++) {
25+
for (int j = i+1; j < nums.size(); j++) {
26+
if (nums[i]==nums[j]) {
27+
return true;
28+
}
29+
}
30+
}
31+
32+
return false;
33+
}
34+
};
35+
36+
int main() {
37+
vector<int> arr;
38+
int sz;
39+
cin >> sz;
40+
for (int i = 0; i < sz; i++) {
41+
int nm;
42+
cin >> nm;
43+
arr.push_back(nm);
44+
}
45+
46+
47+
Solution s;\
48+
if (s.containsDuplicate(arr))
49+
cout << "true";
50+
else
51+
cout << "false";
52+
53+
return 0;
54+
}
55+
*/
56+
57+
// O(n^2) 시간 복잡도를 개선해보자
58+
/*
59+
set 을 이용해서 중복이 있다면 배열의 길이가 줄어들테니
60+
이를 통해 중복을 판단할 수 있다고 생각했다.
61+
*/
62+
/*
63+
#include <iostream>
64+
#include <vector>
65+
#include <set>
66+
#include <algorithm>
67+
using namespace std;
68+
69+
class Solution {
70+
public:
71+
bool containsDuplicate(vector<int>& nums) {
72+
// set과 유사하게 만들기 위해서 사용
73+
sort(nums.begin(), nums.end());
74+
for (size_t i = 1; i < nums.size(); i++) {
75+
if (nums[i] == nums[i-1]) return true;
76+
}
77+
return false;
78+
}
79+
};
80+
81+
82+
int main() {
83+
size_t sz;
84+
cin >> sz;
85+
86+
vector<int> arr;
87+
for (int i = 0; i < sz; i++) {
88+
int num;
89+
cin >> num;
90+
arr.push_back(num);
91+
}
92+
93+
Solution sol;
94+
if (sol.containsDuplicate(arr)) {
95+
cout << "true";
96+
} else {
97+
cout << "false";
98+
}
99+
}
100+
*/
101+
102+
// 이보다 더 좋은 방법이 있다고 gpt의 리뷰가 있었다.
103+
/*
104+
unordered_set<int> 를 이용한다.
105+
해시 테이블 자료구조를 이용하여 구현 되어 있는 것.
106+
정렬은 못하지만 탐색하는데에 O(1) 이라는 장점이 있어 이를 활용한다.
107+
-> 빠른 탐색 굿
108+
*/
109+
110+
111+
#include <iostream>
112+
#include <unordered_set>
113+
#include <vector>
114+
using namespace std;
115+
116+
class Solution {
117+
public:
118+
bool containsDuplicate(vector<int>& nums) {
119+
unordered_set<int> copy_set;
120+
for (int x : nums) {
121+
if (copy_set.count(x)) return true;
122+
copy_set.insert(x);
123+
}
124+
return false;
125+
}
126+
};
127+
128+
int main() {
129+
int n;
130+
cin >> n;
131+
132+
vector<int> nums;
133+
nums.reserve(n);
134+
135+
for (int i = 0; i < n; i++) {
136+
int x;
137+
cin >> x;
138+
nums.push_back(x);
139+
}
140+
141+
Solution sol;
142+
cout << (sol.containsDuplicate(nums) ? "true" : "false");
143+
144+
return 0;
145+
}

0 commit comments

Comments
 (0)