Skip to content

Commit cc2703d

Browse files
committed
update
1 parent 25048ba commit cc2703d

File tree

166 files changed

+0
-1049
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

166 files changed

+0
-1049
lines changed

Python/candy.py

-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from __future__ import print_function
21
# Time: O(n)
32
# Space: O(n)
43

@@ -20,8 +19,3 @@ def candy(self, ratings):
2019

2120
return reduce(operator.add, candies)
2221

23-
if __name__ == "__main__":
24-
result = Solution().candy([1, 2, 3, 2, 3, 5, 2, 5])
25-
print(result)
26-
27-

Python/climbing-stairs.py

-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from __future__ import print_function
21
# Time: O(n)
32
# Space: O(1)
43

@@ -22,7 +21,3 @@ def climbStairs1(self, n):
2221
return 2
2322
return self.climbStairs(n - 1) + self.climbStairs(n - 2)
2423

25-
if __name__ == "__main__":
26-
result = Solution().climbStairs(2)
27-
print(result)
28-

Python/combination-sum-ii.py

-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from __future__ import print_function
21
# Time: O(k * C(n, k))
32
# Space: O(k)
43

@@ -23,8 +22,3 @@ def combinationSumRecu(self, candidates, result, start, intermediate, target):
2322
prev = candidates[start]
2423
start += 1
2524

26-
if __name__ == "__main__":
27-
candidates, target = [10, 1, 2, 7, 6, 1, 5], 8
28-
result = Solution().combinationSum2(candidates, target)
29-
print(result)
30-

Python/combination-sum.py

-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from __future__ import print_function
21
# Time: O(k * n^k)
32
# Space: O(k)
43

@@ -20,8 +19,3 @@ def combinationSumRecu(self, candidates, result, start, intermediate, target):
2019
intermediate.pop()
2120
start += 1
2221

23-
if __name__ == "__main__":
24-
candidates, target = [2, 3, 6, 7], 7
25-
result = Solution().combinationSum(candidates, target)
26-
print(result)
27-

Python/combinations.py

-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from __future__ import print_function
21
# Time: O(k * C(n, k))
32
# Space: O(k)
43

@@ -46,7 +45,3 @@ def combineDFS(n, start, intermediate, k, result):
4645
return result
4746

4847

49-
if __name__ == "__main__":
50-
result = Solution().combine(4, 2)
51-
print(result)
52-

Python/compare-version-numbers.py

-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from __future__ import print_function
21
# Time: O(n)
32
# Space: O(1)
43

@@ -83,8 +82,3 @@ def compareVersion4(self, version1, version2):
8382
return cmp(int(main1), int(main2)) or len(rest1 + rest2) and self.compareVersion4(rest1, rest2)
8483

8584

86-
if __name__ == "__main__":
87-
print(Solution().compareVersion("21.0", "121.1.0"))
88-
print(Solution().compareVersion("01", "1"))
89-
print(Solution().compareVersion("1", "1.0"))
90-

Python/construct-binary-tree-from-inorder-and-postorder-traversal.py

-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from __future__ import print_function
21
# Time: O(n)
32
# Space: O(n)
43

@@ -27,11 +26,3 @@ def buildTreeRecu(self, lookup, postorder, inorder, post_end, in_start, in_end):
2726
node.right = self.buildTreeRecu(lookup, postorder, inorder, post_end - 1, i + 1, in_end)
2827
return node
2928

30-
if __name__ == "__main__":
31-
inorder = [2, 1, 3]
32-
postorder = [2, 3, 1]
33-
result = Solution().buildTree(inorder, postorder)
34-
print(result.val)
35-
print(result.left.val)
36-
print(result.right.val)
37-

Python/construct-binary-tree-from-preorder-and-inorder-traversal.py

-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from __future__ import print_function
21
# Time: O(n)
32
# Space: O(n)
43

@@ -27,11 +26,3 @@ def buildTreeRecu(self, lookup, preorder, inorder, pre_start, in_start, in_end):
2726
node.right = self.buildTreeRecu(lookup, preorder, inorder, pre_start + 1 + i - in_start, i + 1, in_end)
2827
return node
2928

30-
if __name__ == "__main__":
31-
preorder = [1, 2, 3]
32-
inorder = [2, 1, 3]
33-
result = Solution().buildTree(preorder, inorder)
34-
print(result.val)
35-
print(result.left.val)
36-
print(result.right.val)
37-

Python/container-with-most-water.py

-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from __future__ import print_function
21
# Time: O(n)
32
# Space: O(1)
43

@@ -14,8 +13,3 @@ def maxArea(self, height):
1413
j -= 1
1514
return max_area
1615

17-
if __name__ == "__main__":
18-
height = [1, 2, 3, 4, 3, 2, 1, 5]
19-
result = Solution().maxArea(height)
20-
print(result)
21-

Python/convert-sorted-array-to-binary-search-tree.py

-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from __future__ import print_function
21
# Time: O(n)
32
# Space: O(logn)
43

@@ -43,10 +42,3 @@ def perfect_tree_pivot(self, n):
4342
# has more nodes and the right subtree is perfect.
4443

4544

46-
if __name__ == "__main__":
47-
num = [1, 2, 3]
48-
result = Solution().sortedArrayToBST(num)
49-
print(result.val)
50-
print(result.left.val)
51-
print(result.right.val)
52-

Python/convert-sorted-list-to-binary-search-tree.py

-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from __future__ import print_function
21
# Time: O(n)
32
# Space: O(logn)
43

@@ -36,12 +35,3 @@ def sortedListToBSTRecu(self, start, end):
3635
current.right = self.sortedListToBSTRecu(mid + 1, end)
3736
return current
3837

39-
if __name__ == "__main__":
40-
head = ListNode(1)
41-
head.next = ListNode(2)
42-
head.next.next = ListNode(3)
43-
result = Solution().sortedListToBST(head)
44-
print(result.val)
45-
print(result.left.val)
46-
print(result.right.val)
47-

Python/copy-list-with-random-pointer.py

-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from __future__ import print_function
21
# Time: O(n)
32
# Space: O(1)
43

@@ -60,12 +59,3 @@ def copyRandomList(self, head):
6059
return dummy.next
6160

6261

63-
if __name__ == "__main__":
64-
head = RandomListNode(1)
65-
head.next = RandomListNode(2)
66-
head.random = head.next
67-
result = Solution().copyRandomList(head)
68-
print(result.label)
69-
print(result.next.label)
70-
print(result.random.label)
71-

Python/count-and-say.py

-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from __future__ import print_function
21
# Time: O(n * 2^n)
32
# Space: O(2^n)
43

@@ -21,9 +20,3 @@ def getNext(self, seq):
2120
i += 1
2221
return next_seq
2322

24-
if __name__ == "__main__":
25-
for i in xrange(1, 4):
26-
print(Solution().countAndSay(i))
27-
28-
29-

Python/counting-bits.py

-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from __future__ import print_function
21
# Time: O(n)
32
# Space: O(n)
43

@@ -25,8 +24,3 @@ def countBits2(self, num):
2524
return s[:num + 1]
2625

2726

28-
if __name__ == '__main__':
29-
s = Solution()
30-
r = s.countBits2(5)
31-
print(r)
32-

Python/course-schedule.py

-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from __future__ import print_function
21
# Time: O(|V| + |E|)
32
# Space: O(|E|)
43

@@ -42,6 +41,3 @@ def canFinish(self, numCourses, prerequisites):
4241
return True
4342

4443

45-
if __name__ == "__main__":
46-
print(Solution().canFinish(1, []))
47-

Python/decode-ways.py

-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from __future__ import print_function
21
# Time: O(n)
32
# Space: O(1)
43

@@ -21,7 +20,3 @@ def numDecodings(self, s):
2120
return prev
2221

2322

24-
if __name__ == "__main__":
25-
for i in ["0", "10", "10", "103", "1032", "10323"]:
26-
print(Solution().numDecodings(i))
27-

Python/distinct-subsequences.py

-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from __future__ import print_function
21
# Time: O(n^2)
32
# Space: O(n)
43

@@ -13,10 +12,3 @@ def numDistinct(self, S, T):
1312
ways[j + 1] += ways[j]
1413
return ways[len(T)]
1514

16-
if __name__ == "__main__":
17-
S = "rabbbit"
18-
T = "rabbit"
19-
result = Solution().numDistinct(S, T)
20-
print(result)
21-
22-

Python/divide-two-integers.py

-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from __future__ import print_function
21
# Time: O(logn) = O(1)
32
# Space: O(1)
43

@@ -43,9 +42,3 @@ def divide2(self, dividend, divisor):
4342
res = -res
4443
return min(max(-2147483648, res), 2147483647)
4544

46-
if __name__ == "__main__":
47-
print(Solution().divide(123, 12))
48-
print(Solution().divide(123, -12))
49-
print(Solution().divide(-123, 12))
50-
print(Solution().divide(-123, -12))
51-

Python/dungeon-game.py

-13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from __future__ import print_function
21
# Time: O(m * n)
32
# Space: O(m + n)
43

@@ -65,15 +64,3 @@ def DP(self, dungeon, HP):
6564

6665
return remain_HP[-1] > 0
6766

68-
if __name__ == "__main__":
69-
dungeon = [[ -2, -3, 3], \
70-
[ -5, -10, 1], \
71-
[ 10, 30, -5]]
72-
print(Solution().calculateMinimumHP(dungeon))
73-
74-
dungeon = [[ -200]]
75-
print(Solution().calculateMinimumHP(dungeon))
76-
77-
dungeon = [[0, -3]]
78-
print(Solution().calculateMinimumHP(dungeon))
79-

Python/edit-distance.py

-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from __future__ import print_function
21
# Time: O(n * m)
32
# Space: O(n + m)
43

@@ -43,8 +42,3 @@ def minDistance(self, word1, word2):
4342

4443
return distance[-1][-1]
4544

46-
if __name__ == "__main__":
47-
print(Solution().minDistance("Rabbit", "Racket"))
48-
print(Solution2().minDistance("Rabbit", "Rabket"))
49-
print(Solution().minDistance("Rabbit", "Rabbitt"))
50-

Python/evaluate-reverse-polish-notation.py

-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from __future__ import print_function
21
# Time: O(n)
32
# Space: O(n)
43

@@ -17,8 +16,3 @@ def evalRPN(self, tokens):
1716
numerals.append(int(operators[token](x * 1.0, y)))
1817
return numerals.pop()
1918

20-
if __name__ == "__main__":
21-
print(Solution().evalRPN(["2", "1", "+", "3", "*"]))
22-
print(Solution().evalRPN(["4", "13", "5", "/", "+"]))
23-
print(Solution().evalRPN(["10","6","9","3","+","-11","*","/","*","17","+","5","+"]))
24-

Python/excel-sheet-column-number.py

-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from __future__ import print_function
21
# Time: O(n)
32
# Space: O(1)
43

@@ -15,6 +14,3 @@ def titleToNumber(self, s):
1514
return result
1615

1716

18-
if __name__ == "__main__":
19-
print(Solution().titleToNumber("AAAB"))
20-

Python/excel-sheet-column-title.py

-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from __future__ import print_function
21
# Time: O(logn)
32
# Space: O(1)
43

@@ -17,7 +16,3 @@ def convertToTitle(self, n):
1716
return result[::-1]
1817

1918

20-
if __name__ == "__main__":
21-
for i in xrange(1, 29):
22-
print(Solution().convertToTitle(i))
23-

Python/factorial-trailing-zeroes.py

-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from __future__ import print_function
21
# Time: O(logn) = O(1)
32
# Space: O(1)
43

@@ -11,6 +10,3 @@ def trailingZeroes(self, n):
1110
n /= 5
1211
return result
1312

14-
if __name__ == "__main__":
15-
print(Solution().trailingZeroes(100))
16-

Python/find-all-numbers-disappeared-in-an-array.py

-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from __future__ import print_function
21
# Time: O(n)
32
# Space: O(1)
43

@@ -35,8 +34,3 @@ def findDisappearedNumbers3(self, nums):
3534
return [i + 1 for i in range(len(nums)) if nums[i] > 0]
3635

3736

38-
if __name__ == '__main__':
39-
s = Solution()
40-
r = s.findDisappearedNumbers([4, 3, 2, 7, 8, 2, 3, 1])
41-
print(r)
42-

Python/find-minimum-in-rotated-sorted-array-ii.py

-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from __future__ import print_function
21
# Time: O(logn) ~ O(n)
32
# Space: O(1)
43

@@ -42,7 +41,3 @@ def findMin(self, nums):
4241
return nums[left]
4342

4443

45-
if __name__ == "__main__":
46-
print(Solution().findMin([3, 1, 1, 2, 2, 3]))
47-
print(Solution2().findMin([2, 2, 2, 3, 3, 1]))
48-

Python/find-minimum-in-rotated-sorted-array.py

-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from __future__ import print_function
21
# Time: O(logn)
32
# Space: O(1)
43

@@ -40,10 +39,3 @@ def findMin(self, nums):
4039
return nums[left]
4140

4241

43-
if __name__ == "__main__":
44-
print(Solution().findMin([1]))
45-
print(Solution().findMin([1, 2]))
46-
print(Solution().findMin([2, 1]))
47-
print(Solution().findMin([3, 1, 2]))
48-
print(Solution().findMin([2, 3, 1]))
49-

0 commit comments

Comments
 (0)