提交 38e70eca 编写于 作者: 每日一练社区's avatar 每日一练社区

update dir sstructure

上级 0297ca80
{
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"keywords": [
"leetcode",
"合并两个有序数组"
],
"children": [],
"export": [
"solution.json"
],
"title": "合并两个有序数组"
}
\ No newline at end of file
<p>给你两个有序整数数组 <code>nums1</code><em> </em><code>nums2</code>,请你将 <code>nums2</code><em> </em>合并到 <code>nums1</code><em> </em><em></em>使 <code>nums1</code><em> </em>成为一个有序数组。</p><p>初始化 <code>nums1</code><code>nums2</code> 的元素数量分别为 <code>m</code><code>n</code><em> </em>。你可以假设 <code>nums1</code><em> </em>的空间大小等于 <code>m + n</code>,这样它就有足够的空间保存来自 <code>nums2</code> 的元素。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3<strong><br />输出:</strong>[1,2,2,3,5,6]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>nums1 = [1], m = 1, nums2 = [], n = 0<strong><br />输出:</strong>[1]</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>nums1.length == m + n</code></li> <li><code>nums2.length == n</code></li> <li><code>0 <= m, n <= 200</code></li> <li><code>1 <= m + n <= 200</code></li> <li><code>-10<sup>9</sup> <= nums1[i], nums2[i] <= 10<sup>9</sup></code></li></ul>
\ No newline at end of file
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
void merge(vector<int> &nums1, int m, vector<int> &nums2, int n)
{
int i = m - 1;
int j = n - 1;
int k = nums1.size() - 1;
while (i >= 0 && j >= 0)
{
if (nums1[i] < nums2[j])
{
nums1[k--] = nums2[j--];
}
else
{
nums1[k--] = nums1[i--];
}
}
while (j >= 0)
{
nums1[k--] = nums2[j--];
}
}
};
\ No newline at end of file
{
"type": "code_options",
"author": "CSDN.net",
"source": "solution.md",
"exercise_id": "5c5734c6d9d747b8bc5bb77b4a2ea0a6"
}
\ No newline at end of file
# 合并两个有序数组
<p>给你两个有序整数数组 <code>nums1</code><em> </em><code>nums2</code>,请你将 <code>nums2</code><em> </em>合并到 <code>nums1</code><em> </em><em></em>使 <code>nums1</code><em> </em>成为一个有序数组。</p><p>初始化 <code>nums1</code><code>nums2</code> 的元素数量分别为 <code>m</code><code>n</code><em> </em>。你可以假设 <code>nums1</code><em> </em>的空间大小等于 <code>m + n</code>,这样它就有足够的空间保存来自 <code>nums2</code> 的元素。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3<strong><br />输出:</strong>[1,2,2,3,5,6]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>nums1 = [1], m = 1, nums2 = [], n = 0<strong><br />输出:</strong>[1]</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>nums1.length == m + n</code></li> <li><code>nums2.length == n</code></li> <li><code>0 <= m, n <= 200</code></li> <li><code>1 <= m + n <= 200</code></li> <li><code>-10<sup>9</sup> <= nums1[i], nums2[i] <= 10<sup>9</sup></code></li></ul>
<p>以下错误的选项是?</p>
## aop
### before
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
```cpp
int main()
{
Solution sol;
vector<int> nums1 = {1, 2, 3, 0, 0, 0};
int m = 3;
vector<int> nums2 = {2, 5, 6};
int n = 3;
sol.merge(nums1, m, nums2, n);
for (auto i : nums1)
cout << i << " ";
return 0;
}
```
## 答案
```cpp
class Solution
{
public:
void merge(vector<int> &nums1, int m, vector<int> &nums2, int n)
{
int i = m - 1;
int j = n - 1;
int k = m + n - 1;
while (i > 0 && j > 0)
{
if (nums1[i] > nums2[j])
{
nums1[k--] = nums1[i--];
}
else
{
nums1[k--] = nums2[j--];
}
}
return;
}
};
```
## 选项
### A
```cpp
class Solution
{
public:
void merge(vector<int> &nums1, int m, vector<int> &nums2, int n)
{
int sum = m + n;
int pos = 0;
if (n < 1)
;
else
{
for (int i = 0; i < sum && n; i++)
{
if (nums2[pos] <= nums1[i] || i >= m)
{
for (int k = nums1.size() - 1; k > i; k--)
{
nums1[k] = nums1[k - 1];
}
nums1[i] = nums2[pos];
pos++;
n--;
m++;
}
}
}
}
};
```
### B
```cpp
class Solution
{
public:
void merge(vector<int> &nums1, int m, vector<int> &nums2, int n)
{
int sum = m + n;
int k = 0;
for (int i = m; i < sum; i++)
{
nums1[i] = nums2[k];
k++;
}
sort(nums1.begin(), nums1.end());
}
};
```
### C
```cpp
class Solution
{
public:
static bool cmp(const int &a, const int &b)
{
return a < b;
}
void merge(vector<int> &nums1, int m, vector<int> &nums2, int n)
{
int sum = m + n;
int k = 0;
for (int i = m; i < sum; i++)
{
nums1[i] = nums2[k];
k++;
}
sort(nums1.begin(), nums1.end(), cmp);
}
};
```
{
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"keywords": [
"leetcode",
"解码方法"
],
"children": [],
"export": [
"solution.json"
],
"title": "解码方法"
}
\ No newline at end of file
<p>一条包含字母 <code>A-Z</code> 的消息通过以下映射进行了 <strong>编码</strong></p>
<pre>'A' -> 1'B' -> 2...'Z' -> 26</pre>
<p><strong>解码</strong> 已编码的消息,所有数字必须基于上述映射的方法,反向映射回字母(可能有多种方法)。例如,<code>"11106"</code> 可以映射为:</p>
<ul>
<li><code>"AAJF"</code> ,将消息分组为 <code>(1 1 10 6)</code></li>
<li><code>"KJF"</code> ,将消息分组为 <code>(11 10 6)</code></li>
</ul>
<p>注意,消息不能分组为  <code>(1 11 06)</code> ,因为 <code>"06"</code> 不能映射为 <code>"F"</code> ,这是由于 <code>"6"</code>
<code>"06"</code> 在映射中并不等价。
</p>
<p>给你一个只含数字的 <strong>非空 </strong>字符串 <code>s</code> ,请计算并返回 <strong>解码</strong> 方法的 <strong>总数</strong></p>
<p>题目数据保证答案肯定是一个 <strong>32 位</strong> 的整数。</p>
<p> </p>
<p><strong>示例 1:</strong></p>
<pre><strong>输入:</strong>s = "12"<strong><br />输出:</strong>2<strong><br />解释:</strong>它可以解码为 "AB"(1 2)或者 "L"(12)。</pre>
<p><strong>示例 2:</strong></p>
<pre><strong>输入:</strong>s = "226"<strong><br />输出:</strong>3<strong><br />解释:</strong>它可以解码为 "BZ" (2 26), "VF" (22 6), 或者 "BBF" (2 2 6) 。</pre>
<p><strong>示例 3:</strong></p>
<pre><strong>输入:</strong>s = "0"<strong><br />输出:</strong>0<strong><br />解释:</strong>没有字符映射到以 0 开头的数字。含有 0 的有效映射是 'J' -> "10" 和 'T'-> "20" 。由于没有字符,因此没有有效的方法对此进行解码,因为所有数字都需要映射。</pre>
<p><strong>示例 4:</strong></p>
<pre><strong>输入:</strong>s = "06"<strong><br />输出:</strong>0<strong><br />解释:</strong>"06" 不能映射到 "F" ,因为字符串含有前导 0("6" 和 "06" 在映射中并不等价)。</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= s.length <= 100</code></li>
<li><code>s</code> 只包含数字,并且可能包含前导零。</li>
</ul>
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static int numDecodings(char *s)
{
int len = strlen(s);
if (len == 0)
{
return 0;
}
int a = 1;
int b = s[0] == '0' ? 0 : a;
int c = b;
for (int i = 2; i <= len; i++)
{
c = s[i - 1] == '0' ? 0 : b;
int num = (s[i - 2] - '0') * 10 + (s[i - 1] - '0');
if (num >= 10 && num <= 26)
{
c += a;
}
a = b;
b = c;
}
return c;
}
int main(int argc, char **argv)
{
if (argc != 2)
{
fprintf(stderr, "Usage: ./test number\n");
exit(-1);
}
printf("%d\n", numDecodings(argv[1]));
return 0;
}
\ No newline at end of file
{
"type": "code_options",
"author": "CSDN.net",
"source": "solution.md",
"exercise_id": "43470f52b94346fba47c299883a84167"
}
\ No newline at end of file
# 解码方法
<p>一条包含字母 <code>A-Z</code> 的消息通过以下映射进行了 <strong>编码</strong></p>
<pre>'A' -> 1'B' -> 2...'Z' -> 26</pre>
<p><strong>解码</strong> 已编码的消息,所有数字必须基于上述映射的方法,反向映射回字母(可能有多种方法)。例如,<code>"11106"</code> 可以映射为:</p>
<ul>
<li><code>"AAJF"</code> ,将消息分组为 <code>(1 1 10 6)</code></li>
<li><code>"KJF"</code> ,将消息分组为 <code>(11 10 6)</code></li>
</ul>
<p>注意,消息不能分组为  <code>(1 11 06)</code> ,因为 <code>"06"</code> 不能映射为 <code>"F"</code> ,这是由于 <code>"6"</code>
<code>"06"</code> 在映射中并不等价。
</p>
<p>给你一个只含数字的 <strong>非空 </strong>字符串 <code>s</code> ,请计算并返回 <strong>解码</strong> 方法的 <strong>总数</strong></p>
<p>题目数据保证答案肯定是一个 <strong>32 位</strong> 的整数。</p>
<p> </p>
<p><strong>示例 1:</strong></p>
<pre><strong>输入:</strong>s = "12"<strong><br />输出:</strong>2<strong><br />解释:</strong>它可以解码为 "AB"(1 2)或者 "L"(12)。</pre>
<p><strong>示例 2:</strong></p>
<pre><strong>输入:</strong>s = "226"<strong><br />输出:</strong>3<strong><br />解释:</strong>它可以解码为 "BZ" (2 26), "VF" (22 6), 或者 "BBF" (2 2 6) 。</pre>
<p><strong>示例 3:</strong></p>
<pre><strong>输入:</strong>s = "0"<strong><br />输出:</strong>0<strong><br />解释:</strong>没有字符映射到以 0 开头的数字。含有 0 的有效映射是 'J' -> "10" 和 'T'-> "20" 。由于没有字符,因此没有有效的方法对此进行解码,因为所有数字都需要映射。</pre>
<p><strong>示例 4:</strong></p>
<pre><strong>输入:</strong>s = "06"<strong><br />输出:</strong>0<strong><br />解释:</strong>"06" 不能映射到 "F" ,因为字符串含有前导 0("6" 和 "06" 在映射中并不等价)。</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= s.length <= 100</code></li>
<li><code>s</code> 只包含数字,并且可能包含前导零。</li>
</ul>
<p>以下错误的选项是?</p>
## aop
### before
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
```cpp
int main()
{
Solution sol;
int res;
string s = "226";
res = sol.numDecodings(s);
cout << res;
return 0;
}
```
## 答案
```cpp
class Solution
{
public:
int numDecodings(string s)
{
if (s.empty() || s[0] == '0')
return 0;
vector<int> dp(s.size() + 1, 0);
dp[0] = 1;
for (int i = 1; i < dp.size(); ++i)
{
dp[i] = (s[i - 1] == '0') ? 0 : dp[i - 1];
if (i > 1 && (s[i - 2] == '1' || (s[i - 1] == '2' && s[i - 1] <= '6')))
dp[i] += dp[i - 1];
}
return dp[s.size()];
}
};
```
## 选项
### A
```cpp
class Solution
{
public:
int dp(string &s, int i, int j)
{
int n = j - i + 1;
if (n == 0)
return 0;
if (n == 1)
return s[i] == '0' ? 0 : 1;
if (n == 2)
{
if (s[i] == '0')
return 0;
if (s[i] > '2')
return s[j] == '0' ? 0 : 1;
if (s[i] == '2' && s[j] > '6')
return 1;
if (s[j] == '0')
return 1;
return 2;
}
if (s[i] > '2')
return dp(s, i + 1, j);
if (s[i] == '2')
{
if (s[i + 1] == '0')
return dp(s, i + 2, j);
else if (s[i + 1] < '7')
return dp(s, i + 1, j) + dp(s, i + 2, j);
else
return dp(s, i + 1, j);
}
if (s[i] == '0')
return 0;
if (s[i + 1] == '0')
return dp(s, i + 2, j);
return dp(s, i + 1, j) + dp(s, i + 2, j);
}
int numDecodings(string s)
{
return dp(s, 0, s.size() - 1);
}
};
```
### B
```cpp
class Solution
{
public:
int numDecodings(string s)
{
vector<int> nums(s.size());
if (s[0] == '0')
{
return 0;
}
nums[0] = 1;
if (s.size() > 1)
{
if (s[1] != '0')
{
nums[1] += 1;
}
if ((s[0] - '0') * 10 + (s[1] - '0') <= 26)
{
nums[1] += 1;
}
}
for (int i = 2; i < s.size(); i++)
{
if (s[i] != '0')
{
nums[i] += nums[i - 1];
}
if (s[i - 1] != '0' && ((s[i - 1] - '0') * 10 + (s[i] - '0') <= 26))
{
nums[i] += nums[i - 2];
}
}
return nums[s.size() - 1];
}
};
```
### C
```cpp
class Solution
{
public:
int numDecodings(string s)
{
int n = s.size();
if (s.empty())
return 0;
if (s[0] == '0')
return 0;
vector<int> info(n + 1, 0);
info[0] = 1;
info[1] = 1;
for (int i = 2; i < n + 1; ++i)
{
if (s[i - 1] != '0')
info[i] += info[i - 1];
if (s.substr(i - 2, 2) <= "26" && s.substr(i - 2, 2) >= "10")
info[i] += info[i - 2];
}
return info[n];
}
};
```
{
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"keywords": [
"leetcode",
"格雷编码"
],
"children": [],
"export": [
"solution.json"
],
"title": "格雷编码"
}
\ No newline at end of file
<p>格雷编码是一个二进制数字系统,在该系统中,两个连续的数值仅有一个位数的差异。</p>
<p>给定一个代表编码总位数的非负整数<em> n</em>,打印其格雷编码序列。即使有多个不同答案,你也只需要返回其中一种。</p>
<p>格雷编码序列必须以 0 开头。</p>
<p>&nbsp;</p>
<p><strong>示例 1:</strong></p>
<pre><strong>输入:</strong>&nbsp;2<strong><br />输出:</strong>&nbsp;[0,1,3,2]<strong><br />解释:</strong>00 - 001 - 111 - 310 - 2对于给定的&nbsp;<em>n</em>,其格雷编码序列并不唯一。例如,[0,2,3,1]&nbsp;也是一个有效的格雷编码序列。00 - 010 - 211 - 301 - 1</pre>
<p><strong>示例&nbsp;2:</strong></p>
<pre><strong>输入:</strong>&nbsp;0<strong><br />输出:</strong>&nbsp;[0]<strong><br />解释:</strong> 我们定义格雷编码序列必须以 0 开头。给定编码总位数为 <em>n</em> 的格雷编码序列,其长度为 2<sup>n</sup>。当 <em>n</em> = 0 时,长度为 2<sup>0</sup> = 1。因此,当 <em>n</em> = 0 时,其格雷编码序列为 [0]。</pre>
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
int *grayCode(int n, int *returnSize)
{
if (n < 0)
{
return NULL;
}
int i, count = 1 << n;
int *codes = malloc(count * sizeof(int));
for (i = 0; i < count; i++)
{
codes[i] = (i >> 1) ^ i;
}
*returnSize = 1 << n;
return codes;
}
int main(int argc, char **argv)
{
if (argc != 2)
{
fprintf(stderr, "Usage: ./test n\n");
exit(-1);
}
int i, count;
int *list = grayCode(atoi(argv[1]), &count);
for (i = 0; i < count; i++)
{
printf("%d ", list[i]);
}
printf("\n");
return 0;
}
\ No newline at end of file
{
"type": "code_options",
"author": "CSDN.net",
"source": "solution.md",
"exercise_id": "99b5b43d1851414b81907935a8495cd6"
}
\ No newline at end of file
# 格雷编码
<p>格雷编码是一个二进制数字系统,在该系统中,两个连续的数值仅有一个位数的差异。</p>
<p>给定一个代表编码总位数的非负整数<em> n</em>,打印其格雷编码序列。即使有多个不同答案,你也只需要返回其中一种。</p>
<p>格雷编码序列必须以 0 开头。</p>
<p>&nbsp;</p>
<p><strong>示例 1:</strong></p>
<pre><strong>输入:</strong>&nbsp;2<strong><br />输出:</strong>&nbsp;[0,1,3,2]<strong><br />解释:</strong>00 - 001 - 111 - 310 - 2对于给定的&nbsp;<em>n</em>,其格雷编码序列并不唯一。例如,[0,2,3,1]&nbsp;也是一个有效的格雷编码序列。00 - 010 - 211 - 301 - 1</pre>
<p><strong>示例&nbsp;2:</strong></p>
<pre><strong>输入:</strong>&nbsp;0<strong><br />输出:</strong>&nbsp;[0]<strong><br />解释:</strong> 我们定义格雷编码序列必须以 0 开头。给定编码总位数为 <em>n</em> 的格雷编码序列,其长度为 2<sup>n</sup>。当 <em>n</em> = 0 时,长度为 2<sup>0</sup> = 1。因此,当 <em>n</em> = 0 时,其格雷编码序列为 [0]。</pre>
<p>以下错误的选项是?</p>
## aop
### before
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
```cpp
int main()
{
Solution sol;
vector<int> res;
int n = 2;
res = sol.grayCode(n);
for (auto i : res)
cout << i << " ";
return 0;
}
```
## 答案
```cpp
class Solution
{
public:
vector<int> grayCode(int n)
{
vector<int> res;
if (0 == n)
{
res.push_back(0);
}
else
{
res.push_back(0);
res.push_back(1);
int i = 2;
while (i <= n)
{
int m = res.size();
int cc = pow(2, i - 1);
for (int j = 0; j < m; j++)
{
res.push_back(cc + res[m - j]);
}
i++;
}
}
return res;
}
};
```
## 选项
### A
```cpp
class Solution
{
public:
vector<int> grayCode(int n)
{
int size = 1 << n;
vector<int> res;
for (int i = 0; i < size; i++)
{
int graycode = i ^ (i >> 1);
res.push_back(graycode);
}
return res;
}
};
```
### B
```cpp
class Solution
{
public:
vector<int> grayCode(int n)
{
vector<int> res;
res.push_back(0);
if (n == 0)
return res;
int head = 1;
for (int i = 0; i < n; i++)
{
for (int j = res.size() - 1; j >= 0; j--)
{
res.push_back(head + res[j]);
}
head <<= 1;
}
return res;
}
};
```
### C
```cpp
class Solution
{
public:
vector<int> grayCode(int n)
{
vector<int> res;
for (int i = 0; i < (int)pow(2, n); i++)
res.push_back(i ^ (i >> 1));
return res;
}
};
```
{
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"keywords": [
"leetcode",
"子集 II"
],
"children": [],
"export": [
"solution.json"
],
"title": "子集 II"
}
\ No newline at end of file
<p>给你一个整数数组 <code>nums</code> ,其中可能包含重复元素,请你返回该数组所有可能的子集(幂集)。</p><p>解集 <strong>不能</strong> 包含重复的子集。返回的解集中,子集可以按 <strong>任意顺序</strong> 排列。</p><div class="original__bRMd"><div><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>nums = [1,2,2]<strong><br />输出:</strong>[[],[1],[1,2],[1,2,2],[2],[2,2]]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>nums = [0]<strong><br />输出:</strong>[[],[0]]</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>1 <= nums.length <= 10</code></li> <li><code>-10 <= nums[i] <= 10</code></li></ul></div></div>
\ No newline at end of file
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
vector<vector<int>> subsetsWithDup(vector<int> &nums)
{
vector<vector<int>> res;
sort(nums.begin(), nums.end());
dfs(nums, 0, res);
return res;
}
private:
vector<int> stack;
void dfs(vector<int> &nums, int start, vector<vector<int>> &res)
{
res.push_back(stack);
int last = INT_MIN;
for (int i = start; i < nums.size(); i++)
{
if (last != nums[i])
{
stack.push_back(nums[i]);
dfs(nums, i + 1, res);
stack.pop_back();
}
last = nums[i];
}
}
};
\ No newline at end of file
{
"type": "code_options",
"author": "CSDN.net",
"source": "solution.md",
"exercise_id": "0f85191b5acc4f2fbe194a96390ced2d"
}
\ No newline at end of file
# 子集 II
<p>给你一个整数数组 <code>nums</code> ,其中可能包含重复元素,请你返回该数组所有可能的子集(幂集)。</p><p>解集 <strong>不能</strong> 包含重复的子集。返回的解集中,子集可以按 <strong>任意顺序</strong> 排列。</p><div class="original__bRMd"><div><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>nums = [1,2,2]<strong><br />输出:</strong>[[],[1],[1,2],[1,2,2],[2],[2,2]]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>nums = [0]<strong><br />输出:</strong>[[],[0]]</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>1 <= nums.length <= 10</code></li> <li><code>-10 <= nums[i] <= 10</code></li></ul></div></div>
<p>以下错误的选项是?</p>
## aop
### before
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
```cpp
int main()
{
Solution sol;
vector<vector<int>> res;
vector<int> nums = {1, 2, 2};
res = sol.subsetsWithDup(nums);
for (auto i : res)
{
for (auto j : i)
cout << j << " ";
cout << endl;
}
return 0;
}
```
## 答案
```cpp
class Solution
{
public:
vector<vector<int>> result;
vector<vector<int>> subsetsWithDup(vector<int> &nums)
{
sort(nums.begin(), nums.end());
vector<int> temp;
result.push_back(temp);
getAns(0, nums, result, temp);
return result;
}
void getAns(int start, vector<int> &nums, vector<vector<int>> &result, vector<int> temp)
{
if (start == nums.size() - 1)
{
temp.push_back(nums[start]);
result.push_back(temp);
}
else
{
for (int i = start; i < nums.size(); i++)
{
while (i != 0 && i != start && nums[i] == nums[i - 1])
{
i++;
}
if (i == nums.size())
break;
temp.push_back(nums[i]);
result.push_back(temp);
getAns(i, nums, result, temp);
temp.pop_back();
}
}
}
};
```
## 选项
### A
```cpp
class Solution
{
public:
vector<vector<int>> subsetsWithDup(vector<int> &nums)
{
sort(nums.begin(), nums.end());
vector<vector<int>> res;
vector<int> cur;
for (int i = 0; i <= nums.size(); i++)
{
dfs(res, cur, nums, 0, i);
}
return res;
}
void dfs(vector<vector<int>> &res, vector<int> &cur, vector<int> &nums, int begin, int n)
{
if (cur.size() == n)
{
res.push_back(cur);
return;
}
for (int i = begin; i < nums.size(); i++)
{
if (i > begin && nums[i] == nums[i - 1])
continue;
cur.push_back(nums[i]);
dfs(res, cur, nums, i + 1, n);
cur.pop_back();
}
return;
}
};
```
### B
```cpp
class Solution
{
public:
vector<vector<int>> ans;
vector<int> cur;
vector<int> v;
void dfs(int depth)
{
ans.push_back(cur);
if (depth == v.size())
return;
for (int i = depth; i < v.size(); ++i)
{
if (i > depth && v[i] == v[i - 1])
continue;
cur.push_back(v[i]);
dfs(i + 1);
cur.pop_back();
}
}
vector<vector<int>> subsetsWithDup(vector<int> &nums)
{
sort(nums.begin(), nums.end());
v = nums;
dfs(0);
return ans;
}
};
```
### C
```cpp
class Solution
{
public:
vector<vector<int>> subsetsWithDup(vector<int> &nums)
{
vector<vector<int>> result;
vector<int> item;
set<vector<int>> rset;
result.push_back(item);
sort(nums.begin(), nums.end());
CreatSet(0, result, item, nums, rset);
return result;
}
void CreatSet(int i, vector<vector<int>> &result,
vector<int> &item, vector<int> &nums,
set<vector<int>> &rset)
{
if (i >= nums.size())
return;
item.push_back(nums[i]);
if (rset.find(item) == rset.end())
{
rset.insert(item);
result.push_back(item);
}
CreatSet(i + 1, result, item, nums, rset);
item.pop_back();
CreatSet(i + 1, result, item, nums, rset);
}
};
```
......@@ -4,30 +4,123 @@
## aop
### before
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
```cpp
int main()
{
Solution sol;
vector<int> nums1 = {1, 2, 3, 0, 0, 0};
int m = 3;
vector<int> nums2 = {2, 5, 6};
int n = 3;
sol.merge(nums1, m, nums2, n);
for (auto i : nums1)
cout << i << " ";
return 0;
}
```
## 答案
```cpp
class Solution
{
public:
void merge(vector<int> &nums1, int m, vector<int> &nums2, int n)
{
int i = m - 1;
int j = n - 1;
int k = m + n - 1;
while (i > 0 && j > 0)
{
if (nums1[i] > nums2[j])
{
nums1[k--] = nums1[i--];
}
else
{
nums1[k--] = nums2[j--];
}
}
return;
}
};
```
## 选项
### A
```cpp
class Solution
{
public:
void merge(vector<int> &nums1, int m, vector<int> &nums2, int n)
{
int sum = m + n;
int pos = 0;
if (n < 1)
;
else
{
for (int i = 0; i < sum && n; i++)
{
if (nums2[pos] <= nums1[i] || i >= m)
{
for (int k = nums1.size() - 1; k > i; k--)
{
nums1[k] = nums1[k - 1];
}
nums1[i] = nums2[pos];
pos++;
n--;
m++;
}
}
}
}
};
```
### B
```cpp
class Solution
{
public:
void merge(vector<int> &nums1, int m, vector<int> &nums2, int n)
{
int sum = m + n;
int k = 0;
for (int i = m; i < sum; i++)
{
nums1[i] = nums2[k];
k++;
}
sort(nums1.begin(), nums1.end());
}
};
```
### C
```cpp
class Solution
{
public:
static bool cmp(const int &a, const int &b)
{
return a < b;
}
void merge(vector<int> &nums1, int m, vector<int> &nums2, int n)
{
int sum = m + n;
int k = 0;
for (int i = m; i < sum; i++)
{
nums1[i] = nums2[k];
k++;
}
sort(nums1.begin(), nums1.end(), cmp);
}
};
```
......@@ -11,30 +11,113 @@
## aop
### before
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
```cpp
int main()
{
Solution sol;
vector<int> res;
int n = 2;
res = sol.grayCode(n);
for (auto i : res)
cout << i << " ";
return 0;
}
```
## 答案
```cpp
class Solution
{
public:
vector<int> grayCode(int n)
{
vector<int> res;
if (0 == n)
{
res.push_back(0);
}
else
{
res.push_back(0);
res.push_back(1);
int i = 2;
while (i <= n)
{
int m = res.size();
int cc = pow(2, i - 1);
for (int j = 0; j < m; j++)
{
res.push_back(cc + res[m - j]);
}
i++;
}
}
return res;
}
};
```
## 选项
### A
```cpp
class Solution
{
public:
vector<int> grayCode(int n)
{
int size = 1 << n;
vector<int> res;
for (int i = 0; i < size; i++)
{
int graycode = i ^ (i >> 1);
res.push_back(graycode);
}
return res;
}
};
```
### B
```cpp
class Solution
{
public:
vector<int> grayCode(int n)
{
vector<int> res;
res.push_back(0);
if (n == 0)
return res;
int head = 1;
for (int i = 0; i < n; i++)
{
for (int j = res.size() - 1; j >= 0; j--)
{
res.push_back(head + res[j]);
}
head <<= 1;
}
return res;
}
};
```
### C
```cpp
class Solution
{
public:
vector<int> grayCode(int n)
{
vector<int> res;
for (int i = 0; i < (int)pow(2, n); i++)
res.push_back(i ^ (i >> 1));
return res;
}
};
```
......@@ -4,30 +4,172 @@
## aop
### before
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
```cpp
int main()
{
Solution sol;
vector<vector<int>> res;
vector<int> nums = {1, 2, 2};
res = sol.subsetsWithDup(nums);
for (auto i : res)
{
for (auto j : i)
cout << j << " ";
cout << endl;
}
return 0;
}
```
## 答案
```cpp
class Solution
{
public:
vector<vector<int>> result;
vector<vector<int>> subsetsWithDup(vector<int> &nums)
{
sort(nums.begin(), nums.end());
vector<int> temp;
result.push_back(temp);
getAns(0, nums, result, temp);
return result;
}
void getAns(int start, vector<int> &nums, vector<vector<int>> &result, vector<int> temp)
{
if (start == nums.size() - 1)
{
temp.push_back(nums[start]);
result.push_back(temp);
}
else
{
for (int i = start; i < nums.size(); i++)
{
while (i != 0 && i != start && nums[i] == nums[i - 1])
{
i++;
}
if (i == nums.size())
break;
temp.push_back(nums[i]);
result.push_back(temp);
getAns(i, nums, result, temp);
temp.pop_back();
}
}
}
};
```
## 选项
### A
```cpp
class Solution
{
public:
vector<vector<int>> subsetsWithDup(vector<int> &nums)
{
sort(nums.begin(), nums.end());
vector<vector<int>> res;
vector<int> cur;
for (int i = 0; i <= nums.size(); i++)
{
dfs(res, cur, nums, 0, i);
}
return res;
}
void dfs(vector<vector<int>> &res, vector<int> &cur, vector<int> &nums, int begin, int n)
{
if (cur.size() == n)
{
res.push_back(cur);
return;
}
for (int i = begin; i < nums.size(); i++)
{
if (i > begin && nums[i] == nums[i - 1])
continue;
cur.push_back(nums[i]);
dfs(res, cur, nums, i + 1, n);
cur.pop_back();
}
return;
}
};
```
### B
```cpp
class Solution
{
public:
vector<vector<int>> ans;
vector<int> cur;
vector<int> v;
void dfs(int depth)
{
ans.push_back(cur);
if (depth == v.size())
return;
for (int i = depth; i < v.size(); ++i)
{
if (i > depth && v[i] == v[i - 1])
continue;
cur.push_back(v[i]);
dfs(i + 1);
cur.pop_back();
}
}
vector<vector<int>> subsetsWithDup(vector<int> &nums)
{
sort(nums.begin(), nums.end());
v = nums;
dfs(0);
return ans;
}
};
```
### C
```cpp
class Solution
{
public:
vector<vector<int>> subsetsWithDup(vector<int> &nums)
{
vector<vector<int>> result;
vector<int> item;
set<vector<int>> rset;
result.push_back(item);
sort(nums.begin(), nums.end());
CreatSet(0, result, item, nums, rset);
return result;
}
void CreatSet(int i, vector<vector<int>> &result,
vector<int> &item, vector<int> &nums,
set<vector<int>> &rset)
{
if (i >= nums.size())
return;
item.push_back(nums[i]);
if (rset.find(item) == rset.end())
{
rset.insert(item);
result.push_back(item);
}
CreatSet(i + 1, result, item, nums, rset);
item.pop_back();
CreatSet(i + 1, result, item, nums, rset);
}
};
```
......@@ -30,30 +30,158 @@
## aop
### before
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
```cpp
int main()
{
Solution sol;
int res;
string s = "226";
res = sol.numDecodings(s);
cout << res;
return 0;
}
```
## 答案
```cpp
class Solution
{
public:
int numDecodings(string s)
{
if (s.empty() || s[0] == '0')
return 0;
vector<int> dp(s.size() + 1, 0);
dp[0] = 1;
for (int i = 1; i < dp.size(); ++i)
{
dp[i] = (s[i - 1] == '0') ? 0 : dp[i - 1];
if (i > 1 && (s[i - 2] == '1' || (s[i - 1] == '2' && s[i - 1] <= '6')))
dp[i] += dp[i - 1];
}
return dp[s.size()];
}
};
```
## 选项
### A
```cpp
class Solution
{
public:
int dp(string &s, int i, int j)
{
int n = j - i + 1;
if (n == 0)
return 0;
if (n == 1)
return s[i] == '0' ? 0 : 1;
if (n == 2)
{
if (s[i] == '0')
return 0;
if (s[i] > '2')
return s[j] == '0' ? 0 : 1;
if (s[i] == '2' && s[j] > '6')
return 1;
if (s[j] == '0')
return 1;
return 2;
}
if (s[i] > '2')
return dp(s, i + 1, j);
if (s[i] == '2')
{
if (s[i + 1] == '0')
return dp(s, i + 2, j);
else if (s[i + 1] < '7')
return dp(s, i + 1, j) + dp(s, i + 2, j);
else
return dp(s, i + 1, j);
}
if (s[i] == '0')
return 0;
if (s[i + 1] == '0')
return dp(s, i + 2, j);
return dp(s, i + 1, j) + dp(s, i + 2, j);
}
int numDecodings(string s)
{
return dp(s, 0, s.size() - 1);
}
};
```
### B
```cpp
class Solution
{
public:
int numDecodings(string s)
{
vector<int> nums(s.size());
if (s[0] == '0')
{
return 0;
}
nums[0] = 1;
if (s.size() > 1)
{
if (s[1] != '0')
{
nums[1] += 1;
}
if ((s[0] - '0') * 10 + (s[1] - '0') <= 26)
{
nums[1] += 1;
}
}
for (int i = 2; i < s.size(); i++)
{
if (s[i] != '0')
{
nums[i] += nums[i - 1];
}
if (s[i - 1] != '0' && ((s[i - 1] - '0') * 10 + (s[i] - '0') <= 26))
{
nums[i] += nums[i - 2];
}
}
return nums[s.size() - 1];
}
};
```
### C
```cpp
class Solution
{
public:
int numDecodings(string s)
{
int n = s.size();
if (s.empty())
return 0;
if (s[0] == '0')
return 0;
vector<int> info(n + 1, 0);
info[0] = 1;
info[1] = 1;
for (int i = 2; i < n + 1; ++i)
{
if (s[i - 1] != '0')
info[i] += info[i - 1];
if (s.substr(i - 2, 2) <= "26" && s.substr(i - 2, 2) >= "10")
info[i] += info[i - 2];
}
return info[n];
}
};
```
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册