-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a2a3ff9
commit 3592bb1
Showing
27 changed files
with
800 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
Oops, something went wrong.