Skip to content

第二周作业#26 #110

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 4 commits into from
Jun 24, 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
3 changes: 3 additions & 0 deletions Week_01/id_26/LeetCode_15_26.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ def threeSum(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
思路:
1) 防止重复结果,所以先排序,遍历时跳过相邻的重复值
2) 二分查找,大于0则右测向左移动,小于0则左边向右移动
"""
nums.sort()
ret, n = [], len(nums)
Expand Down
3 changes: 1 addition & 2 deletions Week_01/id_26/LeetCode_26_26.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ def removeDuplicates(self, nums):
"""
k = 1
for n in nums:
if n != nums[k-1]:
if n != nums[k - 1]:
nums[k] = n
k += 1
return k

66 changes: 66 additions & 0 deletions Week_02/id_26/LeetCode_101_26.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#
# @lc app=leetcode.cn id=101 lang=python
#
# [101] 对称二叉树
#
# https://leetcode-cn.com/problems/symmetric-tree/description/
#
# algorithms
# Easy (46.65%)
# Likes: 351
# Dislikes: 0
# Total Accepted: 34.7K
# Total Submissions: 74.4K
# Testcase Example: '[1,2,2,3,4,4,3]'
#
# 给定一个二叉树,检查它是否是镜像对称的。
#
# 例如,二叉树 [1,2,2,3,4,4,3] 是对称的。
#
# ⁠ 1
# ⁠ / \
# ⁠ 2 2
# ⁠/ \ / \
# 3 4 4 3
#
#
# 但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的:
#
# ⁠ 1
# ⁠ / \
# ⁠ 2 2
# ⁠ \ \
# ⁠ 3 3
#
#
# 说明:
#
# 如果你可以运用递归和迭代两种方法解决这个问题,会很加分。
#
#
# 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 node1.val != node2.val:
return False
return _isSymmetric(node1.left, node2.right) and _isSymmetric(
node1.right, node2.left)

return _isSymmetric(root, root)
65 changes: 65 additions & 0 deletions Week_02/id_26/LeetCode_102_26.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#
# @lc app=leetcode.cn id=102 lang=python
#
# [102] 二叉树的层次遍历
#
# https://leetcode-cn.com/problems/binary-tree-level-order-traversal/description/
#
# algorithms
# Medium (56.47%)
# Likes: 213
# Dislikes: 0
# Total Accepted: 28.4K
# Total Submissions: 50.2K
# Testcase Example: '[3,9,20,null,null,15,7]'
#
# 给定一个二叉树,返回其按层次遍历的节点值。 (即逐层地,从左到右访问所有节点)。
#
# 例如:
# 给定二叉树: [3,9,20,null,null,15,7],
#
# ⁠ 3
# ⁠ / \
# ⁠ 9 20
# ⁠ / \
# ⁠ 15 7
#
#
# 返回其层次遍历结果:
#
# [
# ⁠ [3],
# ⁠ [9,20],
# ⁠ [15,7]
# ]
#
#
#
# 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 levelOrder(self, root):
"""
:type root: TreeNode
:rtype: List[List[int]]
"""
if not root:
return []
ret, layer = [], [root]
while layer:
tmp_layer, line = [], []
for i in layer:
line.append(i.val)
if i.left:
tmp_layer.append(i.left)
if i.right:
tmp_layer.append(i.right)
layer = tmp_layer
ret.append(line)
return ret
68 changes: 68 additions & 0 deletions Week_02/id_26/LeetCode_103_26.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#
# @lc app=leetcode.cn id=103 lang=python
#
# [103] 二叉树的锯齿形层次遍历
#
# https://leetcode-cn.com/problems/binary-tree-zigzag-level-order-traversal/description/
#
# algorithms
# Medium (49.74%)
# Likes: 64
# Dislikes: 0
# Total Accepted: 11.7K
# Total Submissions: 23.5K
# Testcase Example: '[3,9,20,null,null,15,7]'
#
# 给定一个二叉树,返回其节点值的锯齿形层次遍历。(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行)。
#
# 例如:
# 给定二叉树 [3,9,20,null,null,15,7],
#
# ⁠ 3
# ⁠ / \
# ⁠ 9 20
# ⁠ / \
# ⁠ 15 7
#
#
# 返回锯齿形层次遍历如下:
#
# [
# ⁠ [3],
# ⁠ [20,9],
# ⁠ [15,7]
# ]
#
#
#
# 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 zigzagLevelOrder(self, root):
"""
思路:层序遍历,每隔一层换一个方向遍历
:type root: TreeNode
:rtype: List[List[int]]
"""
if not root:
return []
line, ret, rot = [root], [], 1
while line:
tmp1, tmp2 = [], []
for i in line:
if i.left:
tmp1.append(i.left)
if i.right:
tmp1.append(i.right)
for j in line[::rot]:
tmp2.append(j.val)
ret.append(tmp2)
line = tmp1
rot *= -1
return ret
55 changes: 55 additions & 0 deletions Week_02/id_26/LeetCode_111_26.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#
# @lc app=leetcode.cn id=111 lang=python
#
# [111] 二叉树的最小深度
#
# https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/description/
#
# algorithms
# Easy (38.70%)
# Likes: 125
# Dislikes: 0
# Total Accepted: 19K
# Total Submissions: 49K
# Testcase Example: '[3,9,20,null,null,15,7]'
#
# 给定一个二叉树,找出其最小深度。
#
# 最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
#
# 说明: 叶子节点是指没有子节点的节点。
#
# 示例:
#
# 给定二叉树 [3,9,20,null,null,15,7],
#
# ⁠ 3
# ⁠ / \
# ⁠ 9 20
# ⁠ / \
# ⁠ 15 7
#
# 返回它的最小深度  2.
#
#
# 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 not left or not right:
return 1 + left + right
return 1 + min(left, right)
43 changes: 43 additions & 0 deletions Week_02/id_26/LeetCode_1_26.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#
# @lc app=leetcode.cn id=1 lang=python
#
# [1] 两数之和
#
# https://leetcode-cn.com/problems/two-sum/description/
#
# algorithms
# Easy (46.10%)
# Likes: 5365
# Dislikes: 0
# Total Accepted: 397.2K
# Total Submissions: 861.4K
# Testcase Example: '[2,7,11,15]\n9'
#
# 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。
#
# 你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。
#
# 示例:
#
# 给定 nums = [2, 7, 11, 15], target = 9
#
# 因为 nums[0] + nums[1] = 2 + 7 = 9
# 所以返回 [0, 1]
#
#
#


class Solution(object):
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
s = {}
for i in range(len(nums)):
tmp = target - nums[i]
if tmp in s:
return [s[tmp], i]
s[nums[i]] = i
Loading