Skip to content

Commit 08cdcb8

Browse files
committed
week01_1_2_3_solution
1 parent 0e5ffe5 commit 08cdcb8

3 files changed

Lines changed: 47 additions & 0 deletions

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# https://leetcode.com/problems/contains-duplicate/
2+
3+
class Solution(object):
4+
def containsDuplicate(self, nums):
5+
if len(nums) > len(set(nums)):
6+
return True
7+
else:
8+
return False
9+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# https://leetcode.com/problems/top-k-frequent-elements/
2+
3+
class Solution(object):
4+
def topKFrequent(self, nums, k):
5+
cnt = {}
6+
for num in nums:
7+
if num in cnt:
8+
cnt[num] += 1
9+
else:
10+
cnt[num] = 0
11+
12+
sorted_cnt = sorted(cnt.items(), key=lambda x: x[1], reverse=True)
13+
14+
return [item[0] for item in sorted_cnt[:k]]

โ€Žtwo-sum/YOOHYOJEONG.pyโ€Ž

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# https://leetcode.com/problems/two-sum/description/
2+
3+
# class Solution(object):
4+
# def twoSum(self, nums, target):
5+
# for i in range(len(nums)):
6+
# for j in range(len(nums)):
7+
# if nums[i]+nums[j] == target:
8+
# if i != j:
9+
# return [i, j]
10+
# > ํ•ด๋‹น ๋ฐฉ์‹ ์‚ฌ์šฉ ์‹œ ์‹œ๊ฐ„ ๋ณต์žก๋„๊ฐ€ O(nยฒ)์ด๋ผ ๊ฐœ์„ ์„ ํ•ด ๋ณด๊ณ ์ž ์•„๋ž˜ ์†”๋ฃจ์…˜์œผ๋กœ ์žฌํ’€์ด ์ง„ํ–‰
11+
12+
class Solution(object):
13+
def twoSum(self, nums, target):
14+
seen = {}
15+
16+
for i, num in enumerate(nums):
17+
diff = target - num
18+
19+
if diff in seen:
20+
return [seen[diff], i]
21+
22+
seen[num] = i
23+
24+
# index๋ฅผ ๊ธฐ์–ตํ•˜๋„๋ก ํ•˜๋ฉด ๋ฐ˜๋ณต๋ฌธ ํ•œ๋ฒˆ O(n), ๋”•์…”๋„ˆ๋ฆฌ ์กฐํšŒ ํ‰๊ท  O(1)๋กœ ์ „์ฒด O(n)์ด ๋จ

0 commit comments

Comments
ย (0)