From 29e1a07cc3a51cb61c5aec933d5d914eb105741d Mon Sep 17 00:00:00 2001 From: sharky <1821984081@qq.com> Date: Fri, 17 Mar 2023 12:22:40 +0800 Subject: [PATCH 1/7] =?UTF-8?q?=E5=9B=9E=E6=BA=AF=E7=AE=97=E6=B3=95?= =?UTF-8?q?=E5=8E=BB=E9=87=8D=E9=97=AE=E9=A2=98=E7=9A=84=E5=8F=A6=E4=B8=80?= =?UTF-8?q?=E7=A7=8D=E5=86=99=E6=B3=95=EF=BC=8C40.=E7=BB=84=E5=90=88?= =?UTF-8?q?=E6=80=BB=E5=92=8CII=E7=9A=84java=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...溯算法去重问题的另一种写法.md | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/problems/回溯算法去重问题的另一种写法.md b/problems/回溯算法去重问题的另一种写法.md index b3bd74da..cbbd831f 100644 --- a/problems/回溯算法去重问题的另一种写法.md +++ b/problems/回溯算法去重问题的另一种写法.md @@ -317,7 +317,38 @@ class Solution { } } ``` +**40.组合总和II** +```java +class Solution { + List> result = new ArrayList<>(); + LinkedList path = new LinkedList<>(); + public List> combinationSum2(int[] candidates, int target) { + Arrays.sort( candidates ); + if( candidates[0] > target ) return result; + backtracking(candidates,target,0,0); + return result; + } + public void backtracking(int[] candidates,int target,int sum,int startIndex){ + if( sum > target )return; + if( sum == target ){ + result.add( new ArrayList<>(path) ); + } + HashSet hashSet = new HashSet<>(); + for( int i = startIndex; i < candidates.length; i++){ + if( hashSet.contains(candidates[i]) ){ + continue; + } + hashSet.add(candidates[i]); + path.add(candidates[i]); + sum += candidates[i]; + backtracking(candidates,target,sum,i+1); + path.removeLast(); + sum -= candidates[i]; + } + } +} +``` Python: **90.子集II** From 62a9b6c3722a44f8bf71ea360d6331a2f9918294 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Mon, 27 Mar 2023 14:07:22 +0800 Subject: [PATCH 2/7] =?UTF-8?q?update=200344.=E5=8F=8D=E8=BD=AC=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E4=B8=B2=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/0344.反转字符串.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/problems/0344.反转字符串.md b/problems/0344.反转字符串.md index 6ffbac26..775cfc58 100644 --- a/problems/0344.反转字符串.md +++ b/problems/0344.反转字符串.md @@ -130,6 +130,9 @@ public: }; ``` +* 时间复杂度: O(n) +* 空间复杂度: O(1) + From 22ab6281e44ca025aaca2e5a94ba3e07f2021c1a Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Mon, 27 Mar 2023 14:17:07 +0800 Subject: [PATCH 3/7] =?UTF-8?q?update=200541.=E5=8F=8D=E8=BD=AC=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E4=B8=B2II=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D?= =?UTF-8?q?=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/0541.反转字符串II.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/problems/0541.反转字符串II.md b/problems/0541.反转字符串II.md index 516f78da..179395b3 100644 --- a/problems/0541.反转字符串II.md +++ b/problems/0541.反转字符串II.md @@ -65,6 +65,9 @@ public: }; ``` +* 时间复杂度: O(n) +* 空间复杂度: O(1) + @@ -96,6 +99,9 @@ public: }; ``` +* 时间复杂度: O(n) +* 空间复杂度: O(1)或O(n), 取决于使用的语言中字符串是否可以修改. + 另一种思路的解法 @@ -116,6 +122,9 @@ public: }; ``` +* 时间复杂度: O(n) +* 空间复杂度: O(1) + ## 其他语言版本 From fe386a051dc73b3fd40bc79b70119df2e068ed9b Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Mon, 27 Mar 2023 14:29:13 +0800 Subject: [PATCH 4/7] =?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=EF=BC=9A?= =?UTF-8?q?=E6=B7=BB=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/0151.翻转字符串里的单词.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/problems/0151.翻转字符串里的单词.md b/problems/0151.翻转字符串里的单词.md index eb78cc9d..1b006665 100644 --- a/problems/0151.翻转字符串里的单词.md +++ b/problems/0151.翻转字符串里的单词.md @@ -114,6 +114,7 @@ void removeExtraSpaces(string& s) { } ``` + 有的同学可能发现用erase来移除空格,在leetcode上性能也还行。主要是以下几点;: 1. leetcode上的测试集里,字符串的长度不够长,如果足够长,性能差距会非常明显。 @@ -197,6 +198,9 @@ public: }; ``` +* 时间复杂度: O(n) +* 空间复杂度: O(1) 或 O(n),取决于语言中字符串是否可变 + ## 其他语言版本 From cc1a327fb1d9f9e3ead9ba8f122b20ff4d112baf Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Mon, 27 Mar 2023 14:36:32 +0800 Subject: [PATCH 5/7] =?UTF-8?q?update=20=E5=89=91=E6=8C=87Offer58-II.?= =?UTF-8?q?=E5=B7=A6=E6=97=8B=E8=BD=AC=E5=AD=97=E7=AC=A6=E4=B8=B2=EF=BC=9A?= =?UTF-8?q?=E6=B7=BB=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/剑指Offer58-II.左旋转字符串.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/problems/剑指Offer58-II.左旋转字符串.md b/problems/剑指Offer58-II.左旋转字符串.md index 49480abf..f368514f 100644 --- a/problems/剑指Offer58-II.左旋转字符串.md +++ b/problems/剑指Offer58-II.左旋转字符串.md @@ -66,6 +66,9 @@ public: } }; ``` +* 时间复杂度: O(n) +* 空间复杂度:O(1) + 是不是发现这代码也太简单了,哈哈。 # 总结 From 167cb84ee3b4ae514d898e33090d2453a516f02e Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Mon, 27 Mar 2023 15:12:08 +0800 Subject: [PATCH 6/7] =?UTF-8?q?update=200028.=E5=AE=9E=E7=8E=B0strStr?= =?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/0028.实现strStr.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/problems/0028.实现strStr.md b/problems/0028.实现strStr.md index 2757130c..263c1689 100644 --- a/problems/0028.实现strStr.md +++ b/problems/0028.实现strStr.md @@ -444,6 +444,8 @@ public: }; ``` +* 时间复杂度: O(n + m) +* 空间复杂度: O(m), 只需要保存字符串needle的前缀表 # 前缀表(不减一)C++实现 @@ -540,6 +542,9 @@ public: } }; ``` +* 时间复杂度: O(n + m) +* 空间复杂度: O(m) + # 总结 From dbcd875d3bcc0ccbf6b331bbfd07e799e272688b Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Mon, 27 Mar 2023 16:22:07 +0800 Subject: [PATCH 7/7] =?UTF-8?q?update=200459.=E9=87=8D=E5=A4=8D=E7=9A=84?= =?UTF-8?q?=E5=AD=90=E5=AD=97=E7=AC=A6=E4=B8=B2=EF=BC=9A=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/0459.重复的子字符串.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/problems/0459.重复的子字符串.md b/problems/0459.重复的子字符串.md index 5d870219..98c02a25 100644 --- a/problems/0459.重复的子字符串.md +++ b/problems/0459.重复的子字符串.md @@ -73,6 +73,8 @@ public: } }; ``` +* 时间复杂度: O(n) +* 空间复杂度: O(1) 不过这种解法还有一个问题,就是 我们最终还是要判断 一个字符串(s + s)是否出现过 s 的过程,大家可能直接用contains,find 之类的库函数。 却忽略了实现这些函数的时间复杂度(暴力解法是m * n,一般库函数实现为 O(m + n))。 @@ -185,6 +187,8 @@ public: } }; ``` +* 时间复杂度: O(n) +* 空间复杂度: O(n) 前缀表(不减一)的C++代码实现: @@ -219,6 +223,8 @@ public: } }; ``` +* 时间复杂度: O(n) +* 空间复杂度: O(n) ## 其他语言版本