Skip to content

第一周作业#26 #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions Week_01/id_26/LeetCode_101_26.py
Original file line number Diff line number Diff line change
@@ -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)
23 changes: 23 additions & 0 deletions Week_01/id_26/LeetCode_1021_26.py
Original file line number Diff line number Diff line change
@@ -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('(()())(())(()(()))')
21 changes: 21 additions & 0 deletions Week_01/id_26/LeetCode_1047_26.py
Original file line number Diff line number Diff line change
@@ -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')
17 changes: 17 additions & 0 deletions Week_01/id_26/LeetCode_104_26.py
Original file line number Diff line number Diff line change
@@ -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))
21 changes: 21 additions & 0 deletions Week_01/id_26/LeetCode_111_26.py
Original file line number Diff line number Diff line change
@@ -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
29 changes: 29 additions & 0 deletions Week_01/id_26/LeetCode_15_26.py
Original file line number Diff line number Diff line change
@@ -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
23 changes: 23 additions & 0 deletions Week_01/id_26/LeetCode_174_26.py
Original file line number Diff line number Diff line change
@@ -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]])
27 changes: 27 additions & 0 deletions Week_01/id_26/LeetCode_189_26.py
Original file line number Diff line number Diff line change
@@ -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))
31 changes: 31 additions & 0 deletions Week_01/id_26/LeetCode_21_26.py
Original file line number Diff line number Diff line change
@@ -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
27 changes: 27 additions & 0 deletions Week_01/id_26/LeetCode_236_26.py
Original file line number Diff line number Diff line change
@@ -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
24 changes: 24 additions & 0 deletions Week_01/id_26/LeetCode_242_26.py
Original file line number Diff line number Diff line change
@@ -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
29 changes: 29 additions & 0 deletions Week_01/id_26/LeetCode_24_26.py
Original file line number Diff line number Diff line change
@@ -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
29 changes: 29 additions & 0 deletions Week_01/id_26/LeetCode_257_26.py
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions Week_01/id_26/LeetCode_26_26.py
Original file line number Diff line number Diff line change
@@ -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

42 changes: 42 additions & 0 deletions Week_01/id_26/LeetCode_42_26.py
Original file line number Diff line number Diff line change
@@ -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])
Loading