File tree Expand file tree Collapse file tree 5 files changed +75
-0
lines changed
longest-consecutive-sequence Expand file tree Collapse file tree 5 files changed +75
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution :
2+ def containsDuplicate (self , nums : List [int ]) -> bool :
3+ keys = set ()
4+ for num in nums :
5+ if num in keys :
6+ return True
7+ else :
8+ keys .add (num )
9+
10+ return False
Original file line number Diff line number Diff line change 1+ class Solution :
2+ def rob (self , nums : List [int ]) -> int :
3+ memo = {}
4+
5+ def getAmount (start : int ) -> int :
6+ if not start < len (nums ):
7+ memo [start ] = 0
8+ if start in memo :
9+ return memo [start ]
10+
11+ memo [start ] = max (nums [start ] + getAmount (start + 2 ), getAmount (start + 1 ))
12+
13+ return memo [start ]
14+
15+ return getAmount (0 )
16+
Original file line number Diff line number Diff line change 1+ class Solution :
2+ def longestConsecutive (self , nums : List [int ]) -> int :
3+ result , last = 0 , None
4+ candidates = []
5+
6+ for num in sorted (set (nums )):
7+ if last is None or last + 1 == num :
8+ result += 1
9+ else :
10+ candidates .append (result )
11+ result = 1
12+ last = num
13+
14+ if result is not 0 :
15+ candidates .append (result )
16+
17+ return max (candidates ) if len (candidates ) > 0 else 0
18+
Original file line number Diff line number Diff line change 1+ class Solution :
2+ def topKFrequent (self , nums : List [int ], k : int ) -> List [int ]:
3+ counters = {}
4+
5+ for num in nums :
6+ if counters .get (num ):
7+ counters [num ] += 1
8+ else :
9+ counters [num ] = 1
10+
11+ return [val [0 ] for val in sorted (counters .items (), key = lambda x : x [1 ], reverse = True )[:k ]]
12+
Original file line number Diff line number Diff line change 1+ import re
2+
3+ class Solution :
4+ def isPalindrome (self , s : str ) -> bool :
5+ replaced_string = re .sub (r"[^a-zA-Z0-9]" , "" , s ).lower ()
6+
7+ if len (replaced_string ) == 0 :
8+ return True
9+
10+ start , end = 0 , len (replaced_string )- 1
11+
12+ while start <= len (replaced_string ) // 2 :
13+ if replaced_string [start ] is not replaced_string [end ]:
14+ return False
15+
16+ start += 1
17+ end -= 1
18+
19+ return True
You can’t perform that action at this time.
0 commit comments