Skip to content

Commit a08f09b

Browse files
authored
Merge branch 'DaleStudy:main' into main
2 parents 16abf6e + 64df86c commit a08f09b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

98 files changed

+2782
-207
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Approach 3: HashSet (Using TypeScript Set)
2+
// โณ Time Complexity: O(n)
3+
// ๐Ÿ’พ Space Complexity: O(n)
4+
5+
function containsDuplicate(nums: number[]): boolean {
6+
7+
const seen = new Set<number>();
8+
9+
for (const num of nums) {
10+
if (seen.has(num)) {
11+
return true;
12+
}
13+
seen.add(num);
14+
}
15+
16+
return false;
17+
18+
};
19+
20+
21+
// Approach 2: Sorting + Scan
22+
// โณ Time Complexity: O(n * log(n)) โŒ (Faster than O(n^2), but still not optimal)
23+
// ๐Ÿ’พ Space Complexity: O(1)
24+
25+
// function containsDuplicate(nums: number[]): boolean {
26+
27+
// nums.sort();
28+
29+
// for (let i = 0; i < nums.length - 1; i++) {
30+
// if (nums[i] === nums[i + 1]) {
31+
// return true;
32+
// }
33+
// }
34+
35+
// return false;
36+
37+
// };
38+
39+
40+
// Approach 1: Brute Force (O(n^2))
41+
// ๐Ÿšจโณ TLE (Time Limit Exceeded)!
42+
// โณ Time Complexity: O(n^2) โŒ (Inefficient for large inputs)
43+
// ๐Ÿ’พ Space Complexity: O(1) โœ… (Great, no extra memory used!)
44+
45+
// function containsDuplicate(nums: number[]): boolean {
46+
47+
// for (let i = 0; i < nums.length; i++) {
48+
// for (let j = i + 1; j < nums.length; j++) {
49+
// if (nums[i] === nums[j]) {
50+
// return true;
51+
// }
52+
// }
53+
// }
54+
55+
// return false;
56+
57+
// };
58+
59+
60+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# ์ค‘๋ณต์ œ๊ฑฐ ํ›„ ๊ธธ์ด ํ™•์ธ ๋ฌธ์ œ
2+
# ์‹œ๊ฐ„๋ณต์žก๋„ ๋ฐ ๊ณต๊ฐ„๋ณต์žก๋„ O(n)
3+
class Solution:
4+
def containsDuplicate(self, nums: List[int]) -> bool:
5+
return len(nums) != len(set(nums))
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {boolean}
4+
*/
5+
var containsDuplicate = function (nums) {
6+
let count = [nums[0]];
7+
for (let i = 1; i < nums.length; i++) {
8+
if (count.includes(nums[i])) {
9+
return true;
10+
} else {
11+
count.push(nums[i]);
12+
}
13+
}
14+
return false;
15+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// nums์— ์ค‘๋ณต์ด ์žˆ๋Š” ์ง€ ํ™•์ธํ•˜๋Š” ๊ฒƒ
2+
// array์จ์„œ ์‹œ๊ฐ„ ํ†ต๊ณผ ๋ชปํ–ˆ๋‹ค๊ฐ€ Set ๊ฐ์ฒด๋กœ ๋ณ€๊ฒฝํ•ด์„œ ํ†ต๊ณผ
3+
function containsDuplicate(nums: number[]): boolean {
4+
const numSet = new Set();
5+
6+
for(let num of nums) {
7+
if(numSet.has(num)) {
8+
return true;
9+
} else {
10+
numSet.add(num);
11+
}
12+
}
13+
14+
return false;
15+
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.
2+
/**
3+
* @param {number[]} nums
4+
* @return {boolean}
5+
*/
6+
var containsDuplicate = function (nums) {
7+
const uniqueSet = new Set(nums);
8+
return uniqueSet.size !== nums.length;
9+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// https://leetcode.com/problems/contains-duplicate/description/
2+
3+
// TC: O(n)
4+
// SC: O(n)
5+
6+
function containsDuplicate(nums: number[]): boolean {
7+
const set = new Set(nums);
8+
return set.size !== nums.length;
9+
}
10+
11+
function containsDuplicate(nums: number[]): boolean {
12+
const set = new Set();
13+
14+
for (const num of nums) {
15+
if (set.has(num)) {
16+
return true;
17+
}
18+
set.add(num);
19+
}
20+
21+
return false;
22+
}
23+
24+
console.log(containsDuplicate([1, 1, 1, 3, 3, 4, 3, 2, 4, 2]));
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <unordered_set>
4+
5+
using namespace std;
6+
7+
/*
8+
TC: O(n)
9+
๋ฒกํ„ฐ nums์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ ํ•ด์‹œ ํ…Œ์ด๋ธ”์— ์‚ฝ์ž…ํ•˜๋Š” ๊ณผ์ •์—์„œ O(n)๋งŒํผ ์†Œ์š”๋จ
10+
SC: O(n)
11+
12+
ํ’€์ด ๋ฐฉ๋ฒ• : nums์˜ ์ค‘๋ณต์š”์†Œ๋ฅผ ๊ฑฐ๋ฅธ uset์„ ๋งŒ๋“ค๊ณ ,
13+
nums์™€ uset์˜ ํฌ๊ธฐ๊ฐ€ ์„œ๋กœ ๋‹ค๋ฅด๋‹ค๋ฉด nums์— ์ค‘๋ณต ์š”์†Œ๊ฐ€ ํ•˜๋‚˜ ์ด์ƒ ์กด์žฌํ•œ๋‹ค.
14+
*/
15+
16+
class Solution {
17+
public:
18+
bool containsDuplicate(vector<int>& nums) {
19+
unordered_set<int> uset(nums.begin(), nums.end());
20+
return (nums.size() != uset.size());
21+
}
22+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
/**
3+
* Determines if the array contains any duplicate values.
4+
* Uses a Set to track seen numbers for O(n) time complexity.
5+
*
6+
* @param nums - An array of integers.
7+
* @returns `true` if there are duplicates, `false` otherwise.
8+
*
9+
* Time Complexity: O(n)
10+
* Space Complexity: O(n)
11+
*/
12+
function containsDuplicate(nums: number[]): boolean {
13+
let numSet = new Set(nums);
14+
return numSet.size != nums.length;
15+
};
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {boolean}
4+
*/
5+
6+
/**
7+
* ๋ฌธ์ œ์„ค๋ช…: 2๊ฐœ ์ด์ƒ ๋ฐ˜๋ณต๋˜๋Š” ๊ฐ’์ด ์žˆ์œผ๋ฉด true, ๋ชจ๋‘ ๋ฐ˜๋ณต๋˜์ง€ ์•Š์œผ๋ฉด false.
8+
9+
์ œํ•œ์‚ฌํ•ญ
10+
1 <= nums.length <= 10^5
11+
-109 <= nums[i] <= 109
12+
*/
13+
14+
var containsDuplicate = function (nums) {
15+
const numberSet = new Set();
16+
//์‹œ๊ฐ„ ๋ณต์žก๋„ O(n)
17+
for (let i of nums) {
18+
if (!numberSet.has(i)) {
19+
//๊ณต๊ฐ„๋ณต์žก๋„ O(n)
20+
numberSet.add(i);
21+
} else {
22+
return true;
23+
}
24+
}
25+
return false;
26+
};
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
3+
# cpp stl
4+
```cpp
5+
class Solution {
6+
public:
7+
bool containsDuplicate(vector<int>& nums) {
8+
set<int> numSet(nums.begin(), nums.end());
9+
return numSet.size() != nums.size();
10+
}
11+
};
12+
```
13+
14+
- set์œผ๋กœ ๋‹จ์ˆœ๋น„๊ต. ํŽธ๋ฆฌํ•˜๋‚˜, ์ •๋ ฌ์— ๋น„ํ•ด ์‹œ๊ฐ„์ด ์˜ค๋ž˜ ๊ฑธ๋ฆผ.
15+
- unordered_set์„ ์“ฐ๋ฉด set ๋ฐฉ์‹์—์„œ ์กฐ๊ธˆ ๋” ๋น ๋ฅผ ์ˆ˜๋Š” ์žˆ์Œ
16+
17+
```cpp
18+
class Solution {
19+
public:
20+
bool containsDuplicate(vector<int>& nums) {
21+
sort(nums.begin(), nums.end());
22+
for(int i=0;i<nums.size()-1;i++){
23+
if(nums[i]==nums[i+1]){
24+
return true;
25+
}
26+
}
27+
return false;
28+
}
29+
};
30+
```
31+
32+
- ๋‘˜ ๋‹ค O(n logn)์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„์ด๋‚˜, ์ž๋ฃŒ ํŠน์„ฑ ์ƒ ์ •๋ ฌ์ด ๋” ๋น ๋ฆ„
33+
34+
35+
36+
37+

0 commit comments

Comments
ย (0)