From 6aa2d8e276d0db731ee1e9319f596ecb0cb8055e Mon Sep 17 00:00:00 2001 From: SunnyQjm Date: Mon, 29 Jun 2020 11:58:15 +0800 Subject: [PATCH] update --- chapter10/2_balanced-binary-tree.py | 2 +- chapter3/13_sliding-window-maximum.py | 11 +++++++++++ chapter4/1_search-insert-position.py | 2 +- chapter4/3_remove-duplicates-from-sorted-list.py | 2 +- chapter5/4_pow.py | 3 ++- chapter7/5_longest-increasing-subsequence.py | 2 +- chapter9/4_same-tree.py | 6 +++--- 7 files changed, 20 insertions(+), 8 deletions(-) diff --git a/chapter10/2_balanced-binary-tree.py b/chapter10/2_balanced-binary-tree.py index c7ad30b..c122c68 100644 --- a/chapter10/2_balanced-binary-tree.py +++ b/chapter10/2_balanced-binary-tree.py @@ -28,7 +28,7 @@ # 3 3 # / \ # 4 4 -# 返回 false 。 +# 返回 false。 ####################################################################################### diff --git a/chapter3/13_sliding-window-maximum.py b/chapter3/13_sliding-window-maximum.py index d011901..05c58b7 100644 --- a/chapter3/13_sliding-window-maximum.py +++ b/chapter3/13_sliding-window-maximum.py @@ -106,6 +106,17 @@ class Solution: return res def maxSlidingWindow2(self, nums, k): + """ + 思路: + 1. 用一个双向队列来存储处于窗口内的索引值,该队列有如下性质: + - 队列中的元素不会超过窗口大小k + - 保证队头元素对应的数一定是当前队列里面最大的值 + 2. 遍历所有的数,每轮执行如下流程: + - 如果当前队列中元素已经有k个了,则首先左出队一个元素; + - 接着将队列中比当前元素小的数都右出队; + - 将当前索引加到队列当中; + - 如果当前索引大于等于k-1,则取队头元素加入到结果集当中 + """ if not nums: return [] # window是一个双向队列 diff --git a/chapter4/1_search-insert-position.py b/chapter4/1_search-insert-position.py index 2a97ff6..a7127c6 100644 --- a/chapter4/1_search-insert-position.py +++ b/chapter4/1_search-insert-position.py @@ -36,7 +36,7 @@ class Solution: 思路: 1. 用二分查找试图找到目标值; - 2. 找到则返回其索引,没找到则其可能被插入的位置 + 2. 找到则返回其索引,没找到则返回其可能被插入的位置 """ left, right = 0, len(nums) - 1 while left + 1 < right: diff --git a/chapter4/3_remove-duplicates-from-sorted-list.py b/chapter4/3_remove-duplicates-from-sorted-list.py index 4bdf6cb..79be4da 100644 --- a/chapter4/3_remove-duplicates-from-sorted-list.py +++ b/chapter4/3_remove-duplicates-from-sorted-list.py @@ -45,7 +45,7 @@ class Solution: if cur.val == pre.val: pre.next = cur.next else: - last, pre = cur.val, cur + pre = cur cur = cur.next return head diff --git a/chapter5/4_pow.py b/chapter5/4_pow.py index 3c97f89..67312ba 100644 --- a/chapter5/4_pow.py +++ b/chapter5/4_pow.py @@ -25,6 +25,7 @@ # n 是 32 位有符号整数,其数值范围是 [−231, 231 − 1] 。 ################################################################################### + class Solution: def myPow(self, x, n): """ @@ -55,7 +56,7 @@ class Solution: return x * self.myPow(x, n - 1) # 处理n为偶数的情况 - return self.myPow(x * x, n / 2) + return self.myPow(x * x, n // 2) if __name__ == '__main__': diff --git a/chapter7/5_longest-increasing-subsequence.py b/chapter7/5_longest-increasing-subsequence.py index 3664d90..9dff0c6 100644 --- a/chapter7/5_longest-increasing-subsequence.py +++ b/chapter7/5_longest-increasing-subsequence.py @@ -30,7 +30,7 @@ class Solution: 思路: nums => size = n 1. 采用动态规划的思想; - 2. dp[i] => 表示[i:n-1]范围内的最长的上升子序列的长度; + 2. dp[i] => 表示[i:n-1]范围内包含nums[i]的最长的上升子序列的长度; 3. 状态转移方程: f(i) = 1 i == n - 1 1 + max{f(j) | nums[j] > nums[i] && i < j < n} i < n - 1 diff --git a/chapter9/4_same-tree.py b/chapter9/4_same-tree.py index ad5af5c..67a6129 100644 --- a/chapter9/4_same-tree.py +++ b/chapter9/4_same-tree.py @@ -8,7 +8,7 @@ # 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。 # # 示例 1: -# 输入: 1 1 +# 输入: 1 1 # / \ / \ # 2 3 2 3 # @@ -17,7 +17,7 @@ # 输出: true # # 示例 2: -# 输入: 1 1 +# 输入: 1 1 # / \ # 2 2 # @@ -26,7 +26,7 @@ # 输出: false # # 示例 3: -# 输入: 1 1 +# 输入: 1 1 # / \ / \ # 2 1 1 2 #