diff --git a/Python/candy.py b/Python/candy.py index eded6772a..c2c0c2e08 100644 --- a/Python/candy.py +++ b/Python/candy.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(n) # @@ -11,6 +12,7 @@ # import operator +from functools import reduce class Solution: # @param ratings, a list of integer @@ -29,5 +31,5 @@ def candy(self, ratings): if __name__ == "__main__": result = Solution().candy([1, 2, 3, 2, 3, 5, 2, 5]) - print result + print(result) diff --git a/Python/climbing-stairs.py b/Python/climbing-stairs.py index a6ccff427..5ef865236 100644 --- a/Python/climbing-stairs.py +++ b/Python/climbing-stairs.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(1) # @@ -29,4 +30,4 @@ def climbStairs1(self, n): if __name__ == "__main__": result = Solution().climbStairs(2) - print result + print(result) diff --git a/Python/combination-sum-ii.py b/Python/combination-sum-ii.py index 31975069d..fabfdf352 100644 --- a/Python/combination-sum-ii.py +++ b/Python/combination-sum-ii.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(k * C(n, k)) # Space: O(k) # @@ -42,4 +43,4 @@ def combinationSumRecu(self, candidates, result, start, intermediate, target): if __name__ == "__main__": candidates, target = [10, 1, 2, 7, 6, 1, 5], 8 result = Solution().combinationSum2(candidates, target) - print result + print(result) diff --git a/Python/combination-sum.py b/Python/combination-sum.py index 0120a7cdb..fdb9a1c74 100644 --- a/Python/combination-sum.py +++ b/Python/combination-sum.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(k * n^k) # Space: O(k) # @@ -37,4 +38,4 @@ def combinationSumRecu(self, candidates, result, start, intermediate, target): if __name__ == "__main__": candidates, target = [2, 3, 6, 7], 7 result = Solution().combinationSum(candidates, target) - print result + print(result) diff --git a/Python/combinations.py b/Python/combinations.py index e57640ab1..9513ec413 100644 --- a/Python/combinations.py +++ b/Python/combinations.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(k * C(n, k)) # Space: O(k) @@ -61,4 +62,4 @@ def combineDFS(n, start, intermediate, k, result): if __name__ == "__main__": result = Solution().combine(4, 2) - print result + print(result) diff --git a/Python/compare-version-numbers.py b/Python/compare-version-numbers.py index 342dff213..2cc7ade76 100644 --- a/Python/compare-version-numbers.py +++ b/Python/compare-version-numbers.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(1) @@ -99,6 +100,6 @@ def compareVersion4(self, version1, version2): if __name__ == "__main__": - print Solution().compareVersion("21.0", "121.1.0") - print Solution().compareVersion("01", "1") - print Solution().compareVersion("1", "1.0") + print(Solution().compareVersion("21.0", "121.1.0")) + print(Solution().compareVersion("01", "1")) + print(Solution().compareVersion("1", "1.0")) diff --git a/Python/construct-binary-tree-from-inorder-and-postorder-traversal.py b/Python/construct-binary-tree-from-inorder-and-postorder-traversal.py index 294594fa5..c09e28245 100644 --- a/Python/construct-binary-tree-from-inorder-and-postorder-traversal.py +++ b/Python/construct-binary-tree-from-inorder-and-postorder-traversal.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(n) # @@ -37,6 +38,6 @@ def buildTreeRecu(self, lookup, postorder, inorder, post_end, in_start, in_end): inorder = [2, 1, 3] postorder = [2, 3, 1] result = Solution().buildTree(inorder, postorder) - print result.val - print result.left.val - print result.right.val \ No newline at end of file + print(result.val) + print(result.left.val) + print(result.right.val) \ No newline at end of file diff --git a/Python/construct-binary-tree-from-preorder-and-inorder-traversal.py b/Python/construct-binary-tree-from-preorder-and-inorder-traversal.py index a316ecdd2..3da09b572 100644 --- a/Python/construct-binary-tree-from-preorder-and-inorder-traversal.py +++ b/Python/construct-binary-tree-from-preorder-and-inorder-traversal.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(n) # @@ -37,6 +38,6 @@ def buildTreeRecu(self, lookup, preorder, inorder, pre_start, in_start, in_end): preorder = [1, 2, 3] inorder = [2, 1, 3] result = Solution().buildTree(preorder, inorder) - print result.val - print result.left.val - print result.right.val + print(result.val) + print(result.left.val) + print(result.right.val) diff --git a/Python/container-with-most-water.py b/Python/container-with-most-water.py index 5522e0080..6c8016d76 100644 --- a/Python/container-with-most-water.py +++ b/Python/container-with-most-water.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(1) # @@ -26,4 +27,4 @@ def maxArea(self, height): if __name__ == "__main__": height = [1, 2, 3, 4, 3, 2, 1, 5] result = Solution().maxArea(height) - print result \ No newline at end of file + print(result) \ No newline at end of file diff --git a/Python/convert-sorted-array-to-binary-search-tree.py b/Python/convert-sorted-array-to-binary-search-tree.py index ac3789fb5..57d9165bf 100644 --- a/Python/convert-sorted-array-to-binary-search-tree.py +++ b/Python/convert-sorted-array-to-binary-search-tree.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(logn) # @@ -49,6 +50,6 @@ def perfect_tree_pivot(self, n): if __name__ == "__main__": num = [1, 2, 3] result = Solution().sortedArrayToBST(num) - print result.val - print result.left.val - print result.right.val + print(result.val) + print(result.left.val) + print(result.right.val) diff --git a/Python/convert-sorted-list-to-binary-search-tree.py b/Python/convert-sorted-list-to-binary-search-tree.py index a1e2a5166..4875bcba4 100644 --- a/Python/convert-sorted-list-to-binary-search-tree.py +++ b/Python/convert-sorted-list-to-binary-search-tree.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(logn) # @@ -44,6 +45,6 @@ def sortedListToBSTRecu(self, start, end): head.next = ListNode(2) head.next.next = ListNode(3) result = Solution().sortedListToBST(head) - print result.val - print result.left.val - print result.right.val \ No newline at end of file + print(result.val) + print(result.left.val) + print(result.right.val) \ No newline at end of file diff --git a/Python/copy-list-with-random-pointer.py b/Python/copy-list-with-random-pointer.py index 483aa2a3e..695b144c5 100644 --- a/Python/copy-list-with-random-pointer.py +++ b/Python/copy-list-with-random-pointer.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(1) # @@ -71,6 +72,6 @@ def copyRandomList(self, head): head.next = RandomListNode(2) head.random = head.next result = Solution().copyRandomList(head) - print result.label - print result.next.label - print result.random.label \ No newline at end of file + print(result.label) + print(result.next.label) + print(result.random.label) \ No newline at end of file diff --git a/Python/count-and-say.py b/Python/count-and-say.py index 2bbfc3a4e..5b7d04825 100644 --- a/Python/count-and-say.py +++ b/Python/count-and-say.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n * 2^n) # Space: O(2^n) # @@ -33,6 +34,6 @@ def getNext(self, seq): if __name__ == "__main__": for i in xrange(1, 4): - print Solution().countAndSay(i) + print(Solution().countAndSay(i)) diff --git a/Python/counting-bits.py b/Python/counting-bits.py index 8da8a2185..e3829c7e6 100644 --- a/Python/counting-bits.py +++ b/Python/counting-bits.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(n) @@ -52,4 +53,4 @@ def countBits2(self, num): if __name__ == '__main__': s = Solution() r = s.countBits2(5) - print r + print(r) diff --git a/Python/course-schedule-iii.py b/Python/course-schedule-iii.py index 605f9cc19..7ad66c09e 100644 --- a/Python/course-schedule-iii.py +++ b/Python/course-schedule-iii.py @@ -35,7 +35,7 @@ def scheduleCourse(self, courses): :type courses: List[List[int]] :rtype: int """ - courses.sort(key=lambda(t, end): end) + courses.sort(key=lambda t_end: t_end[1]) max_heap = [] now = 0 for t, end in courses: diff --git a/Python/course-schedule.py b/Python/course-schedule.py index ddd5fc3de..73711a1db 100644 --- a/Python/course-schedule.py +++ b/Python/course-schedule.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(|V| + |E|) # Space: O(|E|) # @@ -72,4 +73,4 @@ def canFinish(self, numCourses, prerequisites): if __name__ == "__main__": - print Solution().canFinish(1, []) + print(Solution().canFinish(1, [])) diff --git a/Python/decode-ways.py b/Python/decode-ways.py index 7d60cfa09..d5c3b9be4 100644 --- a/Python/decode-ways.py +++ b/Python/decode-ways.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(1) # @@ -36,4 +37,4 @@ def numDecodings(self, s): if __name__ == "__main__": for i in ["0", "10", "10", "103", "1032", "10323"]: - print Solution().numDecodings(i) + print(Solution().numDecodings(i)) diff --git a/Python/distinct-subsequences.py b/Python/distinct-subsequences.py index 5d4aad7bc..747aeee53 100644 --- a/Python/distinct-subsequences.py +++ b/Python/distinct-subsequences.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n^2) # Space: O(n) # @@ -28,5 +29,5 @@ def numDistinct(self, S, T): S = "rabbbit" T = "rabbit" result = Solution().numDistinct(S, T) - print result + print(result) diff --git a/Python/divide-two-integers.py b/Python/divide-two-integers.py index 9e2bd8193..b5f7341e9 100644 --- a/Python/divide-two-integers.py +++ b/Python/divide-two-integers.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(logn) = O(1) # Space: O(1) # @@ -46,7 +47,7 @@ def divide2(self, dividend, divisor): return min(max(-2147483648, res), 2147483647) if __name__ == "__main__": - print Solution().divide(123, 12) - print Solution().divide(123, -12) - print Solution().divide(-123, 12) - print Solution().divide(-123, -12) + print(Solution().divide(123, 12)) + print(Solution().divide(123, -12)) + print(Solution().divide(-123, 12)) + print(Solution().divide(-123, -12)) diff --git a/Python/dungeon-game.py b/Python/dungeon-game.py index d42e5d1a3..b02b08ecc 100644 --- a/Python/dungeon-game.py +++ b/Python/dungeon-game.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(m * n) # Space: O(m + n) # @@ -98,10 +99,10 @@ def DP(self, dungeon, HP): dungeon = [[ -2, -3, 3], \ [ -5, -10, 1], \ [ 10, 30, -5]] - print Solution().calculateMinimumHP(dungeon) + print(Solution().calculateMinimumHP(dungeon)) dungeon = [[ -200]] - print Solution().calculateMinimumHP(dungeon) + print(Solution().calculateMinimumHP(dungeon)) dungeon = [[0, -3]] - print Solution().calculateMinimumHP(dungeon) \ No newline at end of file + print(Solution().calculateMinimumHP(dungeon)) \ No newline at end of file diff --git a/Python/edit-distance.py b/Python/edit-distance.py index 2ca1e67c4..f1daec88e 100644 --- a/Python/edit-distance.py +++ b/Python/edit-distance.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n * m) # Space: O(n + m) # @@ -53,6 +54,6 @@ def minDistance(self, word1, word2): return distance[-1][-1] if __name__ == "__main__": - print Solution().minDistance("Rabbit", "Racket") - print Solution2().minDistance("Rabbit", "Rabket") - print Solution().minDistance("Rabbit", "Rabbitt") + print(Solution().minDistance("Rabbit", "Racket")) + print(Solution2().minDistance("Rabbit", "Rabket")) + print(Solution().minDistance("Rabbit", "Rabbitt")) diff --git a/Python/evaluate-reverse-polish-notation.py b/Python/evaluate-reverse-polish-notation.py index b64bad971..50606d757 100644 --- a/Python/evaluate-reverse-polish-notation.py +++ b/Python/evaluate-reverse-polish-notation.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(n) # @@ -25,6 +26,6 @@ def evalRPN(self, tokens): return numerals.pop() if __name__ == "__main__": - print Solution().evalRPN(["2", "1", "+", "3", "*"]) - print Solution().evalRPN(["4", "13", "5", "/", "+"]) - print Solution().evalRPN(["10","6","9","3","+","-11","*","/","*","17","+","5","+"]) \ No newline at end of file + print(Solution().evalRPN(["2", "1", "+", "3", "*"])) + print(Solution().evalRPN(["4", "13", "5", "/", "+"])) + print(Solution().evalRPN(["10","6","9","3","+","-11","*","/","*","17","+","5","+"])) \ No newline at end of file diff --git a/Python/excel-sheet-column-number.py b/Python/excel-sheet-column-number.py index ad16cb326..ceac0cb02 100644 --- a/Python/excel-sheet-column-number.py +++ b/Python/excel-sheet-column-number.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(1) @@ -29,4 +30,4 @@ def titleToNumber(self, s): if __name__ == "__main__": - print Solution().titleToNumber("AAAB") + print(Solution().titleToNumber("AAAB")) diff --git a/Python/excel-sheet-column-title.py b/Python/excel-sheet-column-title.py index 041031253..0b833b155 100644 --- a/Python/excel-sheet-column-title.py +++ b/Python/excel-sheet-column-title.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(logn) # Space: O(1) @@ -30,4 +31,4 @@ def convertToTitle(self, n): if __name__ == "__main__": for i in xrange(1, 29): - print Solution().convertToTitle(i) + print(Solution().convertToTitle(i)) diff --git a/Python/factorial-trailing-zeroes.py b/Python/factorial-trailing-zeroes.py index bccfd7e75..f071fd710 100644 --- a/Python/factorial-trailing-zeroes.py +++ b/Python/factorial-trailing-zeroes.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(logn) = O(1) # Space: O(1) # @@ -16,4 +17,4 @@ def trailingZeroes(self, n): return result if __name__ == "__main__": - print Solution().trailingZeroes(100) + print(Solution().trailingZeroes(100)) diff --git a/Python/find-all-numbers-disappeared-in-an-array.py b/Python/find-all-numbers-disappeared-in-an-array.py index cde02ffa0..6461968c6 100644 --- a/Python/find-all-numbers-disappeared-in-an-array.py +++ b/Python/find-all-numbers-disappeared-in-an-array.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(1) @@ -54,4 +55,4 @@ def findDisappearedNumbers3(self, nums): if __name__ == '__main__': s = Solution() r = s.findDisappearedNumbers([4, 3, 2, 7, 8, 2, 3, 1]) - print r + print(r) diff --git a/Python/find-minimum-in-rotated-sorted-array-ii.py b/Python/find-minimum-in-rotated-sorted-array-ii.py index 911eacbb7..7ca773143 100644 --- a/Python/find-minimum-in-rotated-sorted-array-ii.py +++ b/Python/find-minimum-in-rotated-sorted-array-ii.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(logn) ~ O(n) # Space: O(1) # @@ -55,5 +56,5 @@ def findMin(self, nums): if __name__ == "__main__": - print Solution().findMin([3, 1, 1, 2, 2, 3]) - print Solution2().findMin([2, 2, 2, 3, 3, 1]) + print(Solution().findMin([3, 1, 1, 2, 2, 3])) + print(Solution2().findMin([2, 2, 2, 3, 3, 1])) diff --git a/Python/find-minimum-in-rotated-sorted-array.py b/Python/find-minimum-in-rotated-sorted-array.py index 87bf741e8..e850ea1cf 100644 --- a/Python/find-minimum-in-rotated-sorted-array.py +++ b/Python/find-minimum-in-rotated-sorted-array.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(logn) # Space: O(1) # @@ -49,8 +50,8 @@ def findMin(self, nums): if __name__ == "__main__": - print Solution().findMin([1]) - print Solution().findMin([1, 2]) - print Solution().findMin([2, 1]) - print Solution().findMin([3, 1, 2]) - print Solution().findMin([2, 3, 1]) + print(Solution().findMin([1])) + print(Solution().findMin([1, 2])) + print(Solution().findMin([2, 1])) + print(Solution().findMin([3, 1, 2])) + print(Solution().findMin([2, 3, 1])) diff --git a/Python/find-peak-element.py b/Python/find-peak-element.py index b986c43bf..4d6569ac5 100644 --- a/Python/find-peak-element.py +++ b/Python/find-peak-element.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(logn) # Space: O(1) @@ -38,4 +39,4 @@ def findPeakElement(self, nums): if __name__ == "__main__": # print Solution().findPeakElement([1,2,1]) - print Solution().findPeakElement([1,2,3,1]) + print(Solution().findPeakElement([1,2,3,1])) diff --git a/Python/find-the-difference.py b/Python/find-the-difference.py index d661e7967..691f63a9e 100644 --- a/Python/find-the-difference.py +++ b/Python/find-the-difference.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(1) @@ -22,6 +23,7 @@ import operator import collections +from functools import reduce class Solution(object): @@ -59,4 +61,4 @@ def findTheDifference5(self, s, t): if __name__ == '__main__': s = Solution() r = s.findTheDifference2('abcd', 'abcde') - print r + print(r) diff --git a/Python/first-missing-positive.py b/Python/first-missing-positive.py index eb09936d4..0fec5e20d 100644 --- a/Python/first-missing-positive.py +++ b/Python/first-missing-positive.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(1) # @@ -27,5 +28,5 @@ def firstMissingPositive(self, A): return len(A) + 1 if __name__ == "__main__": - print Solution().firstMissingPositive([1,2,0]) - print Solution().firstMissingPositive([3,4,-1,1]) + print(Solution().firstMissingPositive([1,2,0])) + print(Solution().firstMissingPositive([3,4,-1,1])) diff --git a/Python/fizz-buzz.py b/Python/fizz-buzz.py index 0e46b90e1..9c56e8524 100644 --- a/Python/fizz-buzz.py +++ b/Python/fizz-buzz.py @@ -71,4 +71,4 @@ def fizzBuzz3(self, n): return ['Fizz' * (not i % 3) + 'Buzz' * (not i % 5) or str(i) for i in range(1, n + 1)] def fizzBuzz4(self, n): - return ['FizzBuzz'[i % -3 & -4:i % -5 & 8 ^ 12] or `i` for i in range(1, n + 1)] + return ['FizzBuzz'[i % -3 & -4:i % -5 & 8 ^ 12] or repr(i) for i in range(1, n + 1)] diff --git a/Python/flatten-binary-tree-to-linked-list.py b/Python/flatten-binary-tree-to-linked-list.py index 581275cb0..92e726125 100644 --- a/Python/flatten-binary-tree-to-linked-list.py +++ b/Python/flatten-binary-tree-to-linked-list.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(h), h is height of binary tree # @@ -69,9 +70,9 @@ def flatten(self, root): root.right = TreeNode(5) root.right.right = TreeNode(6) result = Solution().flatten(root) - print result.val - print result.right.val - print result.right.right.val - print result.right.right.right.val - print result.right.right.right.right.val - print result.right.right.right.right.right.val + print(result.val) + print(result.right.val) + print(result.right.right.val) + print(result.right.right.right.val) + print(result.right.right.right.right.val) + print(result.right.right.right.right.right.val) diff --git a/Python/fraction-to-recurring-decimal.py b/Python/fraction-to-recurring-decimal.py index 7fed92fb2..02b14c678 100644 --- a/Python/fraction-to-recurring-decimal.py +++ b/Python/fraction-to-recurring-decimal.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(logn), where logn is the length of result strings # Space: O(1) @@ -44,7 +45,7 @@ def fractionToDecimal(self, numerator, denominator): if __name__ == "__main__": - print Solution().fractionToDecimal(1, 9) - print Solution().fractionToDecimal(-50, 8) - print Solution().fractionToDecimal(22, 2) - print Solution().fractionToDecimal(-22, -2) + print(Solution().fractionToDecimal(1, 9)) + print(Solution().fractionToDecimal(-50, 8)) + print(Solution().fractionToDecimal(22, 2)) + print(Solution().fractionToDecimal(-22, -2)) diff --git a/Python/gas-station.py b/Python/gas-station.py index 30a6fe5cc..45413aa93 100644 --- a/Python/gas-station.py +++ b/Python/gas-station.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(1) # @@ -31,7 +32,7 @@ def canCompleteCircuit(self, gas, cost): return -1 if __name__ == "__main__": - print Solution().canCompleteCircuit([1, 2, 3], [3, 2, 1]) - print Solution().canCompleteCircuit([1, 2, 3], [2, 2, 2]) - print Solution().canCompleteCircuit([1, 2, 3], [1, 2, 3]) - print Solution().canCompleteCircuit([1, 2, 3], [1, 2, 4]) + print(Solution().canCompleteCircuit([1, 2, 3], [3, 2, 1])) + print(Solution().canCompleteCircuit([1, 2, 3], [2, 2, 2])) + print(Solution().canCompleteCircuit([1, 2, 3], [1, 2, 3])) + print(Solution().canCompleteCircuit([1, 2, 3], [1, 2, 4])) diff --git a/Python/generate-parentheses.py b/Python/generate-parentheses.py index 2ffcfc194..113f59077 100644 --- a/Python/generate-parentheses.py +++ b/Python/generate-parentheses.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(4^n / n^(3/2)) ~= Catalan numbers # Space: O(n) # @@ -26,4 +27,4 @@ def generateParenthesisRecu(self, result, current, left, right): self.generateParenthesisRecu(result, current + ")", left, right - 1) if __name__ == "__main__": - print Solution().generateParenthesis(3) \ No newline at end of file + print(Solution().generateParenthesis(3)) \ No newline at end of file diff --git a/Python/gray-code.py b/Python/gray-code.py index f54b9b421..8cd9f0411 100644 --- a/Python/gray-code.py +++ b/Python/gray-code.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(2^n) # Space: O(1) @@ -46,5 +47,5 @@ def grayCode(self, n): if __name__ == "__main__": - print Solution().grayCode(0) - print Solution().grayCode(2) + print(Solution().grayCode(0)) + print(Solution().grayCode(2)) diff --git a/Python/group-anagrams.py b/Python/group-anagrams.py index f1a51e789..ade7079f8 100644 --- a/Python/group-anagrams.py +++ b/Python/group-anagrams.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n * glogg), g is the max size of groups. # Space: O(n) # @@ -27,4 +28,4 @@ def groupAnagrams(self, strs): if __name__ == "__main__": result = Solution().groupAnagrams(["cat", "dog", "act", "mac"]) - print result + print(result) diff --git a/Python/house-robber-ii.py b/Python/house-robber-ii.py index 5f1b2fbff..fdbe57a29 100644 --- a/Python/house-robber-ii.py +++ b/Python/house-robber-ii.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(1) # @@ -34,4 +35,4 @@ def robRange(self, nums, start, end): return num_i if __name__ == '__main__': - print Solution().rob([8,4,8,5,9,6,5,4,4,10]) + print(Solution().rob([8,4,8,5,9,6,5,4,4,10])) diff --git a/Python/house-robber.py b/Python/house-robber.py index d94ae3b53..4dfec5567 100644 --- a/Python/house-robber.py +++ b/Python/house-robber.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(1) # @@ -38,4 +39,4 @@ def rob2(self, nums): if __name__ == '__main__': - print Solution().rob([8,4,8,5,9,6,5,4,4,10]) + print(Solution().rob([8,4,8,5,9,6,5,4,4,10])) diff --git a/Python/implement-strstr.py b/Python/implement-strstr.py index 1e7a5919a..b7f812d80 100644 --- a/Python/implement-strstr.py +++ b/Python/implement-strstr.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n + k) # Space: O(k) # @@ -61,5 +62,5 @@ def strStr(self, haystack, needle): if __name__ == "__main__": - print Solution().strStr("a", "") - print Solution().strStr("abababcdab", "ababcdx") + print(Solution().strStr("a", "")) + print(Solution().strStr("abababcdab", "ababcdx")) diff --git a/Python/insert-interval.py b/Python/insert-interval.py index 33dd83cec..695f19cdd 100644 --- a/Python/insert-interval.py +++ b/Python/insert-interval.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(1) @@ -45,4 +46,4 @@ def insert(self, intervals, newInterval): if __name__ == "__main__": - print Solution().insert([Interval(1, 2), Interval(3, 5), Interval(6, 7), Interval(8, 10), Interval(12, 16)], Interval(4, 9)) + print(Solution().insert([Interval(1, 2), Interval(3, 5), Interval(6, 7), Interval(8, 10), Interval(12, 16)], Interval(4, 9))) diff --git a/Python/insertion-sort-list.py b/Python/insertion-sort-list.py index 30a8a7500..d92f95881 100644 --- a/Python/insertion-sort-list.py +++ b/Python/insertion-sort-list.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n ^ 2) # Space: O(1) # @@ -49,4 +50,4 @@ def isSorted(self, head): head = ListNode(3) head.next = ListNode(2) head.next.next = ListNode(1) - print Solution().insertionSortList(head) + print(Solution().insertionSortList(head)) diff --git a/Python/integer-to-roman.py b/Python/integer-to-roman.py index 672201a80..1d4e591f9 100644 --- a/Python/integer-to-roman.py +++ b/Python/integer-to-roman.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(1) # @@ -28,5 +29,5 @@ def intToRoman(self, num): if __name__ == "__main__": - print Solution().intToRoman(999) - print Solution().intToRoman(3999) + print(Solution().intToRoman(999)) + print(Solution().intToRoman(3999)) diff --git a/Python/interleaving-string.py b/Python/interleaving-string.py index 8e0ff98c6..4c90b3bec 100644 --- a/Python/interleaving-string.py +++ b/Python/interleaving-string.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(m * n) # Space: O(m + n) # @@ -82,6 +83,6 @@ def isInterleaveRecu(self, s1, s2, s3, a, b, c): return result if __name__ == "__main__": - print Solution().isInterleave("a", "", "a") - print Solution().isInterleave("aabcc", "dbbca", "aadbbcbcac") - print Solution().isInterleave("aabcc", "dbbca", "aadbbbaccc") + print(Solution().isInterleave("a", "", "a")) + print(Solution().isInterleave("aabcc", "dbbca", "aadbbcbcac")) + print(Solution().isInterleave("aabcc", "dbbca", "aadbbbaccc")) diff --git a/Python/jump-game-ii.py b/Python/jump-game-ii.py index d99f97642..50c30b2dc 100644 --- a/Python/jump-game-ii.py +++ b/Python/jump-game-ii.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(1) # @@ -32,5 +33,5 @@ def jump(self, A): if __name__ == "__main__": - print Solution().jump([2,3,1,1,4]) - print Solution().jump([3,2,1,0,4]) + print(Solution().jump([2,3,1,1,4])) + print(Solution().jump([3,2,1,0,4])) diff --git a/Python/jump-game.py b/Python/jump-game.py index de25a28b3..8cd87f9c5 100644 --- a/Python/jump-game.py +++ b/Python/jump-game.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(1) # @@ -24,5 +25,5 @@ def canJump(self, A): return reachable >= len(A) - 1 if __name__ == "__main__": - print Solution().canJump([2,3,1,1,4]) - print Solution().canJump([3,2,1,0,4]) \ No newline at end of file + print(Solution().canJump([2,3,1,1,4])) + print(Solution().canJump([3,2,1,0,4])) \ No newline at end of file diff --git a/Python/largest-number.py b/Python/largest-number.py index 008d5e0e7..c1bd3c1df 100644 --- a/Python/largest-number.py +++ b/Python/largest-number.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(nlogn) # Space: O(1) # @@ -19,4 +20,4 @@ def largestNumber(self, num): if __name__ == "__main__": num = [3, 30, 34, 5, 9] - print Solution().largestNumber(num) + print(Solution().largestNumber(num)) diff --git a/Python/largest-rectangle-in-histogram.py b/Python/largest-rectangle-in-histogram.py index 3e4d6fa82..db40803f7 100644 --- a/Python/largest-rectangle-in-histogram.py +++ b/Python/largest-rectangle-in-histogram.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(n) # @@ -28,6 +29,6 @@ def largestRectangleArea(self, height): return area if __name__ == "__main__": - print Solution().largestRectangleArea([2, 0, 2]) - print Solution().largestRectangleArea([2, 1, 5, 6, 2, 3]) + print(Solution().largestRectangleArea([2, 0, 2])) + print(Solution().largestRectangleArea([2, 1, 5, 6, 2, 3])) diff --git a/Python/length-of-last-word.py b/Python/length-of-last-word.py index ec1c239e0..8630b312e 100644 --- a/Python/length-of-last-word.py +++ b/Python/length-of-last-word.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(1) # @@ -34,5 +35,5 @@ def lengthOfLastWord(self, s): return len(s.strip().split(" ")[-1]) if __name__ == "__main__": - print Solution().lengthOfLastWord("Hello World") - print Solution2().lengthOfLastWord("") + print(Solution().lengthOfLastWord("Hello World")) + print(Solution2().lengthOfLastWord("")) diff --git a/Python/letter-combinations-of-a-phone-number.py b/Python/letter-combinations-of-a-phone-number.py index ca3d1c347..bc85125db 100644 --- a/Python/letter-combinations-of-a-phone-number.py +++ b/Python/letter-combinations-of-a-phone-number.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n * 4^n) # Space: O(n) # @@ -55,4 +56,4 @@ def letterCombinationsRecu(self, result, digits, lookup, cur, n): self.letterCombinationsRecu(result, digits, lookup, cur + choice, n + 1) if __name__ == "__main__": - print Solution().letterCombinations("23") + print(Solution().letterCombinations("23")) diff --git a/Python/linked-list-cycle-ii.py b/Python/linked-list-cycle-ii.py index 281e465fc..3cfb64a41 100644 --- a/Python/linked-list-cycle-ii.py +++ b/Python/linked-list-cycle-ii.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(1) # @@ -38,4 +39,4 @@ def detectCycle(self, head): head.next = ListNode(2) head.next.next = ListNode(3) head.next.next.next = head.next - print Solution().detectCycle(head) \ No newline at end of file + print(Solution().detectCycle(head)) \ No newline at end of file diff --git a/Python/linked-list-cycle.py b/Python/linked-list-cycle.py index 6073a78be..8910df7c1 100644 --- a/Python/linked-list-cycle.py +++ b/Python/linked-list-cycle.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(1) # @@ -29,4 +30,4 @@ def hasCycle(self, head): head.next = ListNode(2) head.next.next = ListNode(3) head.next.next.next = head.next - print Solution().hasCycle(head) \ No newline at end of file + print(Solution().hasCycle(head)) \ No newline at end of file diff --git a/Python/longest-common-prefix.py b/Python/longest-common-prefix.py index f5a918469..a05860d10 100644 --- a/Python/longest-common-prefix.py +++ b/Python/longest-common-prefix.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n * k), k is the length of the common prefix # Space: O(1) @@ -21,4 +22,4 @@ def longestCommonPrefix(self, strs): if __name__ == "__main__": - print Solution().longestCommonPrefix(["hello", "heaven", "heavy"]) + print(Solution().longestCommonPrefix(["hello", "heaven", "heavy"])) diff --git a/Python/longest-consecutive-sequence.py b/Python/longest-consecutive-sequence.py index 240b109e0..b8d05a320 100644 --- a/Python/longest-consecutive-sequence.py +++ b/Python/longest-consecutive-sequence.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(n) # @@ -24,4 +25,4 @@ def longestConsecutive(self, num): return result if __name__ == "__main__": - print Solution().longestConsecutive([100, 4, 200, 1, 3, 2]) \ No newline at end of file + print(Solution().longestConsecutive([100, 4, 200, 1, 3, 2])) \ No newline at end of file diff --git a/Python/longest-palindromic-substring.py b/Python/longest-palindromic-substring.py index 2621a208d..7eae3f3f2 100644 --- a/Python/longest-palindromic-substring.py +++ b/Python/longest-palindromic-substring.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(n) # @@ -48,4 +49,4 @@ def preProcess(s): if __name__ == "__main__": - print Solution().longestPalindrome("abb") + print(Solution().longestPalindrome("abb")) diff --git a/Python/longest-substring-with-at-most-two-distinct-characters.py b/Python/longest-substring-with-at-most-two-distinct-characters.py index 0eb42603b..9f9afb8ee 100644 --- a/Python/longest-substring-with-at-most-two-distinct-characters.py +++ b/Python/longest-substring-with-at-most-two-distinct-characters.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(1) # @@ -29,4 +30,4 @@ def lengthOfLongestSubstringTwoDistinct(self, s): return longest if __name__ == "__main__": - print Solution().lengthOfLongestSubstringTwoDistinct("eceba") + print(Solution().lengthOfLongestSubstringTwoDistinct("eceba")) diff --git a/Python/longest-substring-without-repeating-characters.py b/Python/longest-substring-without-repeating-characters.py index 8208f643c..29f7aceaf 100644 --- a/Python/longest-substring-without-repeating-characters.py +++ b/Python/longest-substring-without-repeating-characters.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(1) # @@ -22,4 +23,4 @@ def lengthOfLongestSubstring(self, s): return longest if __name__ == "__main__": - print Solution().lengthOfLongestSubstring("abcabcbb") + print(Solution().lengthOfLongestSubstring("abcabcbb")) diff --git a/Python/longest-valid-parentheses.py b/Python/longest-valid-parentheses.py index 3c8888175..47a503b55 100644 --- a/Python/longest-valid-parentheses.py +++ b/Python/longest-valid-parentheses.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(1) # @@ -53,5 +54,5 @@ def longestValidParentheses(self, s): return longest if __name__ == "__main__": - print Solution().longestValidParentheses("()") - print Solution().longestValidParentheses(")()())") + print(Solution().longestValidParentheses("()")) + print(Solution().longestValidParentheses(")()())")) diff --git a/Python/lru-cache.py b/Python/lru-cache.py index 9913918fc..25e02e3de 100644 --- a/Python/lru-cache.py +++ b/Python/lru-cache.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(1), per operation. # Space: O(k), k is the capacity of cache. @@ -121,7 +122,7 @@ def put(self, key, value): cache.set(1, 1) cache.set(2, 2) cache.set(3, 3) - print cache.get(1) + print(cache.get(1)) cache.set(4, 4) - print cache.get(2) + print(cache.get(2)) diff --git a/Python/majority-element.py b/Python/majority-element.py index 2f39c0403..d4fc73781 100644 --- a/Python/majority-element.py +++ b/Python/majority-element.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(1) # @@ -35,4 +36,4 @@ def majorityElement2(self, nums): return sorted(collections.Counter(nums).items(), key=lambda a: a[1], reverse=True)[0][0] if __name__ == "__main__": - print Solution().majorityElement([1, 2, 3, 4, 5, 5, 5, 5, 5, 5, 6]) \ No newline at end of file + print(Solution().majorityElement([1, 2, 3, 4, 5, 5, 5, 5, 5, 5, 6])) \ No newline at end of file diff --git a/Python/max-points-on-a-line.py b/Python/max-points-on-a-line.py index 79467c9a3..7655881d1 100644 --- a/Python/max-points-on-a-line.py +++ b/Python/max-points-on-a-line.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n^2) # Space: O(n) # @@ -41,4 +42,4 @@ def maxPoints(self, points): return max_points if __name__ == "__main__": - print Solution().maxPoints([Point(), Point(), Point()]) + print(Solution().maxPoints([Point(), Point(), Point()])) diff --git a/Python/maximal-rectangle.py b/Python/maximal-rectangle.py index f4f7f374f..5d7754c37 100644 --- a/Python/maximal-rectangle.py +++ b/Python/maximal-rectangle.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n^2) # Space: O(n) @@ -86,4 +87,4 @@ def maximalRectangle(self, matrix): "11110", "11111", "00000"] - print Solution().maximalRectangle(matrix) + print(Solution().maximalRectangle(matrix)) diff --git a/Python/maximum-depth-of-binary-tree.py b/Python/maximum-depth-of-binary-tree.py index 2889d824a..b0d4292d0 100644 --- a/Python/maximum-depth-of-binary-tree.py +++ b/Python/maximum-depth-of-binary-tree.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(h), h is height of binary tree # @@ -27,4 +28,4 @@ def maxDepth(self, root): root.left = TreeNode(2) root.right = TreeNode(3) root.left.left = TreeNode(4) - print Solution().maxDepth(root) + print(Solution().maxDepth(root)) diff --git a/Python/maximum-gap.py b/Python/maximum-gap.py index 06d771637..5dd4b7ccc 100644 --- a/Python/maximum-gap.py +++ b/Python/maximum-gap.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(n) @@ -79,4 +80,4 @@ def maximumGap(self, nums): if __name__ == "__main__": - print Solution().maximumGap([3, 1, 1, 1, 5, 5, 5, 5]) + print(Solution().maximumGap([3, 1, 1, 1, 5, 5, 5, 5])) diff --git a/Python/maximum-product-subarray.py b/Python/maximum-product-subarray.py index cf502bd5f..b776fb259 100644 --- a/Python/maximum-product-subarray.py +++ b/Python/maximum-product-subarray.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(1) # @@ -31,5 +32,5 @@ def maxProduct(self, A): return global_max if __name__ == "__main__": - print Solution().maxProduct([2, 3, -2, 4]) - print Solution().maxProduct([-4,-3]) \ No newline at end of file + print(Solution().maxProduct([2, 3, -2, 4])) + print(Solution().maxProduct([-4,-3])) \ No newline at end of file diff --git a/Python/maximum-subarray.py b/Python/maximum-subarray.py index 07a0fcafe..de9a3a4b1 100644 --- a/Python/maximum-subarray.py +++ b/Python/maximum-subarray.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(1) # @@ -28,4 +29,4 @@ def maxSubArray(self, nums): if __name__ == "__main__": - print Solution().maxSubArray([-2,1,-3,4,-1,2,1,-5,4]) + print(Solution().maxSubArray([-2,1,-3,4,-1,2,1,-5,4])) diff --git a/Python/median-of-two-sorted-arrays.py b/Python/median-of-two-sorted-arrays.py index a5d2e957e..5e8c460b5 100644 --- a/Python/median-of-two-sorted-arrays.py +++ b/Python/median-of-two-sorted-arrays.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(log(min(m, n))) # Space: O(1) @@ -121,7 +122,7 @@ def findMedianSortedArrays(self, A, B): return C[indexM2] / 1.0 if __name__ == "__main__": - print Solution().findMedianSortedArrays([1, 3, 5, 7], [2, 4, 6]) - print Solution_Generic().findMedianSortedArrays([1, 3, 5], [2, 4, 6]) - print Solution_3().findMedianSortedArrays([1, 3, 5], [2, 4, 6]) + print(Solution().findMedianSortedArrays([1, 3, 5, 7], [2, 4, 6])) + print(Solution_Generic().findMedianSortedArrays([1, 3, 5], [2, 4, 6])) + print(Solution_3().findMedianSortedArrays([1, 3, 5], [2, 4, 6])) diff --git a/Python/merge-intervals.py b/Python/merge-intervals.py index 1e279e4d6..dad170b34 100644 --- a/Python/merge-intervals.py +++ b/Python/merge-intervals.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(nlogn) # Space: O(1) # @@ -38,4 +39,4 @@ def merge(self, intervals): if __name__ == "__main__": - print Solution().merge([Interval(1, 3), Interval(2, 6), Interval(8, 10), Interval(15,18)]) + print(Solution().merge([Interval(1, 3), Interval(2, 6), Interval(8, 10), Interval(15,18)])) diff --git a/Python/merge-k-sorted-lists.py b/Python/merge-k-sorted-lists.py index 0acffe1af..37b677712 100644 --- a/Python/merge-k-sorted-lists.py +++ b/Python/merge-k-sorted-lists.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(nlogk) # Space: O(1) @@ -111,4 +112,4 @@ def mergeKLists(self, lists): list2 = ListNode(2) list2.next = ListNode(4) - print Solution().mergeKLists([list1, list2]) + print(Solution().mergeKLists([list1, list2])) diff --git a/Python/merge-sorted-array.py b/Python/merge-sorted-array.py index b8ace9bb5..411451fc0 100644 --- a/Python/merge-sorted-array.py +++ b/Python/merge-sorted-array.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(1) # @@ -33,7 +34,7 @@ def merge(self, A, m, B, n): A = [1, 3, 5, 0, 0, 0, 0] B = [2, 4, 6, 7] Solution().merge(A, 3, B, 4) - print A + print(A) # Time: O(n) diff --git a/Python/merge-two-sorted-lists.py b/Python/merge-two-sorted-lists.py index c94df5a8b..26c2ec5c5 100644 --- a/Python/merge-two-sorted-lists.py +++ b/Python/merge-two-sorted-lists.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(1) # @@ -41,6 +42,6 @@ def mergeTwoLists(self, l1, l2): l1.next = ListNode(1) l2 = ListNode (2) l2.next = ListNode(3) - print Solution().mergeTwoLists(l1, l2) + print(Solution().mergeTwoLists(l1, l2)) diff --git a/Python/minimum-depth-of-binary-tree.py b/Python/minimum-depth-of-binary-tree.py index 709093e51..5421138ce 100644 --- a/Python/minimum-depth-of-binary-tree.py +++ b/Python/minimum-depth-of-binary-tree.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(h), h is height of binary tree # @@ -28,4 +29,4 @@ def minDepth(self, root): if __name__ == "__main__": root = TreeNode(1) root.left = TreeNode(2) - print Solution().minDepth(root) + print(Solution().minDepth(root)) diff --git a/Python/minimum-path-sum.py b/Python/minimum-path-sum.py index af07387ca..cedfa2733 100644 --- a/Python/minimum-path-sum.py +++ b/Python/minimum-path-sum.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(m * n) # Space: O(m + n) # @@ -23,8 +24,8 @@ def minPathSum(self, grid): return sum[-1] if __name__ == "__main__": - print Solution().minPathSum([[0,1] - ,[1,0]]) - print Solution().minPathSum([[1,3,1] + print(Solution().minPathSum([[0,1] + ,[1,0]])) + print(Solution().minPathSum([[1,3,1] ,[1,5,1] - ,[4,2,1]]) \ No newline at end of file + ,[4,2,1]])) \ No newline at end of file diff --git a/Python/minimum-window-substring.py b/Python/minimum-window-substring.py index d70f8c212..3936f8c74 100644 --- a/Python/minimum-window-substring.py +++ b/Python/minimum-window-substring.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(k), k is the number of different characters @@ -53,4 +54,4 @@ def minWindow(self, s, t): if __name__ == "__main__": - print Solution().minWindow("ADOBECODEBANC", "ABC") + print(Solution().minWindow("ADOBECODEBANC", "ABC")) diff --git a/Python/missing-ranges.py b/Python/missing-ranges.py index 16f3a36a5..3af0ae2c5 100644 --- a/Python/missing-ranges.py +++ b/Python/missing-ranges.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(1) # @@ -38,4 +39,4 @@ def getRange(lower, upper): if __name__ == "__main__": - print Solution().findMissingRanges([0, 1, 3, 50, 75], 0, 99) + print(Solution().findMissingRanges([0, 1, 3, 50, 75], 0, 99)) diff --git a/Python/move-zeroes.py b/Python/move-zeroes.py index 410eb8ffe..26caf64dd 100644 --- a/Python/move-zeroes.py +++ b/Python/move-zeroes.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(1) @@ -52,4 +53,4 @@ def moveZeroes(self, nums): if __name__ == '__main__': s = Solution() r = s.moveZeroes([0, 1, 0, 3, 12]) - print r + print(r) diff --git a/Python/multiply-strings.py b/Python/multiply-strings.py index cfb6d12d8..fd4fdeb90 100644 --- a/Python/multiply-strings.py +++ b/Python/multiply-strings.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(m * n) # Space: O(m + n) # @@ -42,4 +43,4 @@ def multiply(self, num1, num2): if __name__ == "__main__": - print Solution().multiply("123", "1000") + print(Solution().multiply("123", "1000")) diff --git a/Python/n-queens-ii.py b/Python/n-queens-ii.py index ee48742e4..0c5c76c14 100644 --- a/Python/n-queens-ii.py +++ b/Python/n-queens-ii.py @@ -1,3 +1,5 @@ +from __future__ import print_function +from functools import reduce # Time: O(n!) # Space: O(n) # @@ -42,4 +44,4 @@ def totalNQueensRecu(self, solution, row, n): return result if __name__ == "__main__": - print Solution().totalNQueens(8) + print(Solution().totalNQueens(8)) diff --git a/Python/n-queens.py b/Python/n-queens.py index 6140a2e53..c510ca966 100644 --- a/Python/n-queens.py +++ b/Python/n-queens.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n!) # Space: O(n) @@ -78,4 +79,4 @@ def dfs(col_per_row, xy_diff, xy_sum): if __name__ == "__main__": - print Solution().solveNQueens(8) + print(Solution().solveNQueens(8)) diff --git a/Python/next-permutation.py b/Python/next-permutation.py index def9a572d..4384fd7ea 100644 --- a/Python/next-permutation.py +++ b/Python/next-permutation.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(1) # @@ -36,8 +37,8 @@ def nextPermutation(self, num): if __name__ == "__main__": num = [1, 4, 3, 2] Solution().nextPermutation(num) - print num + print(num) Solution().nextPermutation(num) - print num + print(num) Solution().nextPermutation(num) - print num + print(num) diff --git a/Python/number-of-1-bits.py b/Python/number-of-1-bits.py index 065c5db7c..099a184fc 100644 --- a/Python/number-of-1-bits.py +++ b/Python/number-of-1-bits.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(logn) = O(32) # Space: O(1) # @@ -18,4 +19,4 @@ def hammingWeight(self, n): return result if __name__ == '__main__': - print Solution().hammingWeight(11) + print(Solution().hammingWeight(11)) diff --git a/Python/one-edit-distance.py b/Python/one-edit-distance.py index 25a54f523..cb61cdd9e 100644 --- a/Python/one-edit-distance.py +++ b/Python/one-edit-distance.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(m + n) # Space: O(1) # @@ -29,4 +30,4 @@ def isOneEditDistance(self, s, t): if __name__ == "__main__": - print Solution().isOneEditDistance("teacher", "acher") + print(Solution().isOneEditDistance("teacher", "acher")) diff --git a/Python/palindrome-number.py b/Python/palindrome-number.py index 91a16269b..df81686eb 100644 --- a/Python/palindrome-number.py +++ b/Python/palindrome-number.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(1) # Space: O(1) # @@ -29,6 +30,6 @@ def isPalindrome(self, x): return x == reverse if __name__ == "__main__": - print Solution().isPalindrome(12321) - print Solution().isPalindrome(12320) - print Solution().isPalindrome(-12321) + print(Solution().isPalindrome(12321)) + print(Solution().isPalindrome(12320)) + print(Solution().isPalindrome(-12321)) diff --git a/Python/palindrome-partitioning-ii.py b/Python/palindrome-partitioning-ii.py index 192c6f0de..173eeab03 100644 --- a/Python/palindrome-partitioning-ii.py +++ b/Python/palindrome-partitioning-ii.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n^2) # Space: O(n^2) # @@ -25,4 +26,4 @@ def minCut(self, s): return mincut[0] if __name__ == "__main__": - print Solution().minCut("aab") \ No newline at end of file + print(Solution().minCut("aab")) \ No newline at end of file diff --git a/Python/palindrome-partitioning.py b/Python/palindrome-partitioning.py index 49bec0641..640c6a208 100644 --- a/Python/palindrome-partitioning.py +++ b/Python/palindrome-partitioning.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n^2 ~ 2^n) # Space: O(n^2) # @@ -67,4 +68,4 @@ def isPalindrome(self, s): return True if __name__ == "__main__": - print Solution().partition("aab") + print(Solution().partition("aab")) diff --git a/Python/partition-list.py b/Python/partition-list.py index 076f277b8..48d7b74c5 100644 --- a/Python/partition-list.py +++ b/Python/partition-list.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(1) # @@ -49,5 +50,5 @@ def partition(self, head, x): head.next.next.next = ListNode(2) head.next.next.next.next = ListNode(5) head.next.next.next.next.next = ListNode(2) - print Solution().partition(head, 3) + print(Solution().partition(head, 3)) diff --git a/Python/pascals-triangle-ii.py b/Python/pascals-triangle-ii.py index d5aff1748..0b9eaf12d 100644 --- a/Python/pascals-triangle-ii.py +++ b/Python/pascals-triangle-ii.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n^2) # Space: O(1) @@ -63,4 +64,4 @@ def getRow(self, rowIndex): if __name__ == "__main__": - print Solution().getRow(3) + print(Solution().getRow(3)) diff --git a/Python/pascals-triangle.py b/Python/pascals-triangle.py index 14194cc75..a4d915625 100644 --- a/Python/pascals-triangle.py +++ b/Python/pascals-triangle.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n^2) # Space: O(1) # @@ -57,4 +58,4 @@ def add(nums): return res if __name__ == "__main__": - print Solution().generate(5) + print(Solution().generate(5)) diff --git a/Python/path-sum-ii.py b/Python/path-sum-ii.py index 03a2fbf3a..c92886f74 100644 --- a/Python/path-sum-ii.py +++ b/Python/path-sum-ii.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(h), h is height of binary tree # @@ -51,4 +52,4 @@ def pathSumRecu(self, result, cur, root, sum): if __name__ == "__main__": root = TreeNode(5) - print Solution().pathSum(root, 5) + print(Solution().pathSum(root, 5)) diff --git a/Python/path-sum.py b/Python/path-sum.py index 70040766d..7f9a11fb6 100644 --- a/Python/path-sum.py +++ b/Python/path-sum.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(h), h is height of binary tree # @@ -42,4 +43,4 @@ def hasPathSum(self, root, sum): root.right = TreeNode(8) root.left.left = TreeNode(11) root.left.left.right = TreeNode(2) - print Solution().hasPathSum(root, 22) + print(Solution().hasPathSum(root, 22)) diff --git a/Python/permutation-sequence.py b/Python/permutation-sequence.py index 9382f662c..488f38d61 100644 --- a/Python/permutation-sequence.py +++ b/Python/permutation-sequence.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n^2) # Space: O(n) @@ -39,4 +40,4 @@ def getPermutation(self, n, k): if __name__ == "__main__": - print Solution().getPermutation(3, 2) + print(Solution().getPermutation(3, 2)) diff --git a/Python/permutations-ii.py b/Python/permutations-ii.py index 17a03fd77..d6ee2f737 100644 --- a/Python/permutations-ii.py +++ b/Python/permutations-ii.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n * n!) # Space: O(n) # @@ -52,7 +53,7 @@ def permuteUnique(self, nums): return solutions if __name__ == "__main__": - print Solution().permuteUnique([1, 1, 2]) - print Solution().permuteUnique([1, -1, 1, 2, -1, 2, 2, -1]) + print(Solution().permuteUnique([1, 1, 2])) + print(Solution().permuteUnique([1, -1, 1, 2, -1, 2, 2, -1])) diff --git a/Python/permutations.py b/Python/permutations.py index 0a56829ca..5e3887f62 100644 --- a/Python/permutations.py +++ b/Python/permutations.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n * n!) # Space: O(n) # @@ -31,5 +32,5 @@ def permuteRecu(self, result, used, cur, num): used[i] = False if __name__ == "__main__": - print Solution().permute([1, 2, 3]) + print(Solution().permute([1, 2, 3])) diff --git a/Python/plus-one.py b/Python/plus-one.py index d7cfb5fe2..335dcaf53 100644 --- a/Python/plus-one.py +++ b/Python/plus-one.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(1) @@ -42,4 +43,4 @@ def plusOne(self, digits): if __name__ == "__main__": - print Solution().plusOne([9, 9, 9, 9]) + print(Solution().plusOne([9, 9, 9, 9])) diff --git a/Python/populating-next-right-pointers-in-each-node-ii.py b/Python/populating-next-right-pointers-in-each-node-ii.py index 2029f3293..d0d4049cb 100644 --- a/Python/populating-next-right-pointers-in-each-node-ii.py +++ b/Python/populating-next-right-pointers-in-each-node-ii.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(1) # @@ -70,6 +71,6 @@ def connect(self, root): root, root.left, root.right = TreeNode(1), TreeNode(2), TreeNode(3) root.left.left, root.left.right, root.right.right = TreeNode(4), TreeNode(5), TreeNode(7) Solution().connect(root) - print root - print root.left - print root.left.left + print(root) + print(root.left) + print(root.left.left) diff --git a/Python/populating-next-right-pointers-in-each-node.py b/Python/populating-next-right-pointers-in-each-node.py index 54d7a63fc..a23163646 100644 --- a/Python/populating-next-right-pointers-in-each-node.py +++ b/Python/populating-next-right-pointers-in-each-node.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(1) # @@ -78,7 +79,7 @@ def connect(self, root): root, root.left, root.right = TreeNode(1), TreeNode(2), TreeNode(3) root.left.left, root.left.right, root.right.left, root.right.right = TreeNode(4), TreeNode(5), TreeNode(6), TreeNode(7) Solution().connect(root) - print root - print root.left - print root.left.left + print(root) + print(root.left) + print(root.left.left) diff --git a/Python/powx-n.py b/Python/powx-n.py index a787cfd82..59711eb4a 100644 --- a/Python/powx-n.py +++ b/Python/powx-n.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(logn) = O(1) # Space: O(1) @@ -44,5 +45,5 @@ def myPow(self, x, n): if __name__ == "__main__": - print Solution().pow(3, 5) - print Solution().pow(3, -5) + print(Solution().pow(3, 5)) + print(Solution().pow(3, -5)) diff --git a/Python/queue-reconstruction-by-height.py b/Python/queue-reconstruction-by-height.py index afd5f76cd..844da51c4 100644 --- a/Python/queue-reconstruction-by-height.py +++ b/Python/queue-reconstruction-by-height.py @@ -24,7 +24,7 @@ def reconstructQueue(self, people): :type people: List[List[int]] :rtype: List[List[int]] """ - people.sort(key=lambda (h, k): (-h, k)) + people.sort(key=lambda h_k: (-h_k[0], h_k[1])) blocks = [[]] for p in people: @@ -51,7 +51,7 @@ def reconstructQueue(self, people): :type people: List[List[int]] :rtype: List[List[int]] """ - people.sort(key=lambda (h, k): (-h, k)) + people.sort(key=lambda h_k1: (-h_k1[0], h_k1[1])) result = [] for p in people: result.insert(p[1], p) diff --git a/Python/read-n-characters-given-read4-ii-call-multiple-times.py b/Python/read-n-characters-given-read4-ii-call-multiple-times.py index 70dccb689..d5e474d15 100644 --- a/Python/read-n-characters-given-read4-ii-call-multiple-times.py +++ b/Python/read-n-characters-given-read4-ii-call-multiple-times.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(1) # @@ -64,5 +65,5 @@ def read(self, buf, n): sol = Solution() buf = ['' for _ in xrange(100)] file_content = "ab" - print buf[:sol.read(buf, 1)] - print buf[:sol.read(buf, 2)] + print(buf[:sol.read(buf, 1)]) + print(buf[:sol.read(buf, 2)]) diff --git a/Python/read-n-characters-given-read4.py b/Python/read-n-characters-given-read4.py index 2c754e9f7..5799c9b8d 100644 --- a/Python/read-n-characters-given-read4.py +++ b/Python/read-n-characters-given-read4.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(1) # @@ -49,6 +50,6 @@ def read(self, buf, n): global file_content buf = ['' for _ in xrange(100)] file_content = "a" - print buf[:Solution().read(buf, 9)] + print(buf[:Solution().read(buf, 9)]) file_content = "abcdefghijklmnop" - print buf[:Solution().read(buf, 9)] + print(buf[:Solution().read(buf, 9)]) diff --git a/Python/recover-binary-search-tree.py b/Python/recover-binary-search-tree.py index 073967d9e..478a0ebf8 100644 --- a/Python/recover-binary-search-tree.py +++ b/Python/recover-binary-search-tree.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(1) # @@ -85,5 +86,5 @@ def detectBroken(self, broken, pre, cur): if __name__ == "__main__": root = TreeNode(0) root.left = TreeNode(1) - print root - print Solution().recoverTree(root) + print(root) + print(Solution().recoverTree(root)) diff --git a/Python/regular-expression-matching.py b/Python/regular-expression-matching.py index ef240b84a..8c8fb812e 100644 --- a/Python/regular-expression-matching.py +++ b/Python/regular-expression-matching.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(m * n) # Space: O(n) # @@ -120,12 +121,12 @@ def isMatch(self, s, p): return self.isMatch(s, p[2:]) if __name__ == "__main__": - print Solution3().isMatch("abab", "a*b*") - print Solution().isMatch("aaaaaaaaaaaaab", "a*a*a*a*a*a*a*a*a*a*c") - print Solution().isMatch("aa","a") - print Solution().isMatch("aa","aa") - print Solution().isMatch("aaa","aa") - print Solution().isMatch("aa", "a*") - print Solution().isMatch("aa", ".*") - print Solution().isMatch("ab", ".*") - print Solution().isMatch("aab", "c*a*b") + print(Solution3().isMatch("abab", "a*b*")) + print(Solution().isMatch("aaaaaaaaaaaaab", "a*a*a*a*a*a*a*a*a*a*c")) + print(Solution().isMatch("aa","a")) + print(Solution().isMatch("aa","aa")) + print(Solution().isMatch("aaa","aa")) + print(Solution().isMatch("aa", "a*")) + print(Solution().isMatch("aa", ".*")) + print(Solution().isMatch("ab", ".*")) + print(Solution().isMatch("aab", "c*a*b")) diff --git a/Python/remove-duplicates-from-sorted-array-ii.py b/Python/remove-duplicates-from-sorted-array-ii.py index 104161e0a..80fb570d1 100644 --- a/Python/remove-duplicates-from-sorted-array-ii.py +++ b/Python/remove-duplicates-from-sorted-array-ii.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(1) # @@ -28,4 +29,4 @@ def removeDuplicates(self, A): return last + 1 if __name__ == "__main__": - print Solution().removeDuplicates([1, 1, 1, 2, 2, 3]) + print(Solution().removeDuplicates([1, 1, 1, 2, 2, 3])) diff --git a/Python/remove-duplicates-from-sorted-list-ii.py b/Python/remove-duplicates-from-sorted-list-ii.py index 9d4d364be..c0088c186 100644 --- a/Python/remove-duplicates-from-sorted-list-ii.py +++ b/Python/remove-duplicates-from-sorted-list-ii.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(1) # @@ -45,5 +46,5 @@ def deleteDuplicates(self, head): head, head.next, head.next.next = ListNode(1), ListNode(2), ListNode(3) head.next.next.next, head.next.next.next.next = ListNode(3), ListNode(4) head.next.next.next.next.next, head.next.next.next.next.next.next = ListNode(4), ListNode(5) - print Solution().deleteDuplicates(head) + print(Solution().deleteDuplicates(head)) diff --git a/Python/remove-duplicates-from-sorted-list.py b/Python/remove-duplicates-from-sorted-list.py index 1eda2ef1c..dc29c5e2c 100644 --- a/Python/remove-duplicates-from-sorted-list.py +++ b/Python/remove-duplicates-from-sorted-list.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(1) # @@ -49,4 +50,4 @@ def deleteDuplicates2(self, head): if __name__ == "__main__": head, head.next, head.next.next = ListNode(1), ListNode(1), ListNode(2) head.next.next.next, head.next.next.next.next = ListNode(3), ListNode(3) - print Solution().deleteDuplicates(head) + print(Solution().deleteDuplicates(head)) diff --git a/Python/remove-element.py b/Python/remove-element.py index edb0f237b..0f6a3b18c 100644 --- a/Python/remove-element.py +++ b/Python/remove-element.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(1) # @@ -21,4 +22,4 @@ def removeElement(self, A, elem): return last + 1 if __name__ == "__main__": - print Solution().removeElement([1, 2, 3, 4, 5, 2, 2], 2) \ No newline at end of file + print(Solution().removeElement([1, 2, 3, 4, 5, 2, 2], 2)) \ No newline at end of file diff --git a/Python/remove-nth-node-from-end-of-list.py b/Python/remove-nth-node-from-end-of-list.py index 637c8c198..a79c34902 100644 --- a/Python/remove-nth-node-from-end-of-list.py +++ b/Python/remove-nth-node-from-end-of-list.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(1) # @@ -49,4 +50,4 @@ def removeNthFromEnd(self, head, n): head.next.next.next = ListNode(4) head.next.next.next.next = ListNode(5) - print Solution().removeNthFromEnd(head, 2) \ No newline at end of file + print(Solution().removeNthFromEnd(head, 2)) \ No newline at end of file diff --git a/Python/reorder-list.py b/Python/reorder-list.py index 8d5541c83..6db5922c1 100644 --- a/Python/reorder-list.py +++ b/Python/reorder-list.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(1) # @@ -51,4 +52,4 @@ def reorderList(self, head): head.next.next = ListNode(3) head.next.next.next = ListNode(4) head.next.next.next.next = ListNode(5) - print Solution().reorderList(head) \ No newline at end of file + print(Solution().reorderList(head)) \ No newline at end of file diff --git a/Python/repeated-dna-sequences.py b/Python/repeated-dna-sequences.py index bb9373bfa..bb15174c3 100644 --- a/Python/repeated-dna-sequences.py +++ b/Python/repeated-dna-sequences.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(n) @@ -45,6 +46,6 @@ def findRepeatedDnaSequences2(self, s): if __name__ == "__main__": - print Solution().findRepeatedDnaSequences("AAAAAAAAAA") - print Solution().findRepeatedDnaSequences("") - print Solution().findRepeatedDnaSequences("AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT") + print(Solution().findRepeatedDnaSequences("AAAAAAAAAA")) + print(Solution().findRepeatedDnaSequences("")) + print(Solution().findRepeatedDnaSequences("AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT")) diff --git a/Python/repeated-substring-pattern.py b/Python/repeated-substring-pattern.py index b7925f67b..8a158f903 100644 --- a/Python/repeated-substring-pattern.py +++ b/Python/repeated-substring-pattern.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(n) @@ -55,5 +56,5 @@ def repeatedSubstringPattern2(self, str): return False ss = (str + str)[1:-1] - print ss + print(ss) return ss.find(str) != -1 diff --git a/Python/restore-ip-addresses.py b/Python/restore-ip-addresses.py index e1e338b64..90d126060 100644 --- a/Python/restore-ip-addresses.py +++ b/Python/restore-ip-addresses.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n^m) = O(3^4) # Space: O(n * m) = O(3 * 4) # @@ -37,4 +38,4 @@ def isValid(self, s): return int(s) < 256 if __name__ == "__main__": - print Solution().restoreIpAddresses("0000") + print(Solution().restoreIpAddresses("0000")) diff --git a/Python/reverse-bits.py b/Python/reverse-bits.py index baecdb400..9d33d712b 100644 --- a/Python/reverse-bits.py +++ b/Python/reverse-bits.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time : O(logn) = O(32) # Space: O(1) # @@ -31,4 +32,4 @@ def reverseBits2(self, n): return int(string, 2) if __name__ == '__main__': - print Solution().reverseBits(1) + print(Solution().reverseBits(1)) diff --git a/Python/reverse-integer.py b/Python/reverse-integer.py index 8533bbe1c..7c8ae7f46 100644 --- a/Python/reverse-integer.py +++ b/Python/reverse-integer.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(logn) = O(1) # Space: O(1) # @@ -53,10 +54,10 @@ def reverse3(self, x): :rtype: int """ s = cmp(x, 0) - r = int(`s * x`[::-1]) + r = int(repr(s * x)[::-1]) return s * r * (r < 2 ** 31) if __name__ == "__main__": - print Solution().reverse(123) - print Solution().reverse(-321) + print(Solution().reverse(123)) + print(Solution().reverse(-321)) diff --git a/Python/reverse-linked-list-ii.py b/Python/reverse-linked-list-ii.py index d18404785..42fabb533 100644 --- a/Python/reverse-linked-list-ii.py +++ b/Python/reverse-linked-list-ii.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(1) # @@ -49,4 +50,4 @@ def reverseBetween(self, head, m, n): head.next.next = ListNode(3) head.next.next.next = ListNode(4) head.next.next.next.next = ListNode(5) - print Solution().reverseBetween(head, 2, 4) + print(Solution().reverseBetween(head, 2, 4)) diff --git a/Python/reverse-linked-list.py b/Python/reverse-linked-list.py index b000888f2..ae62a1b29 100644 --- a/Python/reverse-linked-list.py +++ b/Python/reverse-linked-list.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(1) # @@ -58,4 +59,4 @@ def reverseListRecu(self, head): head.next.next = ListNode(3) head.next.next.next = ListNode(4) head.next.next.next.next = ListNode(5) - print Solution2().reverseList(head) \ No newline at end of file + print(Solution2().reverseList(head)) \ No newline at end of file diff --git a/Python/reverse-nodes-in-k-group.py b/Python/reverse-nodes-in-k-group.py index 15f95d5fa..373c7bf94 100644 --- a/Python/reverse-nodes-in-k-group.py +++ b/Python/reverse-nodes-in-k-group.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(1) # @@ -67,4 +68,4 @@ def reverse(self, begin, end): head.next.next = ListNode(3) head.next.next.next = ListNode(4) head.next.next.next.next = ListNode(5) - print Solution().reverseKGroup(head, 2) \ No newline at end of file + print(Solution().reverseKGroup(head, 2)) \ No newline at end of file diff --git a/Python/reverse-words-in-a-string-ii.py b/Python/reverse-words-in-a-string-ii.py index e87268406..c30b947e3 100644 --- a/Python/reverse-words-in-a-string-ii.py +++ b/Python/reverse-words-in-a-string-ii.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space:O(1) # @@ -35,4 +36,4 @@ def reverse(s, begin, end): if __name__ == '__main__': s = ['h','e','l','l','o', ' ', 'w', 'o', 'r', 'l', 'd'] Solution().reverseWords(s) - print s + print(s) diff --git a/Python/reverse-words-in-a-string.py b/Python/reverse-words-in-a-string.py index e660e205c..0db120660 100644 --- a/Python/reverse-words-in-a-string.py +++ b/Python/reverse-words-in-a-string.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(n) # @@ -25,4 +26,4 @@ def reverseWords(self, s): return ' '.join(reversed(s.split())) if __name__ == '__main__': - print Solution().reverseWords('hello world') + print(Solution().reverseWords('hello world')) diff --git a/Python/roman-to-integer.py b/Python/roman-to-integer.py index a5d2d05d9..0a977205d 100644 --- a/Python/roman-to-integer.py +++ b/Python/roman-to-integer.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(1) # @@ -19,5 +20,5 @@ def romanToInt(self, s): return decimal if __name__ == "__main__": - print Solution().romanToInt("IIVX") - print Solution().romanToInt("MMMCMXCIX") + print(Solution().romanToInt("IIVX")) + print(Solution().romanToInt("MMMCMXCIX")) diff --git a/Python/rotate-array.py b/Python/rotate-array.py index 8e43bff0c..d2822f479 100644 --- a/Python/rotate-array.py +++ b/Python/rotate-array.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(1) @@ -113,4 +114,4 @@ def rotate(self, nums, k): if __name__ == '__main__': nums = [1, 2, 3, 4, 5, 6, 7] Solution().rotate(nums, 3) - print nums + print(nums) diff --git a/Python/rotate-image.py b/Python/rotate-image.py index 648b4c715..25b0f9dba 100644 --- a/Python/rotate-image.py +++ b/Python/rotate-image.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n^2) # Space: O(1) # @@ -39,4 +40,4 @@ def rotate(self, matrix): if __name__ == "__main__": matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] - print Solution().rotate(matrix) + print(Solution().rotate(matrix)) diff --git a/Python/rotate-list.py b/Python/rotate-list.py index 59ecb567e..717ab0bdc 100644 --- a/Python/rotate-list.py +++ b/Python/rotate-list.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(1) # @@ -49,4 +50,4 @@ def rotateRight(self, head, k): head.next.next = ListNode(3) head.next.next.next = ListNode(4) head.next.next.next.next = ListNode(5) - print Solution().rotateRight(head, 2) + print(Solution().rotateRight(head, 2)) diff --git a/Python/same-tree.py b/Python/same-tree.py index 6418ffc0e..d89114b14 100644 --- a/Python/same-tree.py +++ b/Python/same-tree.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(h), h is height of binary tree # @@ -29,4 +30,4 @@ def isSameTree(self, p, q): if __name__ == "__main__": root1, root1.left, root1.right = TreeNode(1), TreeNode(2), TreeNode(3) root2, root2.left, root2.right = TreeNode(1), TreeNode(2), TreeNode(3) - print Solution().isSameTree(root1, root2) + print(Solution().isSameTree(root1, root2)) diff --git a/Python/scramble-string.py b/Python/scramble-string.py index 2627135af..ad0b9e8e6 100644 --- a/Python/scramble-string.py +++ b/Python/scramble-string.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n^4) # Space: O(n^3) # @@ -67,4 +68,4 @@ def isScramble(self, s1, s2): return result[n][0][0] if __name__ == "__main__": - print Solution().isScramble("rgtae", "great") + print(Solution().isScramble("rgtae", "great")) diff --git a/Python/search-a-2d-matrix.py b/Python/search-a-2d-matrix.py index 793894f2a..dec6cbbf2 100644 --- a/Python/search-a-2d-matrix.py +++ b/Python/search-a-2d-matrix.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(logm + logn) # Space: O(1) # @@ -41,4 +42,4 @@ def searchMatrix(self, matrix, target): if __name__ == "__main__": matrix = [[1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 50]] - print Solution().searchMatrix(matrix, 3) + print(Solution().searchMatrix(matrix, 3)) diff --git a/Python/search-for-a-range.py b/Python/search-for-a-range.py index 9d3ad9766..81a53c10f 100644 --- a/Python/search-for-a-range.py +++ b/Python/search-for-a-range.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(logn) # Space: O(1) # @@ -59,5 +60,5 @@ def binarySearch3(self, compare, nums, target): if __name__ == "__main__": - print Solution().searchRange([2, 2], 3) - print Solution().searchRange([5, 7, 7, 8, 8, 10], 8) + print(Solution().searchRange([2, 2], 3)) + print(Solution().searchRange([5, 7, 7, 8, 8, 10], 8)) diff --git a/Python/search-in-rotated-sorted-array-ii.py b/Python/search-in-rotated-sorted-array-ii.py index 43a607f05..f3215fa5e 100644 --- a/Python/search-in-rotated-sorted-array-ii.py +++ b/Python/search-in-rotated-sorted-array-ii.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(logn) # Space: O(1) # @@ -35,6 +36,6 @@ def search(self, nums, target): if __name__ == "__main__": - print Solution().search([3, 5, 1], 3) - print Solution().search([2, 2, 3, 3, 4, 1], 1) - print Solution().search([4, 4, 5, 6, 7, 0, 1, 2], 5) + print(Solution().search([3, 5, 1], 3)) + print(Solution().search([2, 2, 3, 3, 4, 1], 1)) + print(Solution().search([4, 4, 5, 6, 7, 0, 1, 2], 5)) diff --git a/Python/search-in-rotated-sorted-array.py b/Python/search-in-rotated-sorted-array.py index 4c93474b0..45b30445c 100644 --- a/Python/search-in-rotated-sorted-array.py +++ b/Python/search-in-rotated-sorted-array.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(logn) # Space: O(1) # @@ -34,6 +35,6 @@ def search(self, nums, target): if __name__ == "__main__": - print Solution().search([3, 5, 1], 3) - print Solution().search([1], 1) - print Solution().search([4, 5, 6, 7, 0, 1, 2], 5) + print(Solution().search([3, 5, 1], 3)) + print(Solution().search([1], 1)) + print(Solution().search([4, 5, 6, 7, 0, 1, 2], 5)) diff --git a/Python/search-insert-position.py b/Python/search-insert-position.py index 89b3f18de..4c31609c2 100644 --- a/Python/search-insert-position.py +++ b/Python/search-insert-position.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(logn) # Space: O(1) # @@ -33,7 +34,7 @@ def searchInsert(self, nums, target): if __name__ == "__main__": - print Solution().searchInsert([1, 3, 5, 6], 5) - print Solution().searchInsert([1, 3, 5, 6], 2) - print Solution().searchInsert([1, 3, 5, 6], 7) - print Solution().searchInsert([1, 3, 5, 6], 0) + print(Solution().searchInsert([1, 3, 5, 6], 5)) + print(Solution().searchInsert([1, 3, 5, 6], 2)) + print(Solution().searchInsert([1, 3, 5, 6], 7)) + print(Solution().searchInsert([1, 3, 5, 6], 0)) diff --git a/Python/set-intersection-size-at-least-two.py b/Python/set-intersection-size-at-least-two.py index 571b8ddd5..94cf0517d 100644 --- a/Python/set-intersection-size-at-least-two.py +++ b/Python/set-intersection-size-at-least-two.py @@ -33,7 +33,7 @@ def intersectionSizeTwo(self, intervals): :type intervals: List[List[int]] :rtype: int """ - intervals.sort(key = lambda(s, e): (s, -e)) + intervals.sort(key = lambda s_e: (s_e[0], -s_e[1])) cnts = [2] * len(intervals) result = 0 while intervals: diff --git a/Python/set-matrix-zeroes.py b/Python/set-matrix-zeroes.py index 4a08ded96..af8226320 100644 --- a/Python/set-matrix-zeroes.py +++ b/Python/set-matrix-zeroes.py @@ -1,3 +1,5 @@ +from __future__ import print_function +from functools import reduce # Time: O(m * n) # Space: O(1) # @@ -41,4 +43,4 @@ def setZeroes(self, matrix): , [1, 1, 1, 0] , [1, 1, 1, 1]] Solution().setZeroes(matrix) - print matrix \ No newline at end of file + print(matrix) \ No newline at end of file diff --git a/Python/simplify-path.py b/Python/simplify-path.py index 532b544c1..6d06133fa 100644 --- a/Python/simplify-path.py +++ b/Python/simplify-path.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(n) # @@ -28,5 +29,5 @@ def simplifyPath(self, path): return "/" + "/".join(stack) if __name__ == "__main__": - print Solution().simplifyPath("/../") - print Solution().simplifyPath("/home//foo/") \ No newline at end of file + print(Solution().simplifyPath("/../")) + print(Solution().simplifyPath("/home//foo/")) \ No newline at end of file diff --git a/Python/single-number-ii.py b/Python/single-number-ii.py index 586b80c30..6e1e91f1f 100644 --- a/Python/single-number-ii.py +++ b/Python/single-number-ii.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(1) # @@ -61,4 +62,4 @@ def singleNumber(self, A): return two if __name__ == "__main__": - print Solution().singleNumber([1, 1, 1, 2, 2, 2, 3]) + print(Solution().singleNumber([1, 1, 1, 2, 2, 2, 3])) diff --git a/Python/single-number.py b/Python/single-number.py index 742ea9c09..e48062ebd 100644 --- a/Python/single-number.py +++ b/Python/single-number.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(1) # @@ -8,6 +9,7 @@ # import operator +from functools import reduce class Solution: @@ -19,4 +21,4 @@ def singleNumber(self, A): return reduce(operator.xor, A) if __name__ == '__main__': - print Solution().singleNumber([1, 1, 2, 2, 3]) + print(Solution().singleNumber([1, 1, 2, 2, 3])) diff --git a/Python/sort-colors.py b/Python/sort-colors.py index 9a7b2b8dd..dc3e519af 100644 --- a/Python/sort-colors.py +++ b/Python/sort-colors.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(1) # @@ -44,4 +45,4 @@ def triPartition(nums, target): if __name__ == "__main__": A = [2, 1, 1, 0, 0, 2] Solution().sortColors(A) - print A + print(A) diff --git a/Python/sort-list.py b/Python/sort-list.py index d01ed7f56..7a2749efc 100644 --- a/Python/sort-list.py +++ b/Python/sort-list.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(nlogn) # Space: O(logn) for stack call # @@ -53,4 +54,4 @@ def mergeTwoLists(self, l1, l2): head.next = ListNode(4) head.next.next = ListNode(1) head.next.next.next= ListNode(2) - print Solution().sortList(head) + print(Solution().sortList(head)) diff --git a/Python/spiral-matrix-ii.py b/Python/spiral-matrix-ii.py index d23da6b19..5244cb820 100644 --- a/Python/spiral-matrix-ii.py +++ b/Python/spiral-matrix-ii.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n^2) # Space: O(1) # @@ -42,5 +43,5 @@ def generateMatrix(self, n): if __name__ == "__main__": - print Solution().generateMatrix(3) - print Solution().generateMatrix(8) + print(Solution().generateMatrix(3)) + print(Solution().generateMatrix(8)) diff --git a/Python/spiral-matrix.py b/Python/spiral-matrix.py index 1e0734fe1..78e4df195 100644 --- a/Python/spiral-matrix.py +++ b/Python/spiral-matrix.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(m * n) # Space: O(1) # @@ -41,7 +42,7 @@ def spiralOrder(self, matrix): if __name__ == "__main__": - print Solution().spiralOrder([[ 1, 2, 3 ], + print(Solution().spiralOrder([[ 1, 2, 3 ], [ 4, 5, 6 ], - [ 7, 8, 9 ]]) - print Solution().spiralOrder([[2,3]]) \ No newline at end of file + [ 7, 8, 9 ]])) + print(Solution().spiralOrder([[2,3]])) \ No newline at end of file diff --git a/Python/sqrtx.py b/Python/sqrtx.py index 05296b789..fce6acc1b 100644 --- a/Python/sqrtx.py +++ b/Python/sqrtx.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(logn) # Space: O(1) @@ -26,5 +27,5 @@ def mySqrt(self, x): if __name__ == "__main__": - print Solution().mySqrt(10) + print(Solution().mySqrt(10)) diff --git a/Python/subsets-ii.py b/Python/subsets-ii.py index ebd20f0bb..3c7199d83 100644 --- a/Python/subsets-ii.py +++ b/Python/subsets-ii.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n * 2^n) # Space: O(1) @@ -84,4 +85,4 @@ def subsetsWithDupRecu(self, result, cur, nums): if __name__ == "__main__": - print Solution().subsetsWithDup([1, 2, 2]) + print(Solution().subsetsWithDup([1, 2, 2])) diff --git a/Python/subsets.py b/Python/subsets.py index 83ba120db..952f4ad48 100644 --- a/Python/subsets.py +++ b/Python/subsets.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n * 2^n) # Space: O(1) @@ -77,4 +78,4 @@ def subsetsRecu(self, cur, nums): if __name__ == "__main__": - print Solution().subsets([1, 2, 3]) + print(Solution().subsets([1, 2, 3])) diff --git a/Python/substring-with-concatenation-of-all-words.py b/Python/substring-with-concatenation-of-all-words.py index f56813378..4d12bdbf9 100644 --- a/Python/substring-with-concatenation-of-all-words.py +++ b/Python/substring-with-concatenation-of-all-words.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O((m + n) * k), where m is string length, n is dictionary size, k is word length # Space: O(n * k) @@ -95,4 +96,4 @@ def findSubstring(self, s, words): if __name__ == "__main__": - print Solution().findSubstring("barfoothefoobarman", ["foo", "bar"]) + print(Solution().findSubstring("barfoothefoobarman", ["foo", "bar"])) diff --git a/Python/sum-root-to-leaf-numbers.py b/Python/sum-root-to-leaf-numbers.py index faad48bff..2c1a71365 100644 --- a/Python/sum-root-to-leaf-numbers.py +++ b/Python/sum-root-to-leaf-numbers.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(h), h is height of binary tree # @@ -44,4 +45,4 @@ def sumNumbersRecu(self, root, num): root = TreeNode(1) root.left = TreeNode(2) root.right = TreeNode(3) - print Solution().sumNumbers(root) + print(Solution().sumNumbers(root)) diff --git a/Python/summary-ranges.py b/Python/summary-ranges.py index 6bdddb9f2..b8bded7ee 100644 --- a/Python/summary-ranges.py +++ b/Python/summary-ranges.py @@ -42,5 +42,5 @@ class Solution2: # @param {integer[]} nums # @return {string[]} def summaryRanges(self, nums): - return [re.sub('->.*>', '->', '->'.join(`n` for _, n in g)) - for _, g in itertools.groupby(enumerate(nums), lambda (i, n): n-i)] + return [re.sub('->.*>', '->', '->'.join(repr(n) for _, n in g)) + for _, g in itertools.groupby(enumerate(nums), lambda i_n: i_n[1]-i_n[0])] diff --git a/Python/surrounded-regions.py b/Python/surrounded-regions.py index e586a1acc..6178ac2d5 100644 --- a/Python/surrounded-regions.py +++ b/Python/surrounded-regions.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(m * n) # Space: O(m + n) # @@ -61,4 +62,4 @@ def solve(self, board): ['X', 'X', 'O', 'X'], ['X', 'O', 'X', 'X']] Solution().solve(board) - print board + print(board) diff --git a/Python/swap-nodes-in-pairs.py b/Python/swap-nodes-in-pairs.py index 91c039f8f..73ca8fc63 100644 --- a/Python/swap-nodes-in-pairs.py +++ b/Python/swap-nodes-in-pairs.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(1) # @@ -38,4 +39,4 @@ def swapPairs(self, head): if __name__ == "__main__": head = ListNode(1) head.next, head.next.next, head.next.next.next = ListNode(2), ListNode(3), ListNode(4) - print Solution().swapPairs(head) + print(Solution().swapPairs(head)) diff --git a/Python/symmetric-tree.py b/Python/symmetric-tree.py index 3bc99e50b..1b361f336 100644 --- a/Python/symmetric-tree.py +++ b/Python/symmetric-tree.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(h), h is height of binary tree # Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). @@ -76,5 +77,5 @@ def isSymmetricRecu(self, left, right): root.left, root.right = TreeNode(2), TreeNode(2) root.left.left, root.right.right = TreeNode(3), TreeNode(3) root.left.right, root.right.left = TreeNode(4), TreeNode(4) - print Solution().isSymmetric(root) + print(Solution().isSymmetric(root)) diff --git a/Python/text-justification.py b/Python/text-justification.py index ccc5ab439..bc9c9b4da 100644 --- a/Python/text-justification.py +++ b/Python/text-justification.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(k), k is maxWidth. # @@ -67,4 +68,4 @@ def connect(words, maxWidth, begin, end, length, is_last): if __name__ == "__main__": - print Solution().fullJustify(["This", "is", "an", "example", "of", "text", "justification."], 16) + print(Solution().fullJustify(["This", "is", "an", "example", "of", "text", "justification."], 16)) diff --git a/Python/trapping-rain-water.py b/Python/trapping-rain-water.py index 28a0a7e41..09b40444e 100644 --- a/Python/trapping-rain-water.py +++ b/Python/trapping-rain-water.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(1) # @@ -59,4 +60,4 @@ def trap(self, A): return result if __name__ == "__main__": - print Solution().trap([0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]) + print(Solution().trap([0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1])) diff --git a/Python/triangle.py b/Python/triangle.py index 7d2b541f6..836c42f4d 100644 --- a/Python/triangle.py +++ b/Python/triangle.py @@ -1,3 +1,5 @@ +from __future__ import print_function +from functools import reduce # Time: O(m * n) # Space: O(n) # @@ -34,5 +36,5 @@ def minimumTotal(self, triangle): return reduce(min, cur) if __name__ == "__main__": - print Solution().minimumTotal([[-1], [2, 3], [1, -1, -3]]) + print(Solution().minimumTotal([[-1], [2, 3], [1, -1, -3]])) diff --git a/Python/two-sum-ii-input-array-is-sorted.py b/Python/two-sum-ii-input-array-is-sorted.py index 8ac26f965..356476b00 100644 --- a/Python/two-sum-ii-input-array-is-sorted.py +++ b/Python/two-sum-ii-input-array-is-sorted.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(1) # @@ -28,4 +29,4 @@ def twoSum(self, nums, target): return [start + 1, end + 1] if __name__ == "__main__": - print Solution().twoSum([2, 7, 11, 15], 9) + print(Solution().twoSum([2, 7, 11, 15], 9)) diff --git a/Python/two-sum-iii-data-structure-design.py b/Python/two-sum-iii-data-structure-design.py index 05bf1c555..55d1ae7aa 100644 --- a/Python/two-sum-iii-data-structure-design.py +++ b/Python/two-sum-iii-data-structure-design.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(n) @@ -51,5 +52,5 @@ def find(self, value): Sol.add(i) for i in (4, 7): - print Sol.find(i) + print(Sol.find(i)) diff --git a/Python/unique-binary-search-trees-ii.py b/Python/unique-binary-search-trees-ii.py index 1b031830e..ddda4ba7f 100644 --- a/Python/unique-binary-search-trees-ii.py +++ b/Python/unique-binary-search-trees-ii.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(4^n / n^(3/2)) ~= Catalan numbers # Space: O(4^n / n^(3/2)) ~= Catalan numbers # @@ -66,4 +67,4 @@ def generateTreesRecu(self, low, high): return result if __name__ == "__main__": - print Solution().generateTrees(3) \ No newline at end of file + print(Solution().generateTrees(3)) \ No newline at end of file diff --git a/Python/unique-binary-search-trees.py b/Python/unique-binary-search-trees.py index 61d446f51..bad28ae88 100644 --- a/Python/unique-binary-search-trees.py +++ b/Python/unique-binary-search-trees.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(1) # @@ -47,4 +48,4 @@ def numTrees(self, n): return counts[-1] if __name__ == "__main__": - print Solution().numTrees(3) + print(Solution().numTrees(3)) diff --git a/Python/unique-paths-ii.py b/Python/unique-paths-ii.py index bbe3507b1..4ed5a8108 100644 --- a/Python/unique-paths-ii.py +++ b/Python/unique-paths-ii.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(m * n) # Space: O(m + n) # @@ -48,4 +49,4 @@ def uniquePathsWithObstacles(self, obstacleGrid): [0,1,0], [0,0,0] ] - print Solution().uniquePathsWithObstacles(obstacleGrid) + print(Solution().uniquePathsWithObstacles(obstacleGrid)) diff --git a/Python/unique-paths.py b/Python/unique-paths.py index 5bfaefa32..efe228e3c 100644 --- a/Python/unique-paths.py +++ b/Python/unique-paths.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(m * n) # Space: O(m + n) # @@ -25,4 +26,4 @@ def uniquePaths(self, m, n): return ways[n - 1] if __name__ == "__main__": - print Solution().uniquePaths(1, 2) + print(Solution().uniquePaths(1, 2)) diff --git a/Python/valid-number.py b/Python/valid-number.py index 047584ba4..1630ec0c6 100644 --- a/Python/valid-number.py +++ b/Python/valid-number.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(1) # @@ -73,8 +74,8 @@ def isNumber(self, s): if __name__ == "__main__": - print Solution().isNumber(" 0.1 ") - print Solution().isNumber("abc") - print Solution().isNumber("1 a") - print Solution().isNumber("2e10") + print(Solution().isNumber(" 0.1 ")) + print(Solution().isNumber("abc")) + print(Solution().isNumber("1 a")) + print(Solution().isNumber("2e10")) diff --git a/Python/valid-palindrome.py b/Python/valid-palindrome.py index fbbcf7f0f..d3db6552e 100644 --- a/Python/valid-palindrome.py +++ b/Python/valid-palindrome.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(1) # @@ -29,4 +30,4 @@ def isPalindrome(self, s): return True if __name__ == "__main__": - print Solution().isPalindrome("A man, a plan, a canal: Panama") + print(Solution().isPalindrome("A man, a plan, a canal: Panama")) diff --git a/Python/valid-parentheses.py b/Python/valid-parentheses.py index 3d9d03781..0569e43e9 100644 --- a/Python/valid-parentheses.py +++ b/Python/valid-parentheses.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(n) # @@ -20,5 +21,5 @@ def isValid(self, s): return len(stack) == 0 if __name__ == "__main__": - print Solution().isValid("()[]{}") - print Solution().isValid("()[{]}") \ No newline at end of file + print(Solution().isValid("()[]{}")) + print(Solution().isValid("()[{]}")) \ No newline at end of file diff --git a/Python/valid-sudoku.py b/Python/valid-sudoku.py index 24f9feb39..243aa4683 100644 --- a/Python/valid-sudoku.py +++ b/Python/valid-sudoku.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(9^2) # Space: O(9) @@ -45,4 +46,4 @@ def isValidList(self, xs): ['.', '.', '.', '.', '.', '.', 7, '.', '.'], ['.', '.', '.', '.', '.', '.', '.', 8, '.'], ['.', '.', '.', '.', '.', '.', '.', '.', 9]] - print Solution().isValidSudoku(board) + print(Solution().isValidSudoku(board)) diff --git a/Python/validate-binary-search-tree.py b/Python/validate-binary-search-tree.py index d37799d21..2ef06fb87 100644 --- a/Python/validate-binary-search-tree.py +++ b/Python/validate-binary-search-tree.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(1) # @@ -68,4 +69,4 @@ def isValidBSTRecu(self, root, low, high): root = TreeNode(2) root.left = TreeNode(1) root.right = TreeNode(3) - print Solution().isValidBST(root) + print(Solution().isValidBST(root)) diff --git a/Python/wildcard-matching.py b/Python/wildcard-matching.py index f98db3e20..9d62f3271 100644 --- a/Python/wildcard-matching.py +++ b/Python/wildcard-matching.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(m + n) # Space: O(1) # @@ -114,12 +115,12 @@ def isMatch(self, s, p): return self.isMatch(s, p[1:]) if __name__ == "__main__": - print Solution().isMatch("aaaabaaaab","a*b*b") - print Solution().isMatch("aaaaaaaaaaaaab", "a*a*a*a*a*a*a*a*a*a*c") - print Solution().isMatch("aa","a") - print Solution().isMatch("aa","aa") - print Solution().isMatch("aaa","aa") - print Solution().isMatch("aa", "a*") - print Solution().isMatch("aa", "?*") - print Solution().isMatch("ab", "?*") - print Solution().isMatch("aab", "c*a*b") + print(Solution().isMatch("aaaabaaaab","a*b*b")) + print(Solution().isMatch("aaaaaaaaaaaaab", "a*a*a*a*a*a*a*a*a*a*c")) + print(Solution().isMatch("aa","a")) + print(Solution().isMatch("aa","aa")) + print(Solution().isMatch("aaa","aa")) + print(Solution().isMatch("aa", "a*")) + print(Solution().isMatch("aa", "?*")) + print(Solution().isMatch("ab", "?*")) + print(Solution().isMatch("aab", "c*a*b")) diff --git a/Python/word-break-ii.py b/Python/word-break-ii.py index e23c7ec25..804778b3e 100644 --- a/Python/word-break-ii.py +++ b/Python/word-break-ii.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n * l^2 + n * r), l is the max length of the words, # r is the number of the results. # Space: O(n^2) @@ -53,4 +54,4 @@ def wordBreakHelper(self, s, valid, start, path, result): if __name__ == "__main__": - print Solution().wordBreak("catsanddog", ["cat", "cats", "and", "sand", "dog"]) + print(Solution().wordBreak("catsanddog", ["cat", "cats", "and", "sand", "dog"])) diff --git a/Python/word-break.py b/Python/word-break.py index 5955525de..0bbc5c266 100644 --- a/Python/word-break.py +++ b/Python/word-break.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n * l^2) # Space: O(n) @@ -35,4 +36,4 @@ def wordBreak(self, s, wordDict): if __name__ == "__main__": - print Solution().wordBreak("leetcode", ["leet", "code"]) + print(Solution().wordBreak("leetcode", ["leet", "code"])) diff --git a/Python/word-ladder-ii.py b/Python/word-ladder-ii.py index 7edc61b1c..e6475bfe6 100644 --- a/Python/word-ladder-ii.py +++ b/Python/word-ladder-ii.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n * d), n is length of string, d is size of dictionary # Space: O(d) # @@ -62,4 +63,4 @@ def backtrack(self, result, trace, path, word): self.backtrack(result, trace, [word] + path, prev) if __name__ == "__main__": - print Solution().findLadders("hit", "cog", set(["hot","dot","dog","lot","log"])) + print(Solution().findLadders("hit", "cog", set(["hot","dot","dog","lot","log"]))) diff --git a/Python/word-ladder.py b/Python/word-ladder.py index ff2b0cb70..db057c541 100644 --- a/Python/word-ladder.py +++ b/Python/word-ladder.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n * d), n is length of string, d is size of dictionary # Space: O(d) # @@ -50,5 +51,5 @@ def ladderLength(self, beginWord, endWord, wordList): if __name__ == "__main__": - print Solution().ladderLength("hit", "cog", set(["hot", "dot", "dog", "lot", "log"])) - print Solution().ladderLength("hit", "cog", set(["hot", "dot", "dog", "lot", "log", "cog"])) + print(Solution().ladderLength("hit", "cog", set(["hot", "dot", "dog", "lot", "log"]))) + print(Solution().ladderLength("hit", "cog", set(["hot", "dot", "dog", "lot", "log", "cog"]))) diff --git a/Python/word-search.py b/Python/word-search.py index 719540086..4dfe9763a 100644 --- a/Python/word-search.py +++ b/Python/word-search.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(m * n * l) # Space: O(l) # @@ -56,6 +57,6 @@ def existRecu(self, board, word, cur, i, j, visited): "SFCS", "ADEE" ] - print Solution().exist(board, "ABCCED") - print Solution().exist(board, "SFCS") - print Solution().exist(board, "ABCB") + print(Solution().exist(board, "ABCCED")) + print(Solution().exist(board, "SFCS")) + print(Solution().exist(board, "ABCB")) diff --git a/Python/zigzag-conversion.py b/Python/zigzag-conversion.py index 5a1d04cee..aec60f9b0 100644 --- a/Python/zigzag-conversion.py +++ b/Python/zigzag-conversion.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Time: O(n) # Space: O(1) # @@ -32,4 +33,4 @@ def convert(self, s, numRows): return zigzag if __name__ == "__main__": - print Solution().convert("PAYPALISHIRING", 3) + print(Solution().convert("PAYPALISHIRING", 3))