Skip to content

Commit eda2f64

Browse files
authored
Merge branch 'DaleStudy:main' into main
2 parents 8ea0678 + 4c1dd3e commit eda2f64

Some content is hidden

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

70 files changed

+2325
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
3+
'''
4+
ํ’€์ด:
5+
์ค‘๋ณต๋œ ์š”์†Œ๊ฐ€ ์žˆ๋Š”์ง€ ์ฐพ๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.
6+
7+
hash set ์œผ๋กœ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•˜๊ณ 
8+
๊ธฐ์กด nums ์˜ ๊ธธ์ด์™€ ์ค‘๋ณต ์ œ๊ฑฐ๋œ nums_set ์˜ ๊ธธ์ด๊ฐ€ ๊ฐ™์€์ง€ return ํ–ˆ์Šต๋‹ˆ๋‹ค.
9+
10+
์‹œ๊ฐ„ ๋ณต์žก๋„:
11+
O(n) - has set ์„ ๋งŒ๋“œ๋Š” ์‹œ๊ฐ„
12+
13+
๊ณต๊ฐ„ ๋ณต์žก๋„:
14+
O(n) - n๊ฐœ์˜ ์š”์†Œ๋ฅผ set์— ๋‹ด๊ธฐ ๋•Œ๋ฌธ
15+
'''
16+
17+
18+
class Solution:
19+
def containsDuplicate(self, nums: List[int]) -> bool:
20+
nums_set = set(nums)
21+
return len(nums_set) != len(nums)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package leetcode_study
2+
3+
/**
4+
* Set ์ž๋ฃŒ ๊ตฌ์กฐ๋กœ ๋ณ€๊ฒฝ ํ›„ ์›์†Œ์˜ ๊ฐœ์ˆ˜๋ฅผ ๋น„๊ตํ•ด ๋ฌธ์ œ ํ•ด๊ฒฐ
5+
* ์‹œ๊ฐ„ ๋ณต์žก๋„ : O(n)
6+
* -> ๋ชจ๋“  Array์˜ ์›์†Œ๋ฅผ ์ˆœํšŒํ•ด์•ผํ•จ.
7+
* ๊ณต๊ฐ„ ๋ณต์žก๋„ : O(n)
8+
* -> IntArray์˜ ์š”์†Œ ๊ฐœ์ˆ˜์— ๋น„๋ก€ํ•˜์—ฌ ์ถ”๊ฐ€์ ์ธ ๊ณต๊ฐ„์ด ํ•„์š”ํ•จ.
9+
*/
10+
fun containsDuplicate(nums: IntArray): Boolean {
11+
val changeSet = nums.toSet()
12+
return changeSet.size != nums.size
13+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import java.util.Arrays;
2+
import java.util.stream.Collectors;
3+
4+
class Solution {
5+
public boolean containsDuplicate(int[] nums) {
6+
/***
7+
compare length of array and length of set.
8+
O(n) given that n is length of array nums
9+
*/
10+
return nums.length > Arrays.stream(nums).boxed().collect(Collectors.toSet()).size();
11+
}
12+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// ์ฃผ์–ด์ง„ ๋ฐฐ์—ด์„ ์„ ํ˜• ์ˆœํšŒ ํ•˜๋ฏ€๋กœ, ์‹œ๊ฐ„ ๋ณต์žก๋„์™€ ๊ณต๊ฐ„ ๋ณต์žก๋„ ๋ชจ๋‘ O(n)์ผ ๊ฒƒ์œผ๋กœ ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค.
2+
3+
package week01
4+
5+
// ์ฃผ์–ด์ง„ ๋ฐฐ์—ด nums๋ฅผ ์ˆœํšŒํ•˜๋ฉฐ ์ค€๋น„ํ•œ ๋งต์— ํ‘œ์‹œ๋ฅผ ํ•˜๋˜,
6+
// - ํ‚ค๊ฐ’์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ’์ด ์—†์œผ๋ฉด ๋งต์˜ ํ•ด๋‹น ํ‚ค๊ฐ’์— true๋ฅผ ์ €์žฅํ•˜๊ณ 
7+
// - ํ‚ค๊ฐ’์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ’์ด ์žˆ์œผ๋ฉด ์ฆ‰์‹œ true๋ฅผ ๋ฐ˜ํ™˜.
8+
// - ์ˆœํšŒ ํ›„์—๋„ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ ์ค‘๋ณต์ด ๋ฐœ๊ฒฌ๋˜์ง€ ์•Š์•˜์œผ๋ฏ€๋กœ false๋ฅผ ๋ฐ˜ํ™˜.
9+
func containsDuplicate(nums []int) bool {
10+
dup := make(map[int]bool, 0)
11+
for _, n := range nums {
12+
if _, ok := dup[n]; !ok {
13+
dup[n] = true
14+
} else {
15+
return true
16+
}
17+
}
18+
19+
return false
20+
}
21+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//์‹œ๊ฐ„๋ณต์žก๋„ : O(nlogn)
2+
class Solution {
3+
public boolean containsDuplicate(int[] nums) {
4+
Arrays.sort(nums);
5+
for(int i = 0; i < nums.length - 1; i++) {
6+
if(nums[i] == nums[i+1])
7+
return true;
8+
}
9+
return false;
10+
}
11+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
Runtime: 10 ms(Beats: 89.16 %)
3+
Time Complexity: O(n)
4+
- HashSet r/w : O(1)
5+
- nums iteration : ( O(1) + O(1) ) * n = O(n)
6+
7+
Memory: 58.63 MB(Beats: 22.32 %)
8+
Space Complexity: O(n)
9+
*/
10+
11+
12+
class Solution {
13+
public boolean containsDuplicate(int[] nums) {
14+
Set<Integer> set = new HashSet<>();
15+
16+
for (int num: nums) {
17+
if (set.contains(num)) {
18+
return true;
19+
}
20+
21+
set.add(num);
22+
}
23+
24+
return false;
25+
}
26+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Time complexity: O(n)
2+
// Space complexity: O(n)
3+
4+
/**
5+
* @param {number[]} nums
6+
* @return {boolean}
7+
*/
8+
var containsDuplicate = function (nums) {
9+
const set = new Set(nums);
10+
11+
return set.size !== nums.length;
12+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Big-O ์˜ˆ์ƒ : O(n)
2+
class Solution:
3+
def containsDuplicate(self, nums: List[int]) -> bool:
4+
num_dict = {}
5+
for num in nums:
6+
if num in num_dict:
7+
return True
8+
else:
9+
num_dict[num] = 1
10+
return False
11+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {boolean}
4+
*/
5+
var containsDuplicate = function(nums) {
6+
7+
// ์ฒซ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•: filter + indexOf ์‚ฌ์šฉ => indexOf(),filter() ๊ฐ๊ฐ ์‹œ๊ฐ„๋ณต์žก๋„ O(n) ๋‘ ๊ฐœ๊ฐ€ ์ค‘์ฒฉ์ด๋ฏ€๋กœ ์‹œ๊ฐ„๋ณต์žก๋„ O(n^2)
8+
// Runtime: Time Limit Exceeded ๋ฐœ์ƒ
9+
const method1 = function() {
10+
const filterNums = nums.filter((item,index) => nums.indexOf(item) !== index);
11+
return filterNums.length > 0;
12+
}
13+
14+
// ๋‘ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•: Set ์‚ฌ์šฉ => nums ๋ฐฐ์—ด์„ set์œผ๋กœ ๋ณ€ํ™˜ํ•  ๋•Œ ํ•œ๋ฒˆ์”ฉ ํ™•์ธํ•˜๋ฉด ๋˜๋ฏ€๋กœ ์‹œ๊ฐ„๋ณต์žก๋„ O(n)
15+
// Runtime: 14ms
16+
const method2 = function() {
17+
const setNums = new Set(nums);
18+
return setNums.size !== nums.length;
19+
}
20+
21+
// ์œ„ ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ• ์ค‘ Set์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์„ฑ๋Šฅ์ƒ ํ›จ์”ฌ ๋‚˜์Œ
22+
return method2();
23+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Source: https://leetcode.com/problems/contains-duplicate/
3+
* ํ’€์ด๋ฐฉ๋ฒ•: Set์„ ์ด์šฉํ•˜์—ฌ ์ค‘๋ณต๋œ ๊ฐ’์ด ์žˆ๋Š”์ง€ ํ™•์ธ
4+
* ์‹œ๊ฐ„๋ณต์žก๋„: O(n)
5+
* ๊ณต๊ฐ„๋ณต์žก๋„: O(n)
6+
*
7+
* ์ƒ๊ฐ๋‚˜๋Š” ํ’€์ด๋ฐฉ๋ฒ•
8+
* 1. ๋‹จ์ˆœํ•˜๊ฒŒ sorted๋ฅผ ์ด์šฉํ•˜์—ฌ ์ด์ „๊ฐ’๊ณผ ๋น„๊ตํ•˜์—ฌ ์ค‘๋ณต๋œ ๊ฐ’์ด ์žˆ๋Š”์ง€ ํ™•์ธ
9+
* 2. ์ •๋ ฌํ•˜์ง€ ์•Š๊ณ  nums์˜ ๊ธธ์ด๋งŒํผ์˜ ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด์„œ ์ค‘๋ณต๋œ ๊ฐ’์ด ์žˆ๋Š”์ง€ ์ €์žฅํ•˜๋ฉด์„œ ํ™•์ธ
10+
*/
11+
function containsDuplicate(nums: number[]): boolean {
12+
// ์ค‘๋ณต๋œ ๊ฐ’์ด ์—†๋Š” ์ž๋ฃŒ๊ตฌ์กฐ Set ํ™œ์šฉ
13+
const set = new Set<number>(nums);
14+
// Set์˜ size์™€ nums์˜ length๋ฅผ ๋น„๊ตํ•˜์—ฌ ์ค‘๋ณต๋œ ๊ฐ’์ด ์žˆ๋Š”์ง€ ํ™•์ธ
15+
return set.size !== nums.length;
16+
}

0 commit comments

Comments
ย (0)