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) + # 总结 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),取决于语言中字符串是否可变 + ## 其他语言版本 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) + 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) ## 其他语言版本 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) + ## 其他语言版本 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) + 是不是发现这代码也太简单了,哈哈。 # 总结 diff --git a/problems/回溯算法去重问题的另一种写法.md b/problems/回溯算法去重问题的另一种写法.md index bc2763ae..f39c3c74 100644 --- a/problems/回溯算法去重问题的另一种写法.md +++ b/problems/回溯算法去重问题的另一种写法.md @@ -286,8 +286,70 @@ class Solution { } ``` +**90.子集II** +```java +class Solution { + List> reslut = new ArrayList<>(); + LinkedList path = new LinkedList<>(); + + public List> subsetsWithDup(int[] nums) { + if(nums.length == 0){ + reslut.add(path); + return reslut; + } + Arrays.sort(nums); + backtracking(nums,0); + return reslut; + } + public void backtracking(int[] nums,int startIndex){ + reslut.add(new ArrayList<>(path)); + if(startIndex >= nums.length)return; + HashSet hashSet = new HashSet<>(); + for(int i = startIndex; i < nums.length; i++){ + if(hashSet.contains(nums[i])){ + continue; + } + hashSet.add(nums[i]); + path.add(nums[i]); + backtracking(nums,i+1); + path.removeLast(); + } + } +} +``` +**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**