File tree Expand file tree Collapse file tree 4 files changed +60
-0
lines changed
product-of-array-except-self Expand file tree Collapse file tree 4 files changed +60
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ # time complexity: O(n^2)
3
+ def threeSum (self , nums : List [int ]) -> List [List [int ]]:
4
+ nums .sort ()
5
+ answer = set ()
6
+
7
+ for i in range (len (nums ) - 2 ):
8
+ if i > 0 and nums [i ] == nums [i - 1 ]:
9
+ continue
10
+
11
+ left , right = i + 1 , len (nums ) - 1
12
+ while left < right :
13
+ sum = nums [i ] + nums [left ] + nums [right ]
14
+ if sum == 0 :
15
+ answer .add ((nums [i ], nums [left ], nums [right ]))
16
+ left += 1
17
+ right -= 1
18
+ elif sum > 0 :
19
+ right -= 1
20
+ elif sum < 0 :
21
+ left += 1
22
+
23
+ return [list (x ) for x in answer ]
24
+
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ # time complexity: O(n)
3
+ def climbStairs (self , n : int ) -> int :
4
+ if n <= 2 :
5
+ return n
6
+
7
+ prev1 , prev2 = 1 , 2
8
+ for _ in range (3 , n + 1 ):
9
+ current = prev1 + prev2
10
+ prev1 , prev2 = prev2 , current
11
+ return prev2
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ # time complexity: O(n)
3
+ def productExceptSelf (self , nums : List [int ]) -> List [int ]:
4
+ n = len (nums )
5
+ answer = [1 ] * n
6
+
7
+ prefix = 1
8
+ for i in range (n ):
9
+ answer [i ] = prefix
10
+ prefix *= nums [i ]
11
+
12
+ suffix = 1
13
+ for i in range (n - 1 , - 1 , - 1 ):
14
+ answer [i ] *= suffix
15
+ suffix *= nums [i ]
16
+
17
+ return answer
18
+
Original file line number Diff line number Diff line change
1
+ from collections import Counter
2
+
3
+ class Solution :
4
+ # time complexity: O(n)
5
+ def isAnagram (self , s : str , t : str ) -> bool :
6
+ return Counter (s ) == Counter (t )
7
+
You can’t perform that action at this time.
0 commit comments