From c0f32a1ba01c56ed3051aae8ec5884d69446ae4b Mon Sep 17 00:00:00 2001 From: lucifer Date: Thu, 14 Jan 2021 10:43:31 +0800 Subject: [PATCH] feat: cpp code --- problems/32.longest-valid-parentheses.md | 78 ++++++++++++++++++++++-- problems/39.combination-sum.md | 35 ++++++++++- problems/46.permutations.md | 32 +++++++++- problems/47.permutations-ii.md | 34 ++++++++++- problems/48.rotate-image.md | 26 +++++++- problems/49.group-anagrams.md | 26 +++++++- problems/50.pow-x-n.md | 25 +++++++- 7 files changed, 242 insertions(+), 14 deletions(-) diff --git a/problems/32.longest-valid-parentheses.md b/problems/32.longest-valid-parentheses.md index 6669279..ec897c6 100644 --- a/problems/32.longest-valid-parentheses.md +++ b/problems/32.longest-valid-parentheses.md @@ -85,7 +85,7 @@ class Solution: ### 代码 -- 语言支持: Python, javascript +- 语言支持: Python, javascript, CPP javascript code: @@ -133,6 +133,26 @@ class Solution: return res ``` +CPP Code: + +```cpp +class Solution { +public: + int longestValidParentheses(string s) { + stack st; + st.push(-1); + int ans = 0; + for (int i = 0; i < s.size(); ++i) { + if (s[i] == ')' && st.top() != -1 && s[st.top()] == '(') { + st.pop(); + ans = max(ans, i - st.top()); + } else st.push(i); + } + return ans; + } +}; +``` + **复杂度分析** - 时间复杂度:$$O(N)$$ @@ -154,7 +174,9 @@ class Solution: ### 代码 -代码支持:Java,Python +代码支持:Java,Python3, CPP + +Java Code: ```java public class Solution { @@ -192,6 +214,8 @@ public class Solution { } ``` +Python3 Code: + ```py class Solution: def longestValidParentheses(self, s: str) -> int: @@ -219,6 +243,31 @@ class Solution: return ans ``` +CPP Code: + +```cpp +class Solution { +public: + int longestValidParentheses(string s) { + int left = 0, right = 0, ans = 0, N = s.size(); + for (int i = 0; i < N; ++i) { + left += s[i] == '('; + right += s[i] == ')'; + if (left == right) ans = max(ans, left + right); + else if (right > left) left = right = 0; + } + left = 0, right = 0; + for (int i = N - 1; i >= 0; --i) { + left += s[i] == '('; + right += s[i] == ')'; + if (left == right) ans = max(ans, left + right); + else if (left > right) left = right = 0; + } + return ans; + } +}; +``` + ## 动态规划 ### 思路 @@ -245,7 +294,9 @@ s = '(())())' ### 代码 -Python Code: +代码支持:Python3, CPP + +Python3 Code: ```py class Solution: @@ -273,6 +324,26 @@ class Solution: return mlen ``` +CPP Code: + +```cpp +class Solution { +public: + int longestValidParentheses(string s) { + vector dp(s.size() + 1, 0); + int ans = 0; + for (int i = 0; i < s.size(); ++i) { + if (s[i] == '(') continue; + int start = i - dp[i] - 1; + if (start >= 0 && s[start] == '(') + dp[i + 1] = dp[i] + 2 + dp[start]; + ans = max(ans, dp[i + 1]); + } + return ans; + } +}; +``` + **复杂度分析** - 时间复杂度:$$O(N)$$ @@ -292,7 +363,6 @@ class Solution: 1. 如果判断的不仅仅只有'('和')', 还有'[', ']', '{'和'}', 该怎么办? 2. 如果输出的不是长度, 而是任意一个最长有效括号对的字符串, 该怎么办? - 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 ![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg) diff --git a/problems/39.combination-sum.md b/problems/39.combination-sum.md index 97864c4..9837b11 100644 --- a/problems/39.combination-sum.md +++ b/problems/39.combination-sum.md @@ -76,7 +76,9 @@ candidate 中的每个元素都是独一无二的。 ## 代码 -- 语言支持: Javascript,Python3 +- 语言支持: Javascript,Python3,CPP + +JS Code: ```js function backtrack(list, tempList, nums, remain, start) { @@ -147,6 +149,33 @@ class Solution: path.pop() ``` +CPP Code: + +```cpp +class Solution { +private: + vector> res; + void dfs(vector &c, int t, int start, vector &v) { + if (!t) { + res.push_back(v); + return; + } + for (int i = start; i < c.size() && t >= c[i]; ++i) { + v.push_back(c[i]); + dfs(c, t - c[i], i, v); + v.pop_back(); + } + } +public: + vector> combinationSum(vector& candidates, int target) { + sort(candidates.begin(), candidates.end()); + vector v; + dfs(candidates, target, 0, v); + return res; + } +}; +``` + ## 相关题目 - [40.combination-sum-ii](./40.combination-sum-ii.md) @@ -156,3 +185,7 @@ class Solution: - [90.subsets-ii](./90.subsets-ii.md) - [113.path-sum-ii](./113.path-sum-ii.md) - [131.palindrome-partitioning](./131.palindrome-partitioning.md) + +``` + +``` diff --git a/problems/46.permutations.md b/problems/46.permutations.md index 87babfc..7226c09 100644 --- a/problems/46.permutations.md +++ b/problems/46.permutations.md @@ -63,7 +63,7 @@ https://leetcode-cn.com/problems/permutations/ ## 代码 -- 语言支持: Javascript, Python3 +- 语言支持: Javascript, Python3,CPP Javascript Code: @@ -130,6 +130,36 @@ class Solution: return res ``` +CPP Code: + +```cpp +class Solution { + vector> ans; + void dfs(vector &nums, int start) { + if (start == nums.size() - 1) { + ans.push_back(nums); + return; + } + for (int i = start; i < nums.size(); ++i) { + swap(nums[i], nums[start]); + dfs(nums, start + 1); + swap(nums[i], nums[start]); + } + } +public: + vector> permute(vector& nums) { + dfs(nums, 0); + return ans; + } +}; +``` + +**复杂度分析** +令 N 为数组长度。 + +- 时间复杂度:$O(N!)$ +- 空间复杂度:$O(N)$ + ## 相关题目 - [31.next-permutation](./31.next-permutation.md) diff --git a/problems/47.permutations-ii.md b/problems/47.permutations-ii.md index ea9fd6f..1f84060 100644 --- a/problems/47.permutations-ii.md +++ b/problems/47.permutations-ii.md @@ -49,11 +49,11 @@ https://leetcode-cn.com/problems/permutations-ii/ 如果继续沿用上面的逻辑,那么我们是永远无法拿到三个数字的,因此我们的逻辑需要变化。 这里我的算法是记录每一个被选取的索引,而不是值,这就保证了**同一个数字不会被选取多次,并且可以选取所有数字了**。 -不过这还是有一个问题。 仍然以 [1,1,2] 为例,如果第一次选取了第一个1,第二次选取了第二个1,这就产生了一个组合 [1,1,2]。 如果继续第一次选取了第二个 1,而第二次选取了第一个 1,那么又会产生组合 [1,1,2],可以看出这两个组合是重复的。 +不过这还是有一个问题。 仍然以 [1,1,2] 为例,如果第一次选取了第一个 1,第二次选取了第二个 1,这就产生了一个组合 [1,1,2]。 如果继续第一次选取了第二个 1,而第二次选取了第一个 1,那么又会产生组合 [1,1,2],可以看出这两个组合是重复的。 一个解决方案是对 nums 进行一次排序,并规定如果 **i > 0 and nums[i] == nums[i - 1] and visited[i - 1]**, 则不进行选取即可。 -经过这样的处理,每次实际上都是从后往前依次重复的数。仍然以上面的 [1,1,2] 为例。[1,1,2] 这个排列一定是先取的第二个 1,再取第一个 1,最后取的 2。因为如果先取的第一个 1,那么永远无法取到三个数,便形成了一个不可行解。 +经过这样的处理,每次实际上都是从后往前依次重复的数。仍然以上面的 [1,1,2] 为例。[1,1,2] 这个排列一定是先取的第二个 1,再取第一个 1,最后取的 2。因为如果先取的第一个 1,那么永远无法取到三个数,便形成了一个不可行解。 ## 关键点解析 @@ -62,7 +62,9 @@ https://leetcode-cn.com/problems/permutations-ii/ ## 代码 -- 语言支持: Javascript,Python3 +- 语言支持: Javascript,Python3, CPP + +JS Code: ```js /* @@ -131,6 +133,32 @@ class Solution: return res ``` +CPP Code: + +```cpp +class Solution { +private: + vector> ans; + void permute(vector nums, int start) { + if (start == nums.size() - 1) { + ans.push_back(nums); + return; + } + for (int i = start; i < nums.size(); ++i) { + if (i != start && nums[i] == nums[start]) continue; + swap(nums[i], nums[start]); + permute(nums, start + 1); + } + } +public: + vector> permuteUnique(vector& nums) { + sort(nums.begin(), nums.end()); + permute(nums, 0); + return ans; + } +}; +``` + ## 相关题目 - [31.next-permutation](./31.next-permutation.md) diff --git a/problems/48.rotate-image.md b/problems/48.rotate-image.md index ded81fb..43593e9 100644 --- a/problems/48.rotate-image.md +++ b/problems/48.rotate-image.md @@ -15,7 +15,7 @@ https://leetcode-cn.com/problems/rotate-image/ 示例 1: -给定 matrix = +给定 matrix = [ [1,2,3], [4,5,6], @@ -36,7 +36,7 @@ https://leetcode-cn.com/problems/rotate-image/ [ 2, 4, 8,10], [13, 3, 6, 7], [15,14,12,16] -], +], 原地旋转输入矩阵,使其变为: [ @@ -100,7 +100,7 @@ var rotate = function (matrix) { ## 代码 -- 语言支持: Javascript,Python3 +- 语言支持: Javascript,Python3, CPP ```js /* @@ -161,6 +161,26 @@ class Solution: matrix[:] = map(list, zip(*matrix[::-1])) ``` +CPP Code: + +```cpp +class Solution { +public: + void rotate(vector>& matrix) { + int N = matrix.size(); + for (int i = 0; i < N / 2; ++i) { + for (int j = i; j < N - i - 1; ++j) { + int tmp = matrix[i][j]; + matrix[i][j] = matrix[N - j - 1][i]; + matrix[N - j - 1][i] = matrix[N - i - 1][N - j - 1]; + matrix[N - i - 1][N - j - 1] = matrix[j][N - i - 1]; + matrix[j][N - i - 1] = tmp; + } + } + } +}; +``` + **复杂度分析** - 时间复杂度:$$O(M * N)$$ diff --git a/problems/49.group-anagrams.md b/problems/49.group-anagrams.md index bdba973..e62e9e7 100644 --- a/problems/49.group-anagrams.md +++ b/problems/49.group-anagrams.md @@ -80,7 +80,9 @@ var groupAnagrams = function (strs) { ## 代码 -- 语言支持: Javascript,Python3 +- 语言支持: Javascript,Python3, CPP + +JS Code: ```js /* @@ -135,6 +137,28 @@ class Solution: return list(str_dict.values()) ``` +CPP Code: + +```cpp +class Solution { +public: + vector> groupAnagrams(vector& A) { + unordered_map m; + vector> ans; + for (auto &s : A) { + auto p = s; + sort(p.begin(), p.end()); + if (!m.count(p)) { + m[p] = ans.size(); + ans.push_back({}); + } + ans[m[p]].push_back(s); + } + return ans; + } +}; +``` + **复杂度分析** 其中 N 为 strs 的长度, M 为 strs 中字符串的平均长度。 diff --git a/problems/50.pow-x-n.md b/problems/50.pow-x-n.md index d263e8d..1f2d312 100644 --- a/problems/50.pow-x-n.md +++ b/problems/50.pow-x-n.md @@ -123,7 +123,7 @@ class Solution: ### 代码 -语言支持: Python3 +语言支持: Python3, CPP Python3 Code: @@ -139,6 +139,29 @@ class Solution: return self.myPow(x _ x, n // 2) if n % 2 == 0 else x _ self.myPow(x, n - 1) ``` +CPP Code: + +```cpp +class Solution { + double myPow(double x, long n) { + if (n < 0) return 1 / myPow(x, -n); + if (n == 0) return 1; + if (n == 1) return x; + if (n == 2) return x * x; + return myPow(myPow(x, n / 2), 2) * (n % 2 ? x : 1); + } +public: + double myPow(double x, int n) { + return myPow(x, (long)n); + } +}; +``` + +**复杂度分析** + +- 时间复杂度:$$O(logN)$$ +- 空间复杂度:$$O(logN)$$ + ## 解法三 - 位运算 ### 思路 -- GitLab