File tree Expand file tree Collapse file tree 4 files changed +49
-0
lines changed
product-of-array-except-self Expand file tree Collapse file tree 4 files changed +49
-0
lines changed Original file line number Diff line number Diff line change
1
+ from typing import List
2
+
3
+
4
+ class Solution :
5
+ def threeSum (self , nums : List [int ]) -> List [List [int ]]:
6
+ if len (nums ) >= 3 and set (nums ) == set ([0 ]):
7
+ return [[0 , 0 , 0 ]]
8
+ triplets = set ()
9
+ for i in range (len (nums ) - 2 ):
10
+ seen = set ()
11
+ for j in range (i + 1 , len (nums )):
12
+ f = - (nums [i ] + nums [j ])
13
+ if f in seen :
14
+ triplet = [nums [i ], nums [j ], f ]
15
+ triplets .add (tuple (sorted (triplet )))
16
+ seen .add (nums [j ])
17
+ return list (triplets )
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def climbStairs (self , n : int ) -> int :
3
+ if n <= 1 :
4
+ return 1
5
+ dp = [0 ] * n
6
+ dp [0 ] = 1
7
+ dp [1 ] = 2
8
+ for i in range (3 , n + 1 ):
9
+ dp [i - 1 ] = dp [i - 2 ] + dp [i - 3 ]
10
+ return dp [n - 1 ]
Original file line number Diff line number Diff line change
1
+ from typing import List
2
+
3
+
4
+ class Solution :
5
+ def productExceptSelf (self , nums : List [int ]) -> List [int ]:
6
+ len_n = len (nums )
7
+ front_cum = [1 ] * len_n
8
+ back_cum = [1 ] * len_n
9
+
10
+ for i in range (1 , len_n ):
11
+ front_cum [i ] = front_cum [i - 1 ] * nums [i - 1 ]
12
+
13
+ for j in range (1 , len_n ):
14
+ idx = len_n - j - 1
15
+ back_cum [idx ] = back_cum [idx + 1 ] * nums [idx + 1 ]
16
+
17
+ final = [front_cum [i ] * back_cum [i ] for i in range (len_n )]
18
+
19
+ return final
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def isAnagram (self , s : str , t : str ) -> bool :
3
+ return sorted (s ) == sorted (t )
You can’t perform that action at this time.
0 commit comments