forked from taizilongxu/Leetcode-Py
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 0074abb
Showing
118 changed files
with
1,586 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
download.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
class Solution: | ||
def threeSumClosest(self, A, target): | ||
A, result, closest_diff, i = sorted(A), 2147483647, 2147483647, 0 | ||
while i < len(A) - 2: | ||
j, k = i + 1, len(A) - 1 | ||
while j < k: | ||
diff = A[i] + A[j] + A[k] - target | ||
if diff < 0: | ||
if math.fabs(diff) < math.fabs(closest_diff): | ||
result, closest_diff = A[i] + A[j] + A[k], diff | ||
j += 1 | ||
elif diff > 0: | ||
if math.fabs(diff) < math.fabs(closest_diff): | ||
result, closest_diff = A[i] + A[j] + A[k], diff | ||
k -= 1 | ||
else: | ||
return target | ||
i += 1 | ||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class Solution: | ||
def threeSum(self, A): | ||
A, result, i = sorted(A), [], 0 | ||
while i < len(A) - 2: | ||
j, k = i + 1, len(A) - 1 | ||
while j < k: | ||
if A[i] + A[j] + A[k] < 0: | ||
j += 1 | ||
elif A[i] + A[j] + A[k] > 0: | ||
k -= 1 | ||
else: | ||
result.append([A[i], A[j], A[k]]) | ||
j, k = j + 1, k - 1 | ||
while j < k and A[j] == A[j - 1]: | ||
j += 1 | ||
while j < k and A[k] == A[k + 1]: | ||
k -= 1 | ||
i += 1 | ||
while i < len(A) - 2 and A[i] == A[i - 1]: | ||
i += 1 | ||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class Solution: | ||
def addBinary(self, a, b): | ||
return bin(int(a, 2) + int(b, 2)).split('b')[1] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
class Solution: | ||
def addTwoNumbers(self, l1, l2): | ||
dummy = ListNode(0) | ||
current, carry = dummy, 0 | ||
while l1 != None or l2 != None: | ||
res = carry | ||
if l1 != None: | ||
res += l1.val | ||
l1 = l1.next | ||
if l2 != None: | ||
res += l2.val | ||
l2 = l2.next | ||
carry, res = res / 10, res % 10 | ||
current.next = ListNode(res) | ||
current = current.next | ||
if carry == 1: | ||
current.next = ListNode(1) | ||
return dummy.next |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class Solution: | ||
def anagrams(self, strs): | ||
map, result = {}, [] | ||
if len(strs) >= 1: | ||
for str in strs: | ||
sorted_str = "".join(sorted(str)) | ||
if sorted_str not in map: | ||
map[sorted_str] = [str] | ||
else: | ||
map[sorted_str].append(str) | ||
for word in map: | ||
if len(map[word]) > 1: | ||
result += map[word] | ||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class Solution: | ||
def isBalanced(self, root): | ||
return self.getHeight(root) != -1 | ||
|
||
def getHeight(self, root): | ||
if root == None: | ||
return 0 | ||
left_height, right_height = self.getHeight(root.left), self.getHeight(root.right) | ||
if left_height < 0 or right_height < 0 or math.fabs(left_height - right_height) > 1: | ||
return -1 | ||
return max(left_height, right_height) + 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class Solution: | ||
def maxProfit(self, prices): | ||
profit = 0 | ||
for i in range(len(prices) - 1): | ||
if (prices[i] < prices[i+1]): | ||
profit += prices[i+1] - prices[i] | ||
return profit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class Solution: | ||
def maxProfit(self, prices): | ||
min_price, max_profit = 9223372036854775807, 0 | ||
profits_before = [0 for i in range(len(prices))] | ||
for i in range(len(prices)): | ||
min_price = min(prices[i], min_price) | ||
max_profit = max(prices[i] - min_price, max_profit) | ||
profits_before[i] = max_profit | ||
max_price, max_profit = -9223372036854775808, 0 | ||
profits_after = [0 for i in range(len(prices))] | ||
for i in reversed(range(len(prices))): | ||
max_price = max(prices[i], max_price) | ||
max_profit = max(max_price - prices[i], max_profit) | ||
profits_after[i] = max_profit | ||
return reduce(lambda acc, i: max(profits_before[i] + profits_after[i], acc), range(len(prices)), 0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class Solution: | ||
def maxProfit(self, prices): | ||
min_price = 9223372036854775807 | ||
max_profit = 0 | ||
for i in range(len(prices)): | ||
min_price = min(prices[i], min_price) | ||
max_profit = max(prices[i] - min_price, max_profit) | ||
return max_profit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class Solution: | ||
def inorderTraversal(self, root): | ||
result = [] | ||
self.recur(root, result) | ||
return result | ||
|
||
def recur(self, root, result): | ||
if(root == None): | ||
return | ||
self.recur(root.left, result) | ||
result.append(root.val) | ||
self.recur(root.right, result) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
class Solution: | ||
def levelOrderBottom(self, root): | ||
result = [] | ||
if root == None: | ||
return result | ||
current = [root] | ||
while len(current) > 0: | ||
# Python is just so slow, but who I can complain? I have to write following code to optimize for skewed tree | ||
# -- None of the code from this tag to the next tag is actually needed -- # | ||
if len(current) == 1 and (current[0].left == None or current[0].right== None): | ||
this_node = current[0] | ||
result.insert(0, [this_node.val]) | ||
if this_node.left == None and this_node.right != None: | ||
current = [this_node.right] | ||
elif this_node.left != None and this_node.right == None: | ||
current = [this_node.left] | ||
else: | ||
current.pop(0) | ||
else: | ||
# -- None of the code above this tag up to the previous tag is actually needed -- # | ||
vals = [] | ||
size = len(current) | ||
for i in range(size): | ||
this_node = current[0] | ||
vals.append(this_node.val) | ||
if i < len(current) - 1: | ||
this_node.next = current[i+1] | ||
if this_node.left != None: | ||
current.append(this_node.left) | ||
if this_node.right != None: | ||
current.append(this_node.right) | ||
current.pop(0) | ||
result.insert(0, vals) | ||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
class Solution: | ||
def levelOrder(self, root): | ||
result = [] | ||
if root == None: | ||
return result | ||
current = [root] | ||
while len(current) > 0: | ||
# Python is just so slow, but who I can complain? I have to write following code to optimize for skewed tree | ||
# -- None of the code from this tag to the next tag is actually needed -- # | ||
if len(current) == 1 and (current[0].left == None or current[0].right== None): | ||
this_node = current[0] | ||
result.append([this_node.val]) | ||
if this_node.left == None and this_node.right != None: | ||
current = [this_node.right] | ||
elif this_node.left != None and this_node.right == None: | ||
current = [this_node.left] | ||
else: | ||
current.pop(0) | ||
else: | ||
# -- None of the code above this tag up to the previous tag is actually needed -- # | ||
vals = [] | ||
size = len(current) | ||
for i in range(size): | ||
this_node = current[0] | ||
vals.append(this_node.val) | ||
if i < len(current) - 1: | ||
this_node.next = current[i+1] | ||
if this_node.left != None: | ||
current.append(this_node.left) | ||
if this_node.right != None: | ||
current.append(this_node.right) | ||
current.pop(0) | ||
result.append(vals) | ||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class Solution: | ||
def postorderTraversal(self, root): | ||
result = [] | ||
self.recur(root, result) | ||
return result | ||
|
||
def recur(self, root, result): | ||
if(root == None): | ||
return | ||
self.recur(root.left, result) | ||
self.recur(root.right, result) | ||
result.append(root.val) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class Solution: | ||
def preorderTraversal(self, root): | ||
result = [] | ||
self.recur(root, result) | ||
return result | ||
|
||
def recur(self, root, result): | ||
if(root == None): | ||
return | ||
result.append(root.val) | ||
self.recur(root.left, result) | ||
self.recur(root.right, result) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
class Solution: | ||
def zigzagLevelOrder(self, root): | ||
result = [] | ||
if root == None: | ||
return result | ||
current, reverse = [root], 0 | ||
while len(current) > 0: | ||
# Python is just so slow, but who I can complain? I have to write following code to optimize for skewed tree | ||
# -- None of the code from this tag to the next tag is actually needed -- # | ||
if len(current) == 1 and (current[0].left == None or current[0].right== None): | ||
this_node = current[0] | ||
result.append([this_node.val]) | ||
if this_node.left == None and this_node.right != None: | ||
current = [this_node.right] | ||
elif this_node.left != None and this_node.right == None: | ||
current = [this_node.left] | ||
else: | ||
current.pop(0) | ||
else: | ||
# -- None of the code above this tag up to the previous tag is actually needed -- # | ||
vals = [] | ||
size = len(current) | ||
for i in range(size): | ||
this_node = current[0] | ||
if reverse: | ||
vals.insert(0, this_node.val) | ||
else: | ||
vals.append(this_node.val) | ||
if i < len(current) - 1: | ||
this_node.next = current[i+1] | ||
if this_node.left != None: | ||
current.append(this_node.left) | ||
if this_node.right != None: | ||
current.append(this_node.right) | ||
current.pop(0) | ||
result.append(vals) | ||
reverse = 1 - reverse | ||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class Solution: | ||
def candy(self, ratings): | ||
candies = [1 for i in range(len(ratings))] | ||
for i in range(1, len(ratings)): | ||
if ratings[i] > ratings[i - 1]: | ||
candies[i] = candies[i - 1] + 1 | ||
for i in reversed(range(1, len(ratings))): | ||
if ratings[i - 1] > ratings[i] and candies[i - 1] <= candies[i]: | ||
candies[i - 1] = candies[i] + 1 | ||
return reduce(operator.add, candies) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class Solution: | ||
def climbStairs(self, n): | ||
prev, current = 1, 0 | ||
for i in range(n + 1): | ||
prev, current = current, prev + current | ||
return current |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class Solution: | ||
def combinationSum2(self, candidates, target): | ||
result = [] | ||
self.combinationSumRecur(sorted(candidates), result, [], 0, target) | ||
return result | ||
|
||
def combinationSumRecur(self, candidates, result, current, start, target): | ||
if target == 0 and current not in result: | ||
result.append(current) | ||
else: | ||
while start < len(candidates) and candidates[start] <= target: | ||
self.combinationSumRecur(candidates, result, current + [candidates[start]], start + 1, target - candidates[start]) | ||
start += 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class Solution: | ||
def combinationSum(self, candidates, target): | ||
result = [] | ||
self.combinationSumRecur(sorted(candidates), result, [], 0, target) | ||
return result | ||
|
||
def combinationSumRecur(self, candidates, result, current, start, target): | ||
if target == 0: | ||
result.append(current) | ||
else: | ||
while start < len(candidates) and candidates[start] <= target: | ||
self.combinationSumRecur(candidates, result, current + [candidates[start]], start, target - candidates[start]) | ||
start += 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class Solution: | ||
def combine(self, n, k): | ||
return self.combineRecur([], n, k, 1) | ||
|
||
def combineRecur(self, current, n, k, i): | ||
if k == 0: | ||
return [current] | ||
if i > n: | ||
return [] | ||
return self.combineRecur(current, n, k, i + 1) + self.combineRecur(current + [i], n, k - 1, i + 1) |
16 changes: 16 additions & 0 deletions
16
Construct Binary Tree from Inorder and Postorder Traversal.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
class Solution: | ||
def buildTree(self, inorder, postorder): | ||
# using dictionary for index lookup improves the performance of algorithm from O(N^2) to O(N), where N = |postorder| | ||
lookup = {} | ||
for i in range(len(inorder)): | ||
lookup[inorder[i]] = i | ||
return self.buildTreeRecur(lookup, inorder, postorder, 0, len(postorder) - 1, len(postorder) - 1) | ||
|
||
def buildTreeRecur(self, lookup, inorder, postorder, in_start, in_end, post_end): | ||
if in_start > in_end: | ||
return None | ||
current = TreeNode(postorder[post_end]) | ||
i = lookup[postorder[post_end]] | ||
current.left = self.buildTreeRecur(lookup, inorder, postorder, in_start, i - 1, post_end - (in_end - i) - 1) | ||
current.right = self.buildTreeRecur(lookup, inorder, postorder, i + 1, in_end, post_end - 1) | ||
return current |
16 changes: 16 additions & 0 deletions
16
Construct Binary Tree from Preorder and Inorder Traversal.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
class Solution: | ||
def buildTree(self, preorder, inorder): | ||
# using dictionary for index lookup improves the performance of algorithm from O(N^2) to O(N), where N = |preorder| | ||
lookup = {} | ||
for i in range(len(inorder)): | ||
lookup[inorder[i]] = i | ||
return self.buildTreeRecur(lookup, preorder, inorder, 0, len(preorder) - 1, 0) | ||
|
||
def buildTreeRecur(self, lookup, preorder, inorder, in_start, in_end, pre_start): | ||
if in_start > in_end: | ||
return None | ||
current = TreeNode(preorder[pre_start]) | ||
i = lookup[preorder[pre_start]] | ||
current.left = self.buildTreeRecur(lookup, preorder, inorder, in_start, i - 1, pre_start + 1) | ||
current.right = self.buildTreeRecur(lookup, preorder, inorder, i + 1, in_end, pre_start + i - in_start + 1) | ||
return current |
Oops, something went wrong.