From 200e14cbdedff221fe93387e709c8b349986ac60 Mon Sep 17 00:00:00 2001 From: GODVvVZzz <2662446324@qq.com> Date: Wed, 8 Mar 2023 20:07:45 +0800 Subject: [PATCH 01/19] =?UTF-8?q?=E4=BF=AE=E6=94=B9Java=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0070.爬楼梯完全背包版本.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/problems/0070.爬楼梯完全背包版本.md b/problems/0070.爬楼梯完全背包版本.md index 3093c833..41c2e616 100644 --- a/problems/0070.爬楼梯完全背包版本.md +++ b/problems/0070.爬楼梯完全背包版本.md @@ -128,12 +128,12 @@ Java: class Solution { public int climbStairs(int n) { int[] dp = new int[n + 1]; - int[] weight = {1,2}; + int m = 2; dp[0] = 1; - for (int i = 0; i <= n; i++) { - for (int j = 0; j < weight.length; j++) { - if (i >= weight[j]) dp[i] += dp[i - weight[j]]; + for (int i = 1; i <= n; i++) { // 遍历背包 + for (int j = 1; j <= m; j++) { //遍历物品 + if (i >= j) dp[i] += dp[i - j]; } } @@ -227,3 +227,4 @@ function climbStairs(n: number): number { + From 60d585f4eeb2018483b1f80b8697e9ea80ff871e Mon Sep 17 00:00:00 2001 From: dongyunpeng Date: Thu, 9 Mar 2023 01:36:30 +0800 Subject: [PATCH 02/19] =?UTF-8?q?=E5=AE=8C=E5=96=840707.=E8=AE=BE=E8=AE=A1?= =?UTF-8?q?=E9=93=BE=E8=A1=A8C++=E7=89=88=E6=9C=AC=EF=BC=8C=E5=9C=A8delete?= =?UTF-8?q?=E6=8C=87=E9=92=88=E4=B9=8B=E5=90=8E=E8=B5=8B=E5=80=BC=E4=B8=BA?= =?UTF-8?q?null=EF=BC=8C=E5=9C=A8=E6=B3=A8=E9=87=8A=E4=B8=AD=20=E8=AF=B4?= =?UTF-8?q?=E6=98=8E=E4=BA=86=E9=87=8E=E6=8C=87=E9=92=88=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0707.设计链表.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/problems/0707.设计链表.md b/problems/0707.设计链表.md index 43ed2622..de1e7eb4 100644 --- a/problems/0707.设计链表.md +++ b/problems/0707.设计链表.md @@ -133,6 +133,11 @@ public: LinkedNode* tmp = cur->next; cur->next = cur->next->next; delete tmp; + //delete命令指示释放了tmp指针原本所指的那部分内存, + //被delete后的指针tmp的值(地址)并非就是NULL,而是随机值。也就是被delete后, + //如果不再加上一句tmp=nullptr,tmp会成为乱指的野指针 + //如果之后的程序不小心使用了tmp,会指向难以预想的内存空间 + tmp=nullptr; _size--; } @@ -1447,3 +1452,4 @@ impl MyLinkedList { + From 4164ddbb69da491b05f201a39b7b4c6233fad929 Mon Sep 17 00:00:00 2001 From: fw_qaq Date: Fri, 10 Mar 2023 11:11:54 +0800 Subject: [PATCH 03/19] =?UTF-8?q?Update=200122.=E4=B9=B0=E5=8D=96=E8=82=A1?= =?UTF-8?q?=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6=E6=9C=BAII.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0122.买卖股票的最佳时机II.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/problems/0122.买卖股票的最佳时机II.md b/problems/0122.买卖股票的最佳时机II.md index d094da48..a863afe7 100644 --- a/problems/0122.买卖股票的最佳时机II.md +++ b/problems/0122.买卖股票的最佳时机II.md @@ -274,6 +274,7 @@ const maxProfit = (prices) => { ### TypeScript: +贪心 ```typescript function maxProfit(prices: number[]): number { let resProfit: number = 0; @@ -284,6 +285,21 @@ function maxProfit(prices: number[]): number { }; ``` +动态规划 +```typescript +function maxProfit(prices: number[]): number { + const dp = Array(prices.length) + .fill(0) + .map(() => Array(2).fill(0)) + dp[0][0] = -prices[0] + for (let i = 1; i < prices.length; i++) { + dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] - prices[i]) + dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] + prices[i]) + } + return dp[prices.length - 1][1] +} +``` + ### Rust 贪心: From 3d64ead78d6e865a994a47df38c362a0db0636d9 Mon Sep 17 00:00:00 2001 From: fw_qaq Date: Fri, 10 Mar 2023 14:52:24 +0800 Subject: [PATCH 04/19] =?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 296ef893b4a15d4459e7400bd2bd699b3173bbbc Mon Sep 17 00:00:00 2001 From: ZerenZhang2022 <118794589+ZerenZhang2022@users.noreply.github.com> Date: Fri, 10 Mar 2023 13:12:53 -0500 Subject: [PATCH 05/19] =?UTF-8?q?Update=200151.=E7=BF=BB=E8=BD=AC=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E4=B8=B2=E9=87=8C=E7=9A=84=E5=8D=95=E8=AF=8D.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加python解法:使用双指针法移除空格 --- problems/0151.翻转字符串里的单词.md | 42 ++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/problems/0151.翻转字符串里的单词.md b/problems/0151.翻转字符串里的单词.md index dc12ae23..eb78cc9d 100644 --- a/problems/0151.翻转字符串里的单词.md +++ b/problems/0151.翻转字符串里的单词.md @@ -516,6 +516,48 @@ class Solution: return s[:ps] + s[ps:][::-1] # Must do the last step, because the last word is omit though the pointers are on the correct positions, ``` +```python +class Solution: # 使用双指针法移除空格 + def reverseWords(self, s: str) -> str: + + def removeextraspace(s): + start = 0; end = len(s)-1 + while s[start]==' ': + start+=1 + while s[end]==' ': + end-=1 + news = list(s[start:end+1]) + slow = fast = 0 + while fast0 and news[fast]==news[fast-1]==' ': + fast+=1 + news[slow]=news[fast] + slow+=1; fast+=1 + #return "".join(news[:slow]) + return news[:slow] + + def reversestr(s): + left,right = 0,len(s)-1 + news = list(s) + while left Date: Sun, 12 Mar 2023 13:07:31 +0800 Subject: [PATCH 06/19] =?UTF-8?q?=E6=9B=B4=E6=96=B00746.=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E6=9C=80=E5=B0=8F=E8=8A=B1=E8=B4=B9=E7=88=AC=E6=A5=BC=E6=A2=AF?= =?UTF-8?q?.md=20=E6=8F=90=E4=BE=9BGo=E7=89=88=E6=9C=AC=E8=A7=A3=E6=B3=95?= =?UTF-8?q?=E7=9A=84=E6=96=B0=E6=80=9D=E8=B7=AF(dp[i]=E8=A1=A8=E7=A4=BA?= =?UTF-8?q?=E4=BB=8Ei=E5=B1=82=E8=B5=B7=E8=B7=B3=E6=89=80=E9=9C=80?= =?UTF-8?q?=E8=A6=81=E6=94=AF=E4=BB=98=E7=9A=84=E6=9C=80=E5=B0=8F=E8=B4=B9?= =?UTF-8?q?=E7=94=A8)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/.gitignore | 8 ++++++ .idea/leetcode-master.iml | 9 +++++++ .idea/modules.xml | 8 ++++++ .idea/vcs.xml | 6 +++++ problems/0746.使用最小花费爬楼梯.md | 27 ++++++++++++++++++++ 5 files changed, 58 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/leetcode-master.iml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 00000000..35410cac --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml +# 基于编辑器的 HTTP 客户端请求 +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/leetcode-master.iml b/.idea/leetcode-master.iml new file mode 100644 index 00000000..5e764c4f --- /dev/null +++ b/.idea/leetcode-master.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 00000000..7c250acd --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000..94a25f7f --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/problems/0746.使用最小花费爬楼梯.md b/problems/0746.使用最小花费爬楼梯.md index 44b6406c..d6b3a177 100644 --- a/problems/0746.使用最小花费爬楼梯.md +++ b/problems/0746.使用最小花费爬楼梯.md @@ -284,6 +284,33 @@ func min(a, b int) int { return b } ``` +``` GO +第二种思路: dp[i]表示从i层起跳所需要支付的最小费用 +递推公式: +i Date: Sun, 12 Mar 2023 13:10:21 +0800 Subject: [PATCH 07/19] =?UTF-8?q?=E6=B7=BB=E5=8A=A0.gitignore?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..3d725761 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.DS_Store +.idea \ No newline at end of file From d8df97845c165669713c5606b5e6a5397e356d5e Mon Sep 17 00:00:00 2001 From: jeffreyjia Date: Sun, 12 Mar 2023 13:24:03 +0800 Subject: [PATCH 08/19] =?UTF-8?q?=E5=BF=BD=E7=95=A5idea?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/.gitignore | 8 -------- .idea/leetcode-master.iml | 9 --------- .idea/modules.xml | 8 -------- .idea/vcs.xml | 6 ------ 4 files changed, 31 deletions(-) delete mode 100644 .idea/.gitignore delete mode 100644 .idea/leetcode-master.iml delete mode 100644 .idea/modules.xml delete mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 35410cac..00000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# 默认忽略的文件 -/shelf/ -/workspace.xml -# 基于编辑器的 HTTP 客户端请求 -/httpRequests/ -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml diff --git a/.idea/leetcode-master.iml b/.idea/leetcode-master.iml deleted file mode 100644 index 5e764c4f..00000000 --- a/.idea/leetcode-master.iml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index 7c250acd..00000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 94a25f7f..00000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file From 50cdae9a91df693b122f5bf8e89aa8fc5164a0d8 Mon Sep 17 00:00:00 2001 From: jeffreyjia Date: Sun, 12 Mar 2023 13:25:18 +0800 Subject: [PATCH 09/19] =?UTF-8?q?=E5=88=A0=E9=99=A4.gitignore?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 -- .idea/.gitignore | 8 ++++++++ .idea/leetcode-master.iml | 9 +++++++++ .idea/modules.xml | 8 ++++++++ .idea/vcs.xml | 6 ++++++ 5 files changed, 31 insertions(+), 2 deletions(-) delete mode 100644 .gitignore create mode 100644 .idea/.gitignore create mode 100644 .idea/leetcode-master.iml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 3d725761..00000000 --- a/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -.DS_Store -.idea \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 00000000..35410cac --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml +# 基于编辑器的 HTTP 客户端请求 +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/leetcode-master.iml b/.idea/leetcode-master.iml new file mode 100644 index 00000000..5e764c4f --- /dev/null +++ b/.idea/leetcode-master.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 00000000..7c250acd --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000..94a25f7f --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file From d34e4a8378ae8c62cdb8a3a00462561c70ec0b4a Mon Sep 17 00:00:00 2001 From: jeffreyjia Date: Sun, 12 Mar 2023 13:25:53 +0800 Subject: [PATCH 10/19] =?UTF-8?q?=E5=BF=BD=E7=95=A5idea?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/.gitignore | 8 -------- .idea/leetcode-master.iml | 9 --------- .idea/modules.xml | 8 -------- .idea/vcs.xml | 6 ------ 4 files changed, 31 deletions(-) delete mode 100644 .idea/.gitignore delete mode 100644 .idea/leetcode-master.iml delete mode 100644 .idea/modules.xml delete mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 35410cac..00000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# 默认忽略的文件 -/shelf/ -/workspace.xml -# 基于编辑器的 HTTP 客户端请求 -/httpRequests/ -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml diff --git a/.idea/leetcode-master.iml b/.idea/leetcode-master.iml deleted file mode 100644 index 5e764c4f..00000000 --- a/.idea/leetcode-master.iml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index 7c250acd..00000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 94a25f7f..00000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file From 411ef134a7e194e281affa0be45efe9bbcad4a25 Mon Sep 17 00:00:00 2001 From: fw_qaq Date: Tue, 14 Mar 2023 12:33:05 +0800 Subject: [PATCH 11/19] =?UTF-8?q?Update=201005.K=E6=AC=A1=E5=8F=96?= =?UTF-8?q?=E5=8F=8D=E5=90=8E=E6=9C=80=E5=A4=A7=E5=8C=96=E7=9A=84=E6=95=B0?= =?UTF-8?q?=E7=BB=84=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...1005.K次取反后最大化的数组和.md | 21 +++++++------------ 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/problems/1005.K次取反后最大化的数组和.md b/problems/1005.K次取反后最大化的数组和.md index cdf42511..439bdfde 100644 --- a/problems/1005.K次取反后最大化的数组和.md +++ b/problems/1005.K次取反后最大化的数组和.md @@ -231,23 +231,18 @@ var largestSumAfterKNegations = function(nums, k) { ```Rust impl Solution { - pub fn largest_sum_after_k_negations(nums: Vec, k: i32) -> i32 { - let mut nums = nums; - let mut k = k; - let len = nums.len(); - nums.sort_by(|a, b| b.abs().cmp(&a.abs())); - for i in 0..len { - if nums[i] < 0 && k > 0 { - nums[i] *= -1; + pub fn largest_sum_after_k_negations(mut nums: Vec, mut k: i32) -> i32 { + nums.sort_by_key(|b| std::cmp::Reverse(b.abs())); + for v in nums.iter_mut() { + if *v < 0 && k > 0 { + *v *= -1; k -= 1; } } - if k % 2 == 1 { nums[len - 1] *= -1; } - let mut result = 0; - for num in nums { - result += num; + if k % 2 == 1 { + *nums.last_mut().unwrap() *= -1; } - result + nums.iter().sum() } } ``` From 2175be827dbe4215f3ccf55048e62b158a936a0e Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Thu, 16 Mar 2023 22:24:32 +0800 Subject: [PATCH 12/19] =?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 13/19] =?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 14/19] =?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 15/19] =?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 16/19] =?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 17/19] =?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 18/19] =?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 19/19] =?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) + + ## 补充 二级剪枝的部分: