File tree Expand file tree Collapse file tree
longest-consecutive-sequence Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 1+ # https://leetcode.com/problems/house-robber/
2+
3+ class Solution (object ):
4+ def rob (self , nums ):
5+
6+ prev = 0
7+ prev_2 = 0
8+
9+ for num in nums :
10+ cur = max (prev , prev_2 + num )
11+
12+ prev_2 = prev
13+ prev = cur
14+
15+ return prev
Original file line number Diff line number Diff line change 1+ # https://leetcode.com/problems/longest-consecutive-sequence/
2+
3+ class Solution (object ):
4+ def longestConsecutive (self , nums ):
5+
6+ nums_set = set (nums )
7+ lens = 0
8+
9+ for n in nums_set :
10+
11+ if n - 1 not in nums_set :
12+ cur = n
13+ lenth = 1
14+
15+ while cur + 1 in nums_set :
16+ cur += 1
17+ lenth += 1
18+
19+ lens = max (lens , lenth )
20+
21+ return lens
Original file line number Diff line number Diff line change 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 ]]
Original file line number Diff line number Diff line change 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)์ด ๋จ
You canโt perform that action at this time.
0 commit comments