File tree Expand file tree Collapse file tree 3 files changed +52
-0
lines changed
longest-consecutive-sequence Expand file tree Collapse file tree 3 files changed +52
-0
lines changed Original file line number Diff line number Diff line change 1+
2+ # set에 저장하면서 중복 여부 확인하기
3+ class Solution :
4+ def containsDuplicate (self , nums : list [int ]) -> bool :
5+ hashset = set ()
6+ for i in nums :
7+ if i in hashset :
8+ return True
9+ hashset .add (i )
10+ return False
Original file line number Diff line number Diff line change 1+ # 배열을 정렬하고 포인터를 두개 사용
2+ class Solution :
3+ def longestConsecutive (self , nums : List [int ]) -> int :
4+ if not nums :
5+ return 0
6+
7+ # sort the given list
8+ sorted_nums = sorted (nums )
9+
10+ longest = 1
11+ curr = 1
12+
13+ l , r = 0 , 1
14+
15+ while r < len (sorted_nums ):
16+ # case 1: exactly consecutive (x, x+1)
17+ if sorted_nums [r ] == sorted_nums [l ] + 1 :
18+ curr += 1
19+ longest = max (longest , curr )
20+ l += 1
21+ r += 1
22+
23+ # case 2: duplicate (x, x) → ignore, move right pointer
24+ elif sorted_nums [r ] == sorted_nums [l ]:
25+ l += 1
26+ r += 1
27+
28+ # case 3: gap
29+ else :
30+ curr = 1
31+ l = r
32+ r += 1
33+
34+ return longest
Original file line number Diff line number Diff line change 1+ class Solution :
2+ def twoSum (self , nums : list [int ], target : int ) -> list [int ]:
3+ prevMap = {} # val : index
4+ for i , n in enumerate (nums ):
5+ diff = target - n
6+ if diff in prevMap :
7+ return [prevMap [diff ], i ]
8+ prevMap [n ] = i
You can’t perform that action at this time.
0 commit comments