File tree Expand file tree Collapse file tree 5 files changed +110
-2
lines changed
product_of_array_except_self
unique_binary_search_trees Expand file tree Collapse file tree 5 files changed +110
-2
lines changed Original file line number Diff line number Diff line change @@ -27,8 +27,7 @@ def productExceptSelf(self, nums):
2727 for i in range (1 , n ):
2828 for j in range (i ):
2929 res [j ] *= nums [i ]
30- if i > 0 :
31- product *= nums [i - 1 ]
30+ product *= nums [i - 1 ]
3231 res [i ] = product
3332 return res
3433
Original file line number Diff line number Diff line change @@ -23,6 +23,8 @@ def productExceptSelf(self, nums):
2323 res = [1 for i in range (n )]
2424 # Scan from left to right
2525 for i in range (1 , n ):
26+ # i is from 1 to n - 1
27+ # res[i] is the product accumulated to the left
2628 res [i ] = res [i - 1 ] * nums [i - 1 ]
2729
2830 # right_product is the product accumulated to the right
Original file line number Diff line number Diff line change 1+ # -*- coding: utf-8 -*-
2+ """
3+ Given a sorted array and a target value, return the index if the target is
4+ found. If not, return the index where it would be if it were inserted in
5+ order.
6+
7+ You may assume no duplicates in the array.
8+
9+ Here are few examples.
10+ [1,3,5,6], 5 → 2
11+ [1,3,5,6], 2 → 1
12+ [1,3,5,6], 7 → 4
13+ [1,3,5,6], 0 → 0
14+
15+ """
16+
17+ class Solution (object ):
18+ def searchInsert (self , nums , target ):
19+ n = len (nums )
20+ left = 0
21+ right = n - 1
22+ while left + 1 < right :
23+ mid = left + (right - left ) / 2
24+ if mid > 0 and nums [mid - 1 ] < target < nums [mid ]:
25+ return mid
26+ elif target <= nums [mid ]:
27+ right = mid
28+ else :
29+ left = mid
30+ if nums [left ] < target < nums [right ]:
31+ return left + 1
32+ elif nums [left ] == target :
33+ return left
34+ elif nums [right ] == target :
35+ return right
36+ elif nums [left ] > target :
37+ return min (0 , left )
38+ elif nums [right ] < target :
39+ return max (n , right )
40+
41+
42+ a1 = [1 , 3 ]
43+ s = Solution ()
44+ print s .searchInsert (a1 , 2 )
Original file line number Diff line number Diff line change 1+ """
2+ Given n, how many structurally unique BST's (binary search trees) that store
3+ values 1...n?
4+
5+ For example,
6+ Given n = 3, there are a total of 5 unique BST's.
7+
8+ 1 3 3 2 1
9+ \ / / / \ \
10+ 3 2 1 1 3 2
11+ / / \ \
12+ 2 1 2 3
13+ """
14+
15+ class Solution (object ):
16+ def numTrees (self , n ):
17+ """
18+ :type n: int
19+ :rtype: int
20+ """
21+ t = [- 1 for _ in range (n + 1 )]
22+ return self .num_trees_aux (n , t )
23+
24+ def num_trees_aux (self , n , t ):
25+ if n == 0 or n == 1 :
26+ return 1
27+ elif t [n ] != - 1 :
28+ return t [n ]
29+ else :
30+ res = 0
31+ for i in range (n ):
32+ res += self .num_trees_aux (i , t ) * \
33+ self .num_trees_aux (n - 1 - i , t )
34+ t [n ] = res
35+ return res
Original file line number Diff line number Diff line change 1+ """
2+ Given n, how many structurally unique BST's (binary search trees) that store
3+ values 1...n?
4+
5+ For example,
6+ Given n = 3, there are a total of 5 unique BST's.
7+
8+ 1 3 3 2 1
9+ \ / / / \ \
10+ 3 2 1 1 3 2
11+ / / \ \
12+ 2 1 2 3
13+ """
14+
15+ class Solution (object ):
16+ def numTrees (self , n ):
17+ t = [- 1 for _ in range (n + 1 )]
18+ t [0 ] = 1
19+ t [1 ] = 1
20+ if n <= 1 :
21+ return t [n ]
22+ else :
23+ for i in range (2 , n + 1 ):
24+ res = 0
25+ for j in range (i ):
26+ res += t [j ] * t [i - 1 - j ]
27+ t [i ] = res
28+ return t [n ]
You can’t perform that action at this time.
0 commit comments