Skip to content

Commit 5916ab4

Browse files
committed
chore: merge origin/main
2 parents fdd2a3c + 81f42e0 commit 5916ab4

File tree

178 files changed

+4615
-59
lines changed

Some content is hidden

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

178 files changed

+4615
-59
lines changed

โ€Žcontains-duplicate/DaleSeo.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use std::collections::HashSet;
2+
3+
// TC: O(n)
4+
// SC: O(n)
5+
impl Solution {
6+
pub fn contains_duplicate(nums: Vec<i32>) -> bool {
7+
let mut set = HashSet::new();
8+
!nums.into_iter().all(|num| set.insert(num))
9+
}
10+
}

โ€Žcontains-duplicate/Geegong.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import java.util.HashMap;
2+
import java.util.HashSet;
3+
import java.util.Map;
4+
5+
public class Geegong {
6+
7+
8+
/**
9+
* time complexity : O(n)
10+
* space complexity : o(n)
11+
* @param nums
12+
* @return
13+
*/
14+
public boolean containsDuplicate(int[] nums) {
15+
HashSet<Integer> uniques = new HashSet<>();
16+
17+
for (int num : nums) {
18+
if (uniques.contains(num)) {
19+
return true;
20+
}
21+
22+
uniques.add(num);
23+
}
24+
25+
return false;
26+
}
27+
28+
}
29+

โ€Žcontains-duplicate/Grit03.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {boolean}
4+
*/
5+
var containsDuplicate = function (nums) {
6+
const countMap = new Map();
7+
8+
for (let i = 0; i < nums.length; i++) {
9+
const key = nums[i];
10+
if (countMap.has(key)) {
11+
const value = countMap.get(key);
12+
if (value === 1) {
13+
return true;
14+
}
15+
countMap.set(key, value + 1);
16+
} else {
17+
countMap.set(key, 1);
18+
}
19+
}
20+
21+
return false;
22+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {boolean}
4+
*/
5+
var containsDuplicate = function (nums) {
6+
const map = new Map();
7+
8+
for (let num of nums) {
9+
map.set(num, (map.get(num) || 0) + 1);
10+
}
11+
12+
for (let [_, count] of map) {
13+
if (count > 1) return true;
14+
}
15+
16+
return false;
17+
};
18+
19+
// set ๋ฐฉ์‹์œผ๋กœ๋„ ๋Œ€์ฒด๊ฐ€๋Šฅ
20+
// var containsDuplicate = function(nums) {
21+
// return new Set(nums).size !== nums.length;
22+
// };

โ€Žcontains-duplicate/Lustellz.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function containsDuplicate(nums: number[]): boolean {
2+
let tmp: number[] = nums.toSorted()
3+
for(let i = 0 ; i < nums.length - 1 ; i++){
4+
if(tmp[i]===tmp[i+1]) return true
5+
}
6+
return false
7+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// TC : O(n) : it iterates for the length of nums
2+
// SC : O(n) : hashmap is created with the size of nums
3+
4+
func containsDuplicate(nums []int) bool {
5+
hashmap := make(map[int]bool)
6+
7+
for i:=0;i<len(nums);i++ {
8+
if hashmap[nums[i]] {
9+
return true
10+
}
11+
12+
hashmap[nums[i]] = true
13+
}
14+
15+
return false
16+
}
17+

โ€Žcontains-duplicate/Yg-cho.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {boolean}
4+
*/
5+
var containsDuplicate = function(nums) {
6+
return new Set(nums).size !== nums.length;
7+
};
8+
9+
//console.log(containsDuplicate([1, 2, 3, 1])); // true
10+
// console.log(containsDuplicate([1, 2, 3, 4])); // false
11+
// console.log(containsDuplicate([1, 1, 1, 3, 3, 4, 3, 2, 4, 2])); // true

โ€Žcontains-duplicate/bskkimm.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from collections import defaultdict
2+
class Solution(object):
3+
def containsDuplicate(self, nums):
4+
"""
5+
:type nums: List[int]
6+
:rtype: bool
7+
"""
8+
# [1,2,3,1]
9+
10+
# [1,1,1,3,3,4,3,2,4,2]
11+
12+
# add element to a dict
13+
# if a same value appears, then return True
14+
# if a for loop ends without disruption, return False
15+
16+
dict = defaultdict(bool)
17+
18+
for num in nums:
19+
if dict[num]:
20+
return True
21+
else:
22+
dict[num] = True
23+
24+
return False
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
contains-duplicate
3+
์š”๊ตฌ์‚ฌํ•ญ: ์ฃผ์–ด์ง„ ์ž…๋ ฅ์—์„œ ์ค‘๋ณต๋œ ์›์†Œ๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ์ฐธ/๊ฑฐ์ง“์œผ๋กœ ๋ฐ˜ํ™˜.
4+
์ ‘๊ทผ 1: ๊ฐ€์žฅ ๋‹จ์ˆœํ•œ ๋ฐฉ๋ฒ•์€ ๋‹ค๋ฅธ vector input_list๋ฅผ ๋‘๊ณ , ์ž…๋ ฅ ๋ฐ˜๋ณต๋งˆ๋‹ค ์ „์ฒด ์Šค์บ”ํ•ด์„œ ์ค‘๋ณต ์—ฌ๋ถ€๋ฅผ ์ฒดํฌํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.
5+
input_list์˜ ์Šค์บ” ๋น„์šฉ O(N)์„ ๋ชจ๋“  ์ž…๋ ฅ์— ๋Œ€ํ•ด ๋ฐ˜๋ณตํ•˜๋ฏ€๋กœ ์ด ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(N^2)์ž…๋‹ˆ๋‹ค.
6+
๊ณต๊ฐ„ ๋ณต์žก๋„๋Š” ์ž…๋ ฅ๊ณผ ๊ฐ™์œผ๋ฏ€๋กœ O(N)์ž…๋‹ˆ๋‹ค.
7+
์ ‘๊ทผ 2: ์›์†Œ์˜ ์กด์žฌ ์—ฌ๋ถ€๋ฅผ ๋” ๋น ๋ฅด๊ฒŒ ์ฒดํฌํ•˜๋Š” ๋ฐฉ๋ฒ•์€ set์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.
8+
set์€ ์ค‘๋ณต ์›์†Œ๋ฅผ ๊ฒ€์ถœํ•˜๋Š”๋ฐ ํšจ๊ณผ์ ์ธ ์ž๋ฃŒ๊ตฌ์กฐ๋กœ, ๋‚ด๋ถ€ ๊ตฌํ˜„์— ๋”ฐ๋ผ ๋น„์šฉ๊ณผ ํŠน์„ฑ์ด ๋‹ค๋ฆ…๋‹ˆ๋‹ค.
9+
- C++์—์„œ ordered_{set, map}์€ ์ •๋ ฌ์„ ์œ ์ง€ํ•˜๋Š” search tree๋ฅผ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ ์ฟผ๋ฆฌ์™€ ์‚ฝ์ž…, ์‚ญ์ œ ๋น„์šฉ์ด ํ‰๊ท  O(log(N))์ž…๋‹ˆ๋‹ค.
10+
search tree์˜ ๊ตฌํ˜„์— ๋”ฐ๋ผ ์ตœ์•…์˜ ๊ฒฝ์šฐ ์ฟผ๋ฆฌ ๋น„์šฉ์ด O(N)๊นŒ์ง€ ์ฆ๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
11+
- C++์—์„œ unordered_{set, map}์€ ํ•ด์‹œ ๋ฐ ๋ฒ„ํ‚ท ๊ตฌ์กฐ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ, ํ‰๊ท ์ ์ธ ์ฟผ๋ฆฌ, ์‚ฝ์ž…, ์‚ญ์ œ ๋น„์šฉ์€ O(1)์ž…๋‹ˆ๋‹ค.
12+
์šฐ๋ฆฌ์˜ ์š”๊ตฌ์‚ฌํ•ญ์€ ์ž…๋ ฅ์— ๋Œ€ํ•œ ๋ณ„๋„์˜ ์ •๋ณด๋ฅผ ์ €์žฅํ•  ํ•„์š”๊ฐ€ ์—†์œผ๋ฏ€๋กœ unordered_set์œผ๋กœ ์ค‘๋ณต ๊ฒ€์‚ฌ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ์ด ๋น„์šฉ ํšจ์œจ์ ์ž…๋‹ˆ๋‹ค.
13+
์ตœ์•…์˜ ๊ฒฝ์šฐ ๋ชจ๋“  ์ž…๋ ฅ์„ ํ™•์ธํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. N๊ฐœ์˜ ์ž…๋ ฅ์— ๋Œ€ํ•ด ๊ฐ๊ฐ ์ฟผ๋ฆฌ์™€ ์ž…๋ ฅ์„ ์ˆ˜ํ–‰ํ•˜๋ฏ€๋กœ, ์ด ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(N), ๊ณต๊ฐ„ ๋ณต์žก๋„๋„ ์ž…๋ ฅ๋งŒํผ์ธ O(N)์ž…๋‹ˆ๋‹ค.
14+
*/
15+
16+
#include <unordered_set>
17+
#include <vector>
18+
19+
class Solution {
20+
public:
21+
bool containsDuplicate(std::vector<int>& nums) {
22+
std::unordered_set<int> s;
23+
24+
for (auto it = nums.begin(); it != nums.end(); it++) {
25+
int x = *it;
26+
if (s.find(x) != s.end())
27+
return true;
28+
else
29+
s.insert(x);
30+
}
31+
return false;
32+
}
33+
};

โ€Žcontains-duplicate/chordpli.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from typing import List
2+
3+
class Solution:
4+
def containsDuplicate(self, nums: List[int]) -> bool:
5+
dic = {}
6+
7+
for num in nums:
8+
if dic.get(num):
9+
return True
10+
dic[num] = 1
11+
12+
return False

0 commit comments

Comments
ย (0)