From 3d64ead78d6e865a994a47df38c362a0db0636d9 Mon Sep 17 00:00:00 2001 From: fw_qaq Date: Fri, 10 Mar 2023 14:52:24 +0800 Subject: [PATCH 1/9] =?UTF-8?q?Update=200055.=E8=B7=B3=E8=B7=83=E6=B8=B8?= =?UTF-8?q?=E6=88=8F.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0055.跳跃游戏.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/problems/0055.跳跃游戏.md b/problems/0055.跳跃游戏.md index a898263d..6f6dec26 100644 --- a/problems/0055.跳跃游戏.md +++ b/problems/0055.跳跃游戏.md @@ -178,16 +178,16 @@ var canJump = function(nums) { ```Rust impl Solution { - fn max(a: usize, b: usize) -> usize { - if a > b { a } else { b } - } pub fn can_jump(nums: Vec) -> bool { - let mut cover = 0; - if (nums.len() == 1) { return true; } - let mut i = 0; + if nums.len() == 1 { + return true; + } + let (mut i, mut cover) = (0, 0); while i <= cover { - cover = Self::max(i + nums[i] as usize, cover); - if cover >= nums.len() - 1 { return true; } + cover = (i + nums[i] as usize).max(cover); + if cover >= nums.len() - 1 { + return true; + } i += 1; } false From 2175be827dbe4215f3ccf55048e62b158a936a0e Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Thu, 16 Mar 2023 22:24:32 +0800 Subject: [PATCH 2/9] =?UTF-8?q?update=200242.=E6=9C=89=E6=95=88=E7=9A=84?= =?UTF-8?q?=E5=AD=97=E6=AF=8D=E5=BC=82=E4=BD=8D=E8=AF=8D:=20=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E5=A4=8D=E6=9D=82=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0242.有效的字母异位词.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/problems/0242.有效的字母异位词.md b/problems/0242.有效的字母异位词.md index 9f84a5cd..1006ea35 100644 --- a/problems/0242.有效的字母异位词.md +++ b/problems/0242.有效的字母异位词.md @@ -85,6 +85,9 @@ public: }; ``` +* 时间复杂度: O(n) +* 空间复杂度: O(1) + ## 其他语言版本 From 8a7432ef2174ce8864f3e79816df2399815cd28d Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Sat, 18 Mar 2023 23:49:44 +0800 Subject: [PATCH 3/9] =?UTF-8?q?update=200349.=E4=B8=A4=E4=B8=AA=E6=95=B0?= =?UTF-8?q?=E7=BB=84=E7=9A=84=E4=BA=A4=E9=9B=86:=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=A4=8D=E6=9D=82=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0349.两个数组的交集.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/problems/0349.两个数组的交集.md b/problems/0349.两个数组的交集.md index 905bf4b2..0da0e30c 100644 --- a/problems/0349.两个数组的交集.md +++ b/problems/0349.两个数组的交集.md @@ -72,6 +72,9 @@ public: }; ``` +* 时间复杂度: O(mn) +* 空间复杂度: O(n) + ## 拓展 那有同学可能问了,遇到哈希问题我直接都用set不就得了,用什么数组啊。 @@ -110,6 +113,8 @@ public: }; ``` +* 时间复杂度: O(m + n) +* 空间复杂度: O(n) ## 其他语言版本 From 46a34392d5308b9606436d6722bd43053da4da73 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Sun, 19 Mar 2023 00:02:30 +0800 Subject: [PATCH 4/9] =?UTF-8?q?update=200202.=E5=BF=AB=E4=B9=90=E6=95=B0?= =?UTF-8?q?=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D=E6=9D=82=E5=BA=A6=E5=88=86?= =?UTF-8?q?=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0202.快乐数.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/problems/0202.快乐数.md b/problems/0202.快乐数.md index 2687574f..5ac29e86 100644 --- a/problems/0202.快乐数.md +++ b/problems/0202.快乐数.md @@ -75,6 +75,8 @@ public: }; ``` +* 时间复杂度: O(logn) +* 空间复杂度: O(logn) From 3b79659bc0c3303ccf48219ff9398184b622b88c Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Sun, 19 Mar 2023 00:04:13 +0800 Subject: [PATCH 5/9] =?UTF-8?q?update=200001.=E4=B8=A4=E6=95=B0=E4=B9=8B?= =?UTF-8?q?=E5=92=8C=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D=E6=9D=82=E5=BA=A6?= =?UTF-8?q?=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0001.两数之和.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/problems/0001.两数之和.md b/problems/0001.两数之和.md index a0acdcbe..b3abb991 100644 --- a/problems/0001.两数之和.md +++ b/problems/0001.两数之和.md @@ -109,6 +109,9 @@ public: }; ``` +* 时间复杂度: O(n) +* 空间复杂度: O(n) + ## 总结 本题其实有四个重点: From 08a984d61c5b855b5052d8617dc23cf170d61c01 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Sun, 19 Mar 2023 11:13:34 +0800 Subject: [PATCH 6/9] =?UTF-8?q?update=200454.=E5=9B=9B=E6=95=B0=E7=9B=B8?= =?UTF-8?q?=E5=8A=A0II=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D=E6=9D=82?= =?UTF-8?q?=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0454.四数相加II.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/problems/0454.四数相加II.md b/problems/0454.四数相加II.md index 07c3f711..c2a5710f 100644 --- a/problems/0454.四数相加II.md +++ b/problems/0454.四数相加II.md @@ -83,6 +83,9 @@ public: ``` +* 时间复杂度: O(n^2) +* 空间复杂度: O(n^2),最坏情况下A和B的值各不相同,相加产生的数字个数为 n^2 + From c91ee8fdc2b7ff02953a758b0b9c7da4c031d7d0 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Sun, 19 Mar 2023 11:16:15 +0800 Subject: [PATCH 7/9] =?UTF-8?q?update=200383.=E8=B5=8E=E9=87=91=E4=BF=A1?= =?UTF-8?q?=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D=E6=9D=82=E5=BA=A6=E5=88=86?= =?UTF-8?q?=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0383.赎金信.md | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/problems/0383.赎金信.md b/problems/0383.赎金信.md index 4e6ba033..d9a184b6 100644 --- a/problems/0383.赎金信.md +++ b/problems/0383.赎金信.md @@ -39,8 +39,6 @@ canConstruct("aa", "aab") -> true 那么第一个思路其实就是暴力枚举了,两层for循环,不断去寻找,代码如下: ```CPP -// 时间复杂度: O(n^2) -// 空间复杂度:O(1) class Solution { public: bool canConstruct(string ransomNote, string magazine) { @@ -62,6 +60,9 @@ public: }; ``` +* 时间复杂度: O(n^2) +* 空间复杂度: O(1) + 这里时间复杂度是比较高的,而且里面还有一个字符串删除也就是erase的操作,也是费时的,当然这段代码也可以过这道题。 @@ -78,8 +79,6 @@ public: 代码如下: ```CPP -// 时间复杂度: O(n) -// 空间复杂度:O(1) class Solution { public: bool canConstruct(string ransomNote, string magazine) { @@ -105,6 +104,10 @@ public: }; ``` +* 时间复杂度: O(n) +* 空间复杂度: O(1) + + ## 其他语言版本 From 015935e4fa757f2fee1551e26336aa0fd520d844 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Sun, 19 Mar 2023 11:24:04 +0800 Subject: [PATCH 8/9] =?UTF-8?q?update=200015.=E4=B8=89=E6=95=B0=E4=B9=8B?= =?UTF-8?q?=E5=92=8C=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D=E6=9D=82=E5=BA=A6?= =?UTF-8?q?=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0015.三数之和.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/problems/0015.三数之和.md b/problems/0015.三数之和.md index 6675e408..26c9eaa2 100644 --- a/problems/0015.三数之和.md +++ b/problems/0015.三数之和.md @@ -83,6 +83,10 @@ public: }; ``` +* 时间复杂度: O(n^2) +* 空间复杂度: O(n),额外的 set 开销 + + ## 双指针 **其实这道题目使用哈希法并不十分合适**,因为在去重的操作中有很多细节需要注意,在面试中很难直接写出没有bug的代码。 @@ -158,6 +162,10 @@ public: }; ``` +* 时间复杂度: O(n^2) +* 空间复杂度: O(1) + + ## 去重逻辑的思考 ### a的去重 From cd537cb91aa0c2b879a9b3b8cf0d2f5ab0552495 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Sun, 19 Mar 2023 11:26:02 +0800 Subject: [PATCH 9/9] =?UTF-8?q?update=200018.=E5=9B=9B=E6=95=B0=E4=B9=8B?= =?UTF-8?q?=E5=92=8C=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D=E6=9D=82=E5=BA=A6?= =?UTF-8?q?=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0018.四数之和.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/problems/0018.四数之和.md b/problems/0018.四数之和.md index 8e87fcbd..5f4c2ec9 100644 --- a/problems/0018.四数之和.md +++ b/problems/0018.四数之和.md @@ -121,6 +121,10 @@ public: ``` +* 时间复杂度: O(n^3) +* 空间复杂度: O(1) + + ## 补充 二级剪枝的部分: