Skip to content

Commit

Permalink
3/28
Browse files Browse the repository at this point in the history
  • Loading branch information
Galaxy-Husky committed Mar 27, 2020
1 parent a2a3ff9 commit 3592bb1
Show file tree
Hide file tree
Showing 27 changed files with 800 additions and 33 deletions.
31 changes: 30 additions & 1 deletion 104.二叉树的最大深度.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,38 @@

class Solution:
def maxDepth(self, root: TreeNode) -> int:
# treee_depth = max(left_subtree, right_subtree) + 1
# DFS 递归
if not root:
return 0
return max(self.maxDepth(root.left), self.maxDepth(root.right)) + 1

""" # DFS 非递归 栈
cur, stack, depth, max_depth = root, [], 0, 0
while cur or stack:
while cur:
depth += 1
stack.append((cur, depth))
cur = cur.left
top, depth = stack.pop()
if depth > max_depth:
max_depth = depth
cur = top.right
return max_depth """

""" # BFS 迭代 队列
if not root:
return 0
depth = 0
q = [root]
while q:
depth += 1
for i in range(len(q)):
node = q.pop(0)
if node.left:
q.append(node.left)
if node.right:
q.append(node.right)
return depth """

# @lc code=end

78 changes: 78 additions & 0 deletions 110.平衡二叉树.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#
# @lc app=leetcode.cn id=110 lang=python3
#
# [110] 平衡二叉树
#
# https://leetcode-cn.com/problems/balanced-binary-tree/description/
#
# algorithms
# Easy (50.83%)
# Likes: 265
# Dislikes: 0
# Total Accepted: 60.4K
# Total Submissions: 118.9K
# Testcase Example: '[3,9,20,null,null,15,7]'
#
# 给定一个二叉树,判断它是否是高度平衡的二叉树。
#
# 本题中,一棵高度平衡二叉树定义为:
#
#
# 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1。
#
#
# 示例 1:
#
# 给定二叉树 [3,9,20,null,null,15,7]
#
# ⁠ 3
# ⁠ / \
# ⁠ 9 20
# ⁠ / \
# ⁠ 15 7
#
# 返回 true 。
#
# 示例 2:
#
# 给定二叉树 [1,2,2,3,3,null,null,4,4]
#
# ⁠ 1
# ⁠ / \
# ⁠ 2 2
# ⁠ / \
# ⁠ 3 3
# ⁠ / \
# ⁠4 4
#
#
# 返回 false 。
#
#
#
#

# @lc code=start
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None

class Solution:
def isBalanced(self, root: TreeNode) -> bool:
# 自底向上 DFS
def dfs(root):
if not root:
return 0
left = dfs(root.left)
if left == -1:
return -1
right = dfs(root.right)
if right == -1:
return -1
return max(left, right) + 1 if abs(left - right) <= 1 else -1
return dfs(root) != -1
# @lc code=end

113 changes: 113 additions & 0 deletions 151.翻转字符串里的单词.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#
# @lc app=leetcode.cn id=151 lang=python3
#
# [151] 翻转字符串里的单词
#
# https://leetcode-cn.com/problems/reverse-words-in-a-string/description/
#
# algorithms
# Medium (36.73%)
# Likes: 114
# Dislikes: 0
# Total Accepted: 36.1K
# Total Submissions: 97.9K
# Testcase Example: '"the sky is blue"'
#
# 给定一个字符串,逐个翻转字符串中的每个单词。
#
#
#
# 示例 1:
#
# 输入: "the sky is blue"
# 输出: "blue is sky the"
#
#
# 示例 2:
#
# 输入: "  hello world!  "
# 输出: "world! hello"
# 解释: 输入字符串可以在前面或者后面包含多余的空格,但是反转后的字符不能包括。
#
#
# 示例 3:
#
# 输入: "a good   example"
# 输出: "example good a"
# 解释: 如果两个单词间有多余的空格,将反转后单词间的空格减少到只含一个。
#
#
#
#
# 说明:
#
#
# 无空格字符构成一个单词。
# 输入字符串可以在前面或者后面包含多余的空格,但是反转后的字符不能包括。
# 如果两个单词间有多余的空格,将反转后单词间的空格减少到只含一个。
#
#
#
#
# 进阶:
#
# 请选用 C 语言的用户尝试使用 O(1) 额外空间复杂度的原地解法。
#
#

# @lc code=start
class Solution:
def reverseWords(self, s: str) -> str:
""" #分词 反转 拼接
return ' '.join(reversed(s.split())) """
# 反转字符串 反转单词 去除空格
def reverse_sentence(arr, i, j):
while i < j:
arr[i], arr[j] = arr[j], arr[i]
i += 1
j -= 1
return arr

def reverse_word(arr):
i = j = 0
n = len(arr)
while i < n:
while i < n and arr[i] == ' ':
i += 1
j = i
while j < n and arr[j] != ' ':
j += 1
reverse_sentence(arr, i, j-1)
i = j
return arr

def trim_spaces(arr):
n = len(arr)
i, j = 0, n-1
while i < j and arr[i] == ' ':
i += 1
while i < j and arr[j] == ' ':
j -= 1
return arr[i:j+1]

def remove_spaces(arr):
if ''.join(arr) == ' ':
return []
res = [arr[0]]
for i in range(1, len(arr)):
if arr[i] == ' ' and res[-1] == ' ':
continue
res.append(arr[i])
return res

if not s:
return ''
arr = list(s)
arr = reverse_sentence(arr, 0, len(arr)-1)
arr = reverse_word(arr)
arr = trim_spaces(arr)
res = remove_spaces(arr)
return ''.join(res)

# @lc code=end

56 changes: 54 additions & 2 deletions 189.旋转数组.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,44 @@
#
# [189] 旋转数组
#
# https://leetcode-cn.com/problems/rotate-array/description/
#
# algorithms
# Easy (40.38%)
# Likes: 483
# Dislikes: 0
# Total Accepted: 96.6K
# Total Submissions: 239.1K
# Testcase Example: '[1,2,3,4,5,6,7]\n3'
#
# 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数。
#
# 示例 1:
#
# 输入: [1,2,3,4,5,6,7] 和 k = 3
# 输出: [5,6,7,1,2,3,4]
# 解释:
# 向右旋转 1 步: [7,1,2,3,4,5,6]
# 向右旋转 2 步: [6,7,1,2,3,4,5]
# 向右旋转 3 步: [5,6,7,1,2,3,4]
#
#
# 示例 2:
#
# 输入: [-1,-100,3,99] 和 k = 2
# 输出: [3,99,-1,-100]
# 解释:
# 向右旋转 1 步: [99,-1,-100,3]
# 向右旋转 2 步: [3,99,-1,-100]
#
# 说明:
#
#
# 尽可能想出更多的解决方案,至少有三种不同的方法可以解决这个问题。
# 要求使用空间复杂度为 O(1) 的 原地 算法。
#
#
#

# @lc code=start
class Solution:
Expand All @@ -11,17 +49,31 @@ def rotate(self, nums: List[int], k: int) -> None:
Do not return anything, modify nums in-place instead.
"""
# 三次反转
n = len(nums)
k %= n

def swap(i, j, nums):
while i < j:
nums[i], nums[j] = nums[j], nums[i]
i += 1
j -= 1

n = len(nums)
k %= n
swap(0, n-1, nums)
swap(0, k-1, nums)
swap(k, n-1, nums)

""" nums[:] = nums[::-1]
nums[:k] = nums[:k][::-1]
nums[k:] = nums[k:][::-1] """

""" # 切片
nums[:] = nums[-k:] + nums[:-k] """

""" # 插入
for _ in range(k):
nums.insert(0, nums.pop()) """

# 环状替换

# @lc code=end

72 changes: 72 additions & 0 deletions 236.二叉树的最近公共祖先.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#
# @lc app=leetcode.cn id=236 lang=python3
#
# [236] 二叉树的最近公共祖先
#
# https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-tree/description/
#
# algorithms
# Medium (60.60%)
# Likes: 424
# Dislikes: 0
# Total Accepted: 54.5K
# Total Submissions: 89.6K
# Testcase Example: '[3,5,1,6,2,0,8,null,null,7,4]\n5\n1'
#
# 给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。
#
# 百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,满足 x 是 p、q 的祖先且 x
# 的深度尽可能大(一个节点也可以是它自己的祖先)。”
#
# 例如,给定如下二叉树:  root = [3,5,1,6,2,0,8,null,null,7,4]
#
#
#
#
#
# 示例 1:
#
# 输入: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
# 输出: 3
# 解释: 节点 5 和节点 1 的最近公共祖先是节点 3。
#
#
# 示例 2:
#
# 输入: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
# 输出: 5
# 解释: 节点 5 和节点 4 的最近公共祖先是节点 5。因为根据定义最近公共祖先节点可以为节点本身。
#
#
#
#
# 说明:
#
#
# 所有节点的值都是唯一的。
# p、q 为不同节点且均存在于给定的二叉树中。
#
#
#

# @lc code=start
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None

class Solution:
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
# 递归
if not root or root == p or root == q:
return root
left = self.lowestCommonAncestor(root.left, p, q)
right = self.lowestCommonAncestor(root.right, p, q)
if left and right:
return root
return left if left else right

# @lc code=end

Loading

0 comments on commit 3592bb1

Please sign in to comment.