Skip to content
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# vscode
.vscode/
.idea/
86 changes: 86 additions & 0 deletions Week_02/id_36/LeetCode_101_36.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#
# @lc app=leetcode.cn id=101 lang=python3
#
# [101] 对称二叉树
#
# https://leetcode-cn.com/problems/symmetric-tree/description/
#
# algorithms
# Easy (46.79%)
# Likes: 357
# Dislikes: 0
# Total Accepted: 36K
# Total Submissions: 77K
# 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:
def __init__(self, x):
self.val = x
self.left = None
self.right = None

# # 递归
# class Solution:
# def isSymmetric(self, root: TreeNode) -> bool:
# return self.isMirror(root, root)

# def isMirror(self, root1: TreeNode, root2: TreeNode) -> bool:
# if root1 is None and root2 is None:
# return True
# if root1 is None or root2 is None:
# return False

# return root1.val == root2.val and self.isMirror(root1.right, root2.left) and self.isMirror(root1.left, root2.right)

# 迭代
class Solution:
def isSymmetric(self, root: TreeNode) -> bool:
queue = []
queue.append(root)
queue.append(root)

while len(queue) > 0:
t1 = queue.pop()
t2 = queue.pop()
if t1 is None and t2 is None:
continue
if t1 is None or t2 is None:
return False
if t1.val != t2.val:
return False

queue.append(t1.left)
queue.append(t2.right)
queue.append(t1.right)
queue.append(t2.left)

return True

73 changes: 73 additions & 0 deletions Week_02/id_36/LeetCode_111_36.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#
# @lc app=leetcode.cn id=111 lang=python3
#
# [111] 二叉树的最小深度
#
# Definition for a binary tree node.


class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None


# #递归
# class Solution:
# def minDepth(self, root: TreeNode) -> int:
# if root is None:
# return 0

# if root.left is None and root.right is not None:
# return self.minDepth(root.right) + 1
# elif root.left is not None and root.right is None:
# return self.minDepth(root.left) + 1

# return min(self.minDepth(root.left), self.minDepth(root.right)) + 1

# #深度优先搜索迭代
# class Solution:
# def minDepth(self, root: TreeNode) -> int:
# if root is None:
# return 0

# stack = []
# import sys
# stack.append((root, 1))
# min_depth = sys.maxsize
# while len(stack) > 0:
# current = stack.pop()
# root = current[0]
# current_depth = current[1]
# if root.left is None and root.right is None:
# min_depth = min(min_depth, current_depth)
# if root.left is not None:
# stack.append((root.left, current_depth + 1))
# if root.right is not None:
# stack.append((root.right, current_depth + 1))

# return min_depth

#宽度优先搜索迭代
class Solution:
def minDepth(self, root: TreeNode) -> int:
if root is None:
return 0

stack = []
stack.append((root, 1))
current_depth = 0
while len(stack) > 0:
current = stack.pop()
root = current[0]
current_depth = current[1]
if root.left is None and root.right is None:
break
if root.left is not None:
stack.insert(0, (root.left, current_depth + 1))
if root.right is not None:
stack.insert(0, (root.right, current_depth + 1))

return current_depth

62 changes: 62 additions & 0 deletions Week_02/id_36/LeetCode_1_36.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#
# @lc app=leetcode.cn id=1 lang=python3
#
# [1] 两数之和
#
# https://leetcode-cn.com/problems/two-sum/description/
#
# algorithms
# Easy (46.08%)
# 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:
# def twoSum(self, nums: List[int], target: int) -> List[int]:
# l = len(nums)
# for i in range(l):
# for j in range(i + 1, l):
# if nums[j] == target - nums[i]:
# return [i, j]
# return []
#
# 两遍哈希表
# class Solution:
# def twoSum(self, nums: List[int], target: int) -> List[int]:
# map = dict()
# l = len(nums)
# for i in range(l):
# map[nums[i]] = i
# for i in range(l):
# r = target - nums[i]
# if map.get(r, i) != i:
# return [i, map.get(r)]
# return []
#
# 一遍哈希表
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
map = dict()
l = len(nums)
for i in range(l):
r = target - nums[i]
if map.get(r, i) != i:
return [map.get(r), i]
map[nums[i]] = i
return []

77 changes: 77 additions & 0 deletions Week_02/id_36/LeetCode_236_36.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#
# @lc app=leetcode.cn id=236 lang=python3
#
# [236] 二叉树的最近公共祖先
#
# https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-tree/description/
#
# algorithms
# Medium (55.09%)
# Likes: 183
# Dislikes: 0
# Total Accepted: 15K
# Total Submissions: 27.3K
# 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 为不同节点且均存在于给定的二叉树中。
#
#
#
# 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 root == p or root == q or root is None:
return root

left = self.lowestCommonAncestor(root.left, p, q)
right = self.lowestCommonAncestor(root.right, p, q)

if left is None and right is None:
return None
elif left is not None and right is None:
return left
elif left is None and right is not None:
return right

return root

77 changes: 77 additions & 0 deletions Week_02/id_36/LeetCode_3_36.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#
# @lc app=leetcode.cn id=3 lang=python3
#
# [3] 无重复字符的最长子串
#
# https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/description/
#
# algorithms
# Medium (29.74%)
# Likes: 1907
# Dislikes: 0
# Total Accepted: 136.6K
# Total Submissions: 457.9K
# Testcase Example: '"abcabcbb"'
#
# 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。
#
# 示例 1:
#
# 输入: "abcabcbb"
# 输出: 3
# 解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。
#
#
# 示例 2:
#
# 输入: "bbbbb"
# 输出: 1
# 解释: 因为无重复字符的最长子串是 "b",所以其长度为 1。
#
#
# 示例 3:
#
# 输入: "pwwkew"
# 输出: 3
# 解释: 因为无重复字符的最长子串是 "wke",所以其长度为 3。
# 请注意,你的答案必须是 子串 的长度,"pwke" 是一个子序列,不是子串。
#
#
#
# class Solution:
# def lengthOfLongestSubstring(self, s: str) -> int:
# n = len(s)
# ans = 0

# for i in range(n):
# for j in range(i + 1, n + 1):
# if self.allUnique(s, i, j):
# ans = max(ans, j - i)

# return ans

# def allUnique(self, s, start, end):
# set_v = set()
# for i in range(start, end):
# ch = s[i]
# if ch in set_v:
# return False
# set_v.add(ch)

# return True


class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
n = len(s)
ans = 0
start = 0
dict_v = dict()
for end in range(n):
ch = s[end]
if dict_v.get(ch, None):
start = max(dict_v.get(ch), start)
ans = max(ans, end - start + 1)
dict_v[ch] = end + 1

return ans
Loading