File tree Expand file tree Collapse file tree 1 file changed +55
-0
lines changed
Expand file tree Collapse file tree 1 file changed +55
-0
lines changed Original file line number Diff line number Diff line change 1+ package contains_duplicate
2+
3+ import "sort"
4+
5+ /*
6+ 1. ๋ฌธ์
7+
8+ ์ฃผ์ด์ง int ๋ฐฐ์ด nums์ ์ซ์๊ฐ ์ค๋ณต๋๋ ๊ฒฝ์ฐ๊ฐ ํ ๋ฒ์ด๋ผ๋ ์์ผ๋ฉด true, ๊ทธ๋ ์ง ์์ผ๋ฉด false ๋ฅผ ๋ฆฌํด
9+
10+ 2. ํ์ด
11+
12+ ๊ณ ์ ๊ฐ๋ง ์ ์ฅํ๋ set(go ์์๋ map)์ ์ฑ์ง์ ํ์ฉํ์ฌ
13+ nums๋ฅผ ์ํํ๋ฉฐ set์ ๊ฐ์ด ์๋์ง ์๋์ง ์ฒดํฌํ์ฌ
14+ ์ซ์๊ฐ ์ค๋ณต๋๋ ๊ฒฝ์ฐ๋ฅผ ์ฒดํฌ
15+
16+ 3. ๋ถ์
17+ - ์๊ฐ ๋ณต์ก๋: O(N)
18+ nums ํ์: O(N)
19+ ๋ฐฐ์ด nums์ ๋ชจ๋ ์์๋ฅผ ๋จ ํ ๋ฒ ์ํ
20+ map ์ฝ์
, ํ์: O(1)
21+ map์ ๋ด๋ถ ๊ตฌํ์ ํด์ ํ
์ด๋ธ.
22+ O(N)๋ณด๋ค ์์ ๋ฌด์๋จ
23+ - ๊ณต๊ฐ ๋ณต์ก๋: O(N)
24+ ์ต์
์ ๊ฒฝ์ฐ๋ผ๋ ์ฌ์ฉ๊ณต๊ฐ์ nums ์ ํฌ๊ธฐ๋งํผ + nums์ ๋ชจ๋ ์์๋ฅผ ํฌํจํ map
25+ */
26+ func containsDuplicate (nums []int ) bool {
27+ seen := map [int ]int {}
28+
29+ for _ , n := range nums {
30+ if _ , ok := seen [n ]; ok {
31+ return true
32+ }
33+
34+ seen [n ] = 1
35+ }
36+
37+ return false
38+ }
39+
40+ func containsDuplicate_SortedApproach (nums []int ) bool {
41+ // early exit for small slices
42+ if len (nums ) < 2 {
43+ return false
44+ }
45+
46+ // sort in ascending order and check adjacent elements
47+ sort .Ints (nums )
48+ for i := 1 ; i < len (nums ); i ++ {
49+ if nums [i ] == nums [i - 1 ] {
50+ return true
51+ }
52+ }
53+
54+ return false
55+ }
You canโt perform that action at this time.
0 commit comments