diff --git a/Week_01/id_26/LeetCode_101_26.py b/Week_01/id_26/LeetCode_101_26.py new file mode 100644 index 00000000..9ee2d4a7 --- /dev/null +++ b/Week_01/id_26/LeetCode_101_26.py @@ -0,0 +1,26 @@ +# Definition for a binary tree node. +class TreeNode(object): + def __init__(self, x): + self.val = x + self.left = None + self.right = None + + +class Solution(object): + def isSymmetric(self, root): + """ + :type root: TreeNode + :rtype: bool + """ + + def _isSymmetric(node1, node2): + if not node1 and not node2: + return True + if not node1 or not node2: + return False + if not node1.val == node2.val: + return False + return _isSymmetric(node1.left, node2.right) and _isSymmetric( + node1.right, node2.left) + + return _isSymmetric(root, root) diff --git a/Week_01/id_26/LeetCode_1021_26.py b/Week_01/id_26/LeetCode_1021_26.py new file mode 100644 index 00000000..e5cca86c --- /dev/null +++ b/Week_01/id_26/LeetCode_1021_26.py @@ -0,0 +1,23 @@ +class Solution(object): + def removeOuterParentheses(self, S): + """ + :type S: str + :rtype: str + """ + if not S: + return S + ret, t = '', 0 + for i in S: + if i == '(': + t += 1 + if t > 1: + ret += i + if i == ')': + t -= 1 + if t > 0: + ret += i + return ret + + +print Solution().removeDuplicates('(()())(())') +print Solution().removeDuplicates('(()())(())(()(()))') diff --git a/Week_01/id_26/LeetCode_1047_26.py b/Week_01/id_26/LeetCode_1047_26.py new file mode 100644 index 00000000..ec59a47a --- /dev/null +++ b/Week_01/id_26/LeetCode_1047_26.py @@ -0,0 +1,21 @@ +class Solution(object): + def removeDuplicates(self, S): + """ + :type S: str + :rtype: str + """ + if not S: + return S + stack = [] + for i in S: + if not stack: + stack.append(i) + continue + if stack[-1] == i: + stack.pop() + else: + stack.append(i) + return ''.join(stack) + + +# print Solution().removeDuplicates('adcc') diff --git a/Week_01/id_26/LeetCode_104_26.py b/Week_01/id_26/LeetCode_104_26.py new file mode 100644 index 00000000..99f00308 --- /dev/null +++ b/Week_01/id_26/LeetCode_104_26.py @@ -0,0 +1,17 @@ +# Definition for a binary tree node. +class TreeNode(object): + def __init__(self, x): + self.val = x + self.left = None + self.right = None + + +class Solution(object): + def maxDepth(self, root): + """ + :type root: TreeNode + :rtype: int + """ + if not root: + return 0 + return 1 + max(self.maxDepth(root.left), self.maxDepth(root.right)) diff --git a/Week_01/id_26/LeetCode_111_26.py b/Week_01/id_26/LeetCode_111_26.py new file mode 100644 index 00000000..04f5b19a --- /dev/null +++ b/Week_01/id_26/LeetCode_111_26.py @@ -0,0 +1,21 @@ +# Definition for a binary tree node. +# class TreeNode(object): +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + + +class Solution(object): + def minDepth(self, root): + """ + :type root: TreeNode + :rtype: int + """ + if not root: + return 0 + left = self.minDepth(root.left) + right = self.minDepth(root.right) + if left and right: + return 1 + min(left, right) + return 1 + left + right diff --git a/Week_01/id_26/LeetCode_15_26.py b/Week_01/id_26/LeetCode_15_26.py new file mode 100644 index 00000000..8eb97948 --- /dev/null +++ b/Week_01/id_26/LeetCode_15_26.py @@ -0,0 +1,29 @@ +class Solution(object): + def threeSum(self, nums): + """ + :type nums: List[int] + :rtype: List[List[int]] + """ + nums.sort() + ret, n = [], len(nums) + for i in range(n): + if i > 0 and nums[i] == nums[i - 1]: + continue + left = i + 1 + right = n - 1 + while left < right: + tmp = nums[i] + nums[left] + nums[right] + if tmp == 0: + s = [nums[i], nums[left], nums[right]] + ret.append(s) + while left < right and nums[left] == nums[left + 1]: + left += 1 + while left < right and nums[right] == nums[right - 1]: + right -= 1 + right -= 1 + left += 1 + elif tmp > 0: + right -= 1 + else: + left += 1 + return ret diff --git a/Week_01/id_26/LeetCode_174_26.py b/Week_01/id_26/LeetCode_174_26.py new file mode 100644 index 00000000..6af2701d --- /dev/null +++ b/Week_01/id_26/LeetCode_174_26.py @@ -0,0 +1,23 @@ +#!/usr/bin/python +# -*-coding:utf-8 -*- + + +class Solution(object): + def calculateMinimumHP(self, dungeon): + """ + :type dungeon: List[List[int]] + :rtype: int + """ + m = len(dungeon) + n = len(dungeon[0]) + dp = [[1 << 31] * (n + 1) for _ in range(m + 1)] + dp[m][n - 1], dp[m - 1][n] = 1, 1 + for i in range(m - 1, -1, -1): + for j in range(n - 1, -1, -1): + dp[i][j] = min(dp[i + 1][j], dp[i][j + 1]) - dungeon[i][j] + if dp[i][j] <= 0: + dp[i][j] = 1 + return dp[0][0] + + +print Solution().calculateMinimumHP([[-2, -3, 3], [-5, -10, 1], [10, 30, -5]]) diff --git a/Week_01/id_26/LeetCode_189_26.py b/Week_01/id_26/LeetCode_189_26.py new file mode 100644 index 00000000..14dcd5f0 --- /dev/null +++ b/Week_01/id_26/LeetCode_189_26.py @@ -0,0 +1,27 @@ +class Solution(object): + def rotate(self, nums, k): + """ + :type nums: List[int] + :type k: int + :rtype: None Do not return anything, modify nums in-place instead. + """ + if not nums: + return [] + k = k % len(nums) + + def reverse(start, end): + while end > start: + nums[start], nums[end] = nums[end], nums[start] + start += 1 + end -= 1 + + reverse(0, len(nums) - 1) + reverse(0, k - 1) + reverse(k, len(nums) - 1) + return nums + + +print(Solution().rotate([1, 2, 3, 4, 5, 6, 7], 3)) +print(Solution().rotate([], 4)) +print(Solution().rotate([-1], 2)) +print(Solution().rotate([1, 2, 3, 4, 5, 6], 2)) diff --git a/Week_01/id_26/LeetCode_21_26.py b/Week_01/id_26/LeetCode_21_26.py new file mode 100644 index 00000000..d1ccbb99 --- /dev/null +++ b/Week_01/id_26/LeetCode_21_26.py @@ -0,0 +1,31 @@ +# Definition for singly-linked list. +class ListNode(object): + def __init__(self, x): + self.val = x + self.next = None + + +class Solution(object): + def mergeTwoLists(self, l1, l2): + """ + :type l1: ListNode + :type l2: ListNode + :rtype: ListNode + """ + s = ListNode(0) + k = s + while l1 is not None or l2 is not None: + if l1 is None: + s.next = l2 + l2 = None + elif l2 is None: + s.next = l1 + l1 = None + elif l1.val <= l2.val: + s.next = ListNode(l1.val) + l1 = l1.next + else: + s.next = ListNode(l2.val) + l2 = l2.next + s = s.next + return k.next diff --git a/Week_01/id_26/LeetCode_236_26.py b/Week_01/id_26/LeetCode_236_26.py new file mode 100644 index 00000000..960af2c6 --- /dev/null +++ b/Week_01/id_26/LeetCode_236_26.py @@ -0,0 +1,27 @@ +# Definition for a binary tree node. +class TreeNode(object): + def __init__(self, x): + self.val = x + self.left = None + self.right = None + + +class Solution(object): + def lowestCommonAncestor(self, root, p, q): + """ + :type root: TreeNode + :type p: TreeNode + :type q: TreeNode + :rtype: TreeNode + """ + if not root: + return root + if root == p or root == q: + return root + left = self.lowestCommonAncestor(root.left, p, q) + right = self.lowestCommonAncestor(root.right, p, q) + if left is None: + return right + if right is None: + return left + return root diff --git a/Week_01/id_26/LeetCode_242_26.py b/Week_01/id_26/LeetCode_242_26.py new file mode 100644 index 00000000..a60e3004 --- /dev/null +++ b/Week_01/id_26/LeetCode_242_26.py @@ -0,0 +1,24 @@ +class Solution(object): + def isAnagram(self, s, t): + """ + :type s: str + :type t: str + :rtype: bool + """ + if len(s) != len(t): + return False + m = {} + for i in s: + if m.get(i) is None: + m[i] = 1 + else: + m[i] += 1 + for i in t: + if m.get(i) is None: + return False + else: + m[i] -= 1 + for i in m.values(): + if not i == 0: + return False + return True diff --git a/Week_01/id_26/LeetCode_24_26.py b/Week_01/id_26/LeetCode_24_26.py new file mode 100644 index 00000000..26ca5cda --- /dev/null +++ b/Week_01/id_26/LeetCode_24_26.py @@ -0,0 +1,29 @@ +# Definition for singly-linked list. +class ListNode(object): + def __init__(self, x): + self.val = x + self.next = None + + +class Solution(object): + def swapPairs(self, head): + """ + :type head: ListNode + :rtype: ListNode + """ + if head is None: + return + if head.next is None: + return head + ret = ListNode(0) + p = ret + while head: + if head.next: + p.next = head.next + head.next = head.next.next + p.next.next = head + p = p.next.next + head = head.next + else: + head = head.next + return ret.next \ No newline at end of file diff --git a/Week_01/id_26/LeetCode_257_26.py b/Week_01/id_26/LeetCode_257_26.py new file mode 100644 index 00000000..0d26b636 --- /dev/null +++ b/Week_01/id_26/LeetCode_257_26.py @@ -0,0 +1,29 @@ +# Definition for a binary tree node. +# class TreeNode(object): +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + + +class Solution(object): + def binaryTreePaths(self, root): + """ + :type root: TreeNode + :rtype: List[str] + """ + paths = [] + + def dfs(root, path=[]): + if not root: + return + path = path + [str(root.val)] + if not root.left and not root.right: + return paths.append('->'.join(path)) + if root.left: + dfs(root.left, path) + if root.right: + dfs(root.right, path) + + dfs(root) + return paths diff --git a/Week_01/id_26/LeetCode_26_26.py b/Week_01/id_26/LeetCode_26_26.py new file mode 100644 index 00000000..d3d2161c --- /dev/null +++ b/Week_01/id_26/LeetCode_26_26.py @@ -0,0 +1,13 @@ +class Solution(object): + def removeDuplicates(self, nums): + """ + :type nums: List[int] + :rtype: int + """ + k = 1 + for n in nums: + if n != nums[k-1]: + nums[k] = n + k += 1 + return k + \ No newline at end of file diff --git a/Week_01/id_26/LeetCode_42_26.py b/Week_01/id_26/LeetCode_42_26.py new file mode 100644 index 00000000..f9c44241 --- /dev/null +++ b/Week_01/id_26/LeetCode_42_26.py @@ -0,0 +1,42 @@ +class Solution(object): + def trap(self, height): + """ + :type height: List[int] + :rtype: int + """ + i, j = 0, len(height) - 1 + ret, left_max, right_max = 0, 0, 0 + while i <= j: + left_max = max(left_max, height[i]) + right_max = max(right_max, height[j]) + if left_max < right_max: + ret += (left_max - height[i]) + i += 1 + else: + ret += (right_max - height[j]) + j -= 1 + return ret + + def trap2(self, height): + if not height: + return 0 + + max_height = height.index(max(height)) + ret = 0 + + max_baffle = 0 + for i in range(1, max_height): + max_baffle = max(height[i - 1], max_baffle) + if height[i] < max_baffle: + ret += max_baffle - height[i] + + max_baffle = 0 + for i in range(len(height) - 2, max_height, -1): + max_baffle = max(height[i + 1], max_baffle) + if height[i] < max_baffle: + ret += max_baffle - height[i] + return ret + + +print Solution().trap([0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]) +print Solution().trap([5, 2, 1, 2, 1, 5]) diff --git a/Week_01/id_26/LeetCode_441_26.py b/Week_01/id_26/LeetCode_441_26.py new file mode 100644 index 00000000..94a2967b --- /dev/null +++ b/Week_01/id_26/LeetCode_441_26.py @@ -0,0 +1,20 @@ +class Solution(object): + def arrangeCoins(self, n): + """ + :type n: int + :rtype: int + """ + left, right = 0, n + while left <= right: + mid = (right + left) / 2 + k = mid * (mid + 1) + if k <= 2 * n: + left = mid + 1 + else: + right = mid - 1 + return right + + +print Solution().arrangeCoins(1) +# print Solution().arrangeCoins(5) +# print Solution().arrangeCoins(8) diff --git a/Week_01/id_26/LeetCode_49_26.py b/Week_01/id_26/LeetCode_49_26.py new file mode 100644 index 00000000..c22bc612 --- /dev/null +++ b/Week_01/id_26/LeetCode_49_26.py @@ -0,0 +1,18 @@ +class Solution(object): + def groupAnagrams(self, strs): + """ + :type strs: List[str] + :rtype: List[List[str]] + """ + if not strs: + return [] + ret = {} + for s in strs: + key = "".join(sorted(s)) + if key not in ret: + ret[key] = [] + ret[key].append(s) + return ret.values() + + +print Solution().groupAnagrams(["eat", "tea", "tan", "ate", "nat", "bat"]) diff --git a/Week_01/id_26/LeetCode_50_26.py b/Week_01/id_26/LeetCode_50_26.py new file mode 100644 index 00000000..32f50e00 --- /dev/null +++ b/Week_01/id_26/LeetCode_50_26.py @@ -0,0 +1,26 @@ +class Solution(object): + def myPow(self, x, n): + """ + :type x: float + :type n: int + :rtype: float + """ + + if n == 0: + return 1 + if n == 1: + return x + if n < 0: + return 1.0 / self.myPow(x, -n) + + t = self.myPow(x, n / 2) + if n & 1 == 0: + return t * t + else: + return x * t * t + + +print Solution().myPow(2.00000, 10) +print Solution().myPow(2.10000, 3) +print Solution().myPow(2.00000, -2) +print Solution().myPow(2.00000, -2147483648) diff --git a/Week_01/id_26/LeetCode_783_26.py b/Week_01/id_26/LeetCode_783_26.py new file mode 100644 index 00000000..c1666ffd --- /dev/null +++ b/Week_01/id_26/LeetCode_783_26.py @@ -0,0 +1,28 @@ +# Definition for a binary tree node. +# class TreeNode(object): +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + + +class Solution(object): + def minDiffInBST(self, root): + """ + :type root: TreeNode + :rtype: int + """ + self.ret = self.preVal = 1 << 31 + + def dfs(node): + if self.ret == 1: + return + if not node: + return + dfs(node.left) + self.ret = min(abs(node.val - self.preVal), self.ret) + self.preVal = node.val + dfs(node.right) + + dfs(root) + return self.ret diff --git a/Week_01/id_26/LeetCode_84_26.py b/Week_01/id_26/LeetCode_84_26.py new file mode 100644 index 00000000..7900cb6f --- /dev/null +++ b/Week_01/id_26/LeetCode_84_26.py @@ -0,0 +1,23 @@ +class Solution(object): + def largestRectangleArea(self, heights): + """ + :type heights: List[int] + :rtype: int + """ + + heights.append(0) + ret = 0 + stack = [-1] + for i in xrange(len(heights)): + while heights[i] < heights[stack[-1]]: + h = heights[stack.pop()] + w = i - stack[-1] - 1 + ret = max(ret, h * w) + stack.append(i) + return ret + + +# print Solution().largestRectangleArea([1]) +print Solution().largestRectangleArea([1, 1]) +print Solution().largestRectangleArea([2, 1, 5, 6, 2, 3]) +print Solution().largestRectangleArea([2, 1, 2]) diff --git a/Week_01/id_26/LeetCode_88_26.py b/Week_01/id_26/LeetCode_88_26.py new file mode 100644 index 00000000..0df23fce --- /dev/null +++ b/Week_01/id_26/LeetCode_88_26.py @@ -0,0 +1,23 @@ +class Solution(object): + def merge(self, nums1, m, nums2, n): + """ + :type nums1: List[int] + :type m: int + :type nums2: List[int] + :type n: int + :rtype: None Do not return anything, modify nums1 in-place instead. + """ + i, j, k = m - 1, n - 1, m + n - 1 + while j >= 0: + if i >= 0 and nums1[i] > nums2[j]: + nums1[k] = nums1[i] + i -= 1 + else: + nums1[k] = nums2[j] + j -= 1 + k -= 1 + return nums1 + + +print Solution().merge([2, 2, 3, 0, 0, 0], 3, [2, 5, 6], 3) +print Solution().merge([2, 2, 3, 0, 0, 0, 0], 3, [1, 2, 5, 6], 4) diff --git a/Week_01/id_26/LeetCode_938_26.py b/Week_01/id_26/LeetCode_938_26.py new file mode 100644 index 00000000..bc6cdf73 --- /dev/null +++ b/Week_01/id_26/LeetCode_938_26.py @@ -0,0 +1,50 @@ +# Definition for a binary tree node. +# class TreeNode(object): +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + + +class Solution(object): + def rangeSumBST(self, root, L, R): + """ + :type root: TreeNode + :type L: int + :type R: int + :rtype: int + """ + self.ret = 0 + + def bst(root, L, R): + if not root: + return + bst(root.left, L, R) + if root.val <= R and root.val >= L: + self.ret += root.val + bst(root.right, L, R) + + bst(root, L, R) + return self.ret + + def rangeSumBST2(self, root, L, R): + """ + :type root: TreeNode + :type L: int + :type R: int + :rtype: int + """ + + def dfs(node): + if not node: + return + if L <= node.val <= R: + self.ans += node.val + if L < node.val: + dfs(node.left) + if node.val < R: + dfs(node.right) + + self.ans = 0 + dfs(root) + return self.ans