diff --git "a/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/14.leetcode|\345\217\214\346\214\207\351\222\210/87_\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204/config.json" "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/14.leetcode|\345\217\214\346\214\207\351\222\210/87_\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..aff2291130505ec60cc106b56e18d55ed0e1beef --- /dev/null +++ "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/14.leetcode|\345\217\214\346\214\207\351\222\210/87_\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204/config.json" @@ -0,0 +1,12 @@ +{ + "node_id": "569d5e11c4fc5de7844053d9a733c5e8", + "keywords": [ + "leetcode", + "合并两个有序数组" + ], + "children": [], + "export": [ + "solution.json" + ], + "title": "合并两个有序数组" +} \ No newline at end of file diff --git "a/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/14.leetcode|\345\217\214\346\214\207\351\222\210/87_\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204/desc.html" "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/14.leetcode|\345\217\214\346\214\207\351\222\210/87_\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204/desc.html" new file mode 100644 index 0000000000000000000000000000000000000000..b32983576c8301c236524874a889bb52f401f3f3 --- /dev/null +++ "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/14.leetcode|\345\217\214\346\214\207\351\222\210/87_\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204/desc.html" @@ -0,0 +1 @@ +

给你两个有序整数数组 nums1 nums2,请你将 nums2 合并到 nums1 使 nums1 成为一个有序数组。

初始化 nums1nums2 的元素数量分别为 mn 。你可以假设 nums1 的空间大小等于 m + n,这样它就有足够的空间保存来自 nums2 的元素。

 

示例 1:

输入:nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3
输出:
[1,2,2,3,5,6]

示例 2:

输入:nums1 = [1], m = 1, nums2 = [], n = 0
输出:
[1]

 

提示:

\ No newline at end of file diff --git "a/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/14.leetcode|\345\217\214\346\214\207\351\222\210/87_\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204/solution.cpp" "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/14.leetcode|\345\217\214\346\214\207\351\222\210/87_\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204/solution.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..e5d43b3dd5cc47780ed944d57dd1fcecc966efe1 --- /dev/null +++ "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/14.leetcode|\345\217\214\346\214\207\351\222\210/87_\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204/solution.cpp" @@ -0,0 +1,27 @@ +#include +using namespace std; +class Solution +{ +public: + void merge(vector &nums1, int m, vector &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 diff --git "a/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/14.leetcode|\345\217\214\346\214\207\351\222\210/87_\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204/solution.json" "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/14.leetcode|\345\217\214\346\214\207\351\222\210/87_\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..7de2e65788cb1e8115dbada3186df7b24d928d77 --- /dev/null +++ "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/14.leetcode|\345\217\214\346\214\207\351\222\210/87_\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "CSDN.net", + "source": "solution.md", + "exercise_id": "5c5734c6d9d747b8bc5bb77b4a2ea0a6" +} \ No newline at end of file diff --git "a/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/14.leetcode|\345\217\214\346\214\207\351\222\210/87_\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204/solution.md" "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/14.leetcode|\345\217\214\346\214\207\351\222\210/87_\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..ae4c0751c111379a146090def216f2128f0631cf --- /dev/null +++ "b/data/2.\347\256\227\346\263\225\344\270\255\351\230\266/14.leetcode|\345\217\214\346\214\207\351\222\210/87_\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204/solution.md" @@ -0,0 +1,126 @@ +# 合并两个有序数组 +

给你两个有序整数数组 nums1 nums2,请你将 nums2 合并到 nums1 使 nums1 成为一个有序数组。

初始化 nums1nums2 的元素数量分别为 mn 。你可以假设 nums1 的空间大小等于 m + n,这样它就有足够的空间保存来自 nums2 的元素。

 

示例 1:

输入:nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3
输出:
[1,2,2,3,5,6]

示例 2:

输入:nums1 = [1], m = 1, nums2 = [], n = 0
输出:
[1]

 

提示:

  • nums1.length == m + n
  • nums2.length == n
  • 0 <= m, n <= 200
  • 1 <= m + n <= 200
  • -109 <= nums1[i], nums2[i] <= 109
+

以下错误的选项是?

+## aop +### before +```cpp +#include +using namespace std; +``` +### after +```cpp +int main() +{ + Solution sol; + vector nums1 = {1, 2, 3, 0, 0, 0}; + int m = 3; + vector 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 &nums1, int m, vector &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 &nums1, int m, vector &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 &nums1, int m, vector &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 &nums1, int m, vector &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); + } +}; +``` diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode|\345\212\250\346\200\201\350\247\204\345\210\222/90_\350\247\243\347\240\201\346\226\271\346\263\225/config.json" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode|\345\212\250\346\200\201\350\247\204\345\210\222/90_\350\247\243\347\240\201\346\226\271\346\263\225/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..16b423cc85b17745ccafcd8e6d510e70dedf3700 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode|\345\212\250\346\200\201\350\247\204\345\210\222/90_\350\247\243\347\240\201\346\226\271\346\263\225/config.json" @@ -0,0 +1,12 @@ +{ + "node_id": "569d5e11c4fc5de7844053d9a733c5e8", + "keywords": [ + "leetcode", + "解码方法" + ], + "children": [], + "export": [ + "solution.json" + ], + "title": "解码方法" +} \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode|\345\212\250\346\200\201\350\247\204\345\210\222/90_\350\247\243\347\240\201\346\226\271\346\263\225/desc.html" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode|\345\212\250\346\200\201\350\247\204\345\210\222/90_\350\247\243\347\240\201\346\226\271\346\263\225/desc.html" new file mode 100644 index 0000000000000000000000000000000000000000..2dfc923ff58d7808291e7784e5e5d0f137120b9e --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode|\345\212\250\346\200\201\350\247\204\345\210\222/90_\350\247\243\347\240\201\346\226\271\346\263\225/desc.html" @@ -0,0 +1,27 @@ +

一条包含字母 A-Z 的消息通过以下映射进行了 编码

+
'A' -> 1'B' -> 2...'Z' -> 26
+

解码 已编码的消息,所有数字必须基于上述映射的方法,反向映射回字母(可能有多种方法)。例如,"11106" 可以映射为:

+
    +
  • "AAJF" ,将消息分组为 (1 1 10 6)
  • +
  • "KJF" ,将消息分组为 (11 10 6)
  • +
+

注意,消息不能分组为  (1 11 06) ,因为 "06" 不能映射为 "F" ,这是由于 "6" 和 + "06" 在映射中并不等价。 +

+

给你一个只含数字的 非空 字符串 s ,请计算并返回 解码 方法的 总数

+

题目数据保证答案肯定是一个 32 位 的整数。

+

 

+

示例 1:

+
输入:s = "12"
输出:
2
解释:
它可以解码为 "AB"(1 2)或者 "L"(12)。
+

示例 2:

+
输入:s = "226"
输出:
3
解释:
它可以解码为 "BZ" (2 26), "VF" (22 6), 或者 "BBF" (2 2 6) 。
+

示例 3:

+
输入:s = "0"
输出:
0
解释:
没有字符映射到以 0 开头的数字。含有 0 的有效映射是 'J' -> "10" 和 'T'-> "20" 。由于没有字符,因此没有有效的方法对此进行解码,因为所有数字都需要映射。
+

示例 4:

+
输入:s = "06"
输出:
0
解释:
"06" 不能映射到 "F" ,因为字符串含有前导 0("6" 和 "06" 在映射中并不等价)。
+

 

+

提示:

+
    +
  • 1 <= s.length <= 100
  • +
  • s 只包含数字,并且可能包含前导零。
  • +
\ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode|\345\212\250\346\200\201\350\247\204\345\210\222/90_\350\247\243\347\240\201\346\226\271\346\263\225/solution.cpp" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode|\345\212\250\346\200\201\350\247\204\345\210\222/90_\350\247\243\347\240\201\346\226\271\346\263\225/solution.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..4b2cceb2add5660a1def29d1ac9845f7f658aba9 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode|\345\212\250\346\200\201\350\247\204\345\210\222/90_\350\247\243\347\240\201\346\226\271\346\263\225/solution.cpp" @@ -0,0 +1,36 @@ +#include +#include +#include +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 diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode|\345\212\250\346\200\201\350\247\204\345\210\222/90_\350\247\243\347\240\201\346\226\271\346\263\225/solution.json" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode|\345\212\250\346\200\201\350\247\204\345\210\222/90_\350\247\243\347\240\201\346\226\271\346\263\225/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..b48df91fe13f75ae514a087a5fabe2fd740713b1 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode|\345\212\250\346\200\201\350\247\204\345\210\222/90_\350\247\243\347\240\201\346\226\271\346\263\225/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "CSDN.net", + "source": "solution.md", + "exercise_id": "43470f52b94346fba47c299883a84167" +} \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode|\345\212\250\346\200\201\350\247\204\345\210\222/90_\350\247\243\347\240\201\346\226\271\346\263\225/solution.md" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode|\345\212\250\346\200\201\350\247\204\345\210\222/90_\350\247\243\347\240\201\346\226\271\346\263\225/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..83949d4038856e9bfc934b2868a74addfa042ac8 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/11.leetcode|\345\212\250\346\200\201\350\247\204\345\210\222/90_\350\247\243\347\240\201\346\226\271\346\263\225/solution.md" @@ -0,0 +1,187 @@ +# 解码方法 +

一条包含字母 A-Z 的消息通过以下映射进行了 编码

+
'A' -> 1'B' -> 2...'Z' -> 26
+

解码 已编码的消息,所有数字必须基于上述映射的方法,反向映射回字母(可能有多种方法)。例如,"11106" 可以映射为:

+
    +
  • "AAJF" ,将消息分组为 (1 1 10 6)
  • +
  • "KJF" ,将消息分组为 (11 10 6)
  • +
+

注意,消息不能分组为  (1 11 06) ,因为 "06" 不能映射为 "F" ,这是由于 "6" 和 + "06" 在映射中并不等价。 +

+

给你一个只含数字的 非空 字符串 s ,请计算并返回 解码 方法的 总数

+

题目数据保证答案肯定是一个 32 位 的整数。

+

 

+

示例 1:

+
输入:s = "12"
输出:
2
解释:
它可以解码为 "AB"(1 2)或者 "L"(12)。
+

示例 2:

+
输入:s = "226"
输出:
3
解释:
它可以解码为 "BZ" (2 26), "VF" (22 6), 或者 "BBF" (2 2 6) 。
+

示例 3:

+
输入:s = "0"
输出:
0
解释:
没有字符映射到以 0 开头的数字。含有 0 的有效映射是 'J' -> "10" 和 'T'-> "20" 。由于没有字符,因此没有有效的方法对此进行解码,因为所有数字都需要映射。
+

示例 4:

+
输入:s = "06"
输出:
0
解释:
"06" 不能映射到 "F" ,因为字符串含有前导 0("6" 和 "06" 在映射中并不等价)。
+

 

+

提示:

+
    +
  • 1 <= s.length <= 100
  • +
  • s 只包含数字,并且可能包含前导零。
  • +
+

以下错误的选项是?

+## aop +### before +```cpp +#include +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 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 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 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]; + } +}; +``` diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/12.leetcode|\345\233\236\346\272\257\347\256\227\346\263\225/88_\346\240\274\351\233\267\347\274\226\347\240\201/config.json" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/12.leetcode|\345\233\236\346\272\257\347\256\227\346\263\225/88_\346\240\274\351\233\267\347\274\226\347\240\201/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..c01a30461252c9c49ccb3449e965ddcc7af940aa --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/12.leetcode|\345\233\236\346\272\257\347\256\227\346\263\225/88_\346\240\274\351\233\267\347\274\226\347\240\201/config.json" @@ -0,0 +1,12 @@ +{ + "node_id": "569d5e11c4fc5de7844053d9a733c5e8", + "keywords": [ + "leetcode", + "格雷编码" + ], + "children": [], + "export": [ + "solution.json" + ], + "title": "格雷编码" +} \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/12.leetcode|\345\233\236\346\272\257\347\256\227\346\263\225/88_\346\240\274\351\233\267\347\274\226\347\240\201/desc.html" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/12.leetcode|\345\233\236\346\272\257\347\256\227\346\263\225/88_\346\240\274\351\233\267\347\274\226\347\240\201/desc.html" new file mode 100644 index 0000000000000000000000000000000000000000..aeca3b47c907522eb8d45df66c2a157af1dd3d16 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/12.leetcode|\345\233\236\346\272\257\347\256\227\346\263\225/88_\346\240\274\351\233\267\347\274\226\347\240\201/desc.html" @@ -0,0 +1,8 @@ +

格雷编码是一个二进制数字系统,在该系统中,两个连续的数值仅有一个位数的差异。

+

给定一个代表编码总位数的非负整数 n,打印其格雷编码序列。即使有多个不同答案,你也只需要返回其中一种。

+

格雷编码序列必须以 0 开头。

+

 

+

示例 1:

+
输入: 2
输出:
 [0,1,3,2]
解释:
00 - 001 - 111 - 310 - 2对于给定的 n,其格雷编码序列并不唯一。例如,[0,2,3,1] 也是一个有效的格雷编码序列。00 - 010 - 211 - 301 - 1
+

示例 2:

+
输入: 0
输出:
 [0]
解释:
我们定义格雷编码序列必须以 0 开头。给定编码总位数为 n 的格雷编码序列,其长度为 2n。当 n = 0 时,长度为 20 = 1。因此,当 n = 0 时,其格雷编码序列为 [0]。
\ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/12.leetcode|\345\233\236\346\272\257\347\256\227\346\263\225/88_\346\240\274\351\233\267\347\274\226\347\240\201/solution.cpp" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/12.leetcode|\345\233\236\346\272\257\347\256\227\346\263\225/88_\346\240\274\351\233\267\347\274\226\347\240\201/solution.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..95b2724cbdfceb9317e46abf436cc44c226f5157 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/12.leetcode|\345\233\236\346\272\257\347\256\227\346\263\225/88_\346\240\274\351\233\267\347\274\226\347\240\201/solution.cpp" @@ -0,0 +1,33 @@ +#include +#include +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 diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/12.leetcode|\345\233\236\346\272\257\347\256\227\346\263\225/88_\346\240\274\351\233\267\347\274\226\347\240\201/solution.json" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/12.leetcode|\345\233\236\346\272\257\347\256\227\346\263\225/88_\346\240\274\351\233\267\347\274\226\347\240\201/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..8f3f5336207fa21a4c97a6bdd4234666a34d345a --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/12.leetcode|\345\233\236\346\272\257\347\256\227\346\263\225/88_\346\240\274\351\233\267\347\274\226\347\240\201/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "CSDN.net", + "source": "solution.md", + "exercise_id": "99b5b43d1851414b81907935a8495cd6" +} \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/12.leetcode|\345\233\236\346\272\257\347\256\227\346\263\225/88_\346\240\274\351\233\267\347\274\226\347\240\201/solution.md" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/12.leetcode|\345\233\236\346\272\257\347\256\227\346\263\225/88_\346\240\274\351\233\267\347\274\226\347\240\201/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..9fd8684c225b8d4e62430f0566a313f38e83f4ea --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/12.leetcode|\345\233\236\346\272\257\347\256\227\346\263\225/88_\346\240\274\351\233\267\347\274\226\347\240\201/solution.md" @@ -0,0 +1,123 @@ +# 格雷编码 +

格雷编码是一个二进制数字系统,在该系统中,两个连续的数值仅有一个位数的差异。

+

给定一个代表编码总位数的非负整数 n,打印其格雷编码序列。即使有多个不同答案,你也只需要返回其中一种。

+

格雷编码序列必须以 0 开头。

+

 

+

示例 1:

+
输入: 2
输出:
 [0,1,3,2]
解释:
00 - 001 - 111 - 310 - 2对于给定的 n,其格雷编码序列并不唯一。例如,[0,2,3,1] 也是一个有效的格雷编码序列。00 - 010 - 211 - 301 - 1
+

示例 2:

+
输入: 0
输出:
 [0]
解释:
我们定义格雷编码序列必须以 0 开头。给定编码总位数为 n 的格雷编码序列,其长度为 2n。当 n = 0 时,长度为 20 = 1。因此,当 n = 0 时,其格雷编码序列为 [0]。
+

以下错误的选项是?

+## aop +### before +```cpp +#include +using namespace std; +``` +### after +```cpp +int main() +{ + Solution sol; + vector res; + int n = 2; + res = sol.grayCode(n); + for (auto i : res) + cout << i << " "; + + return 0; +} +``` + +## 答案 +```cpp +class Solution +{ +public: + vector grayCode(int n) + { + vector 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 grayCode(int n) + { + int size = 1 << n; + vector 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 grayCode(int n) + { + vector 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 grayCode(int n) + { + vector res; + for (int i = 0; i < (int)pow(2, n); i++) + res.push_back(i ^ (i >> 1)); + return res; + } +}; +``` diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/12.leetcode|\345\233\236\346\272\257\347\256\227\346\263\225/89_\345\255\220\351\233\206 II/config.json" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/12.leetcode|\345\233\236\346\272\257\347\256\227\346\263\225/89_\345\255\220\351\233\206 II/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..66211c2a24a4824fc3fd21a3445a704420351ad4 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/12.leetcode|\345\233\236\346\272\257\347\256\227\346\263\225/89_\345\255\220\351\233\206 II/config.json" @@ -0,0 +1,12 @@ +{ + "node_id": "569d5e11c4fc5de7844053d9a733c5e8", + "keywords": [ + "leetcode", + "子集 II" + ], + "children": [], + "export": [ + "solution.json" + ], + "title": "子集 II" +} \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/12.leetcode|\345\233\236\346\272\257\347\256\227\346\263\225/89_\345\255\220\351\233\206 II/desc.html" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/12.leetcode|\345\233\236\346\272\257\347\256\227\346\263\225/89_\345\255\220\351\233\206 II/desc.html" new file mode 100644 index 0000000000000000000000000000000000000000..ac437d3e5d918c3c7948f77d705663b76fb2765e --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/12.leetcode|\345\233\236\346\272\257\347\256\227\346\263\225/89_\345\255\220\351\233\206 II/desc.html" @@ -0,0 +1 @@ +

给你一个整数数组 nums ,其中可能包含重复元素,请你返回该数组所有可能的子集(幂集)。

解集 不能 包含重复的子集。返回的解集中,子集可以按 任意顺序 排列。

 

示例 1:

输入:nums = [1,2,2]
输出:
[[],[1],[1,2],[1,2,2],[2],[2,2]]

示例 2:

输入:nums = [0]
输出:
[[],[0]]

 

提示:

  • 1 <= nums.length <= 10
  • -10 <= nums[i] <= 10
\ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/12.leetcode|\345\233\236\346\272\257\347\256\227\346\263\225/89_\345\255\220\351\233\206 II/solution.cpp" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/12.leetcode|\345\233\236\346\272\257\347\256\227\346\263\225/89_\345\255\220\351\233\206 II/solution.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..d9473a1d1d12c4dad4f23aae56bde29a9b53e424 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/12.leetcode|\345\233\236\346\272\257\347\256\227\346\263\225/89_\345\255\220\351\233\206 II/solution.cpp" @@ -0,0 +1,30 @@ +#include +using namespace std; +class Solution +{ +public: + vector> subsetsWithDup(vector &nums) + { + vector> res; + sort(nums.begin(), nums.end()); + dfs(nums, 0, res); + return res; + } +private: + vector stack; + void dfs(vector &nums, int start, vector> &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 diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/12.leetcode|\345\233\236\346\272\257\347\256\227\346\263\225/89_\345\255\220\351\233\206 II/solution.json" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/12.leetcode|\345\233\236\346\272\257\347\256\227\346\263\225/89_\345\255\220\351\233\206 II/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..d76956bfa100f3b41126f954aa3708f1fc420d83 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/12.leetcode|\345\233\236\346\272\257\347\256\227\346\263\225/89_\345\255\220\351\233\206 II/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "CSDN.net", + "source": "solution.md", + "exercise_id": "0f85191b5acc4f2fbe194a96390ced2d" +} \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/12.leetcode|\345\233\236\346\272\257\347\256\227\346\263\225/89_\345\255\220\351\233\206 II/solution.md" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/12.leetcode|\345\233\236\346\272\257\347\256\227\346\263\225/89_\345\255\220\351\233\206 II/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..b260bc3a74e999b99219ebf4572b5a026a66a24c --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/12.leetcode|\345\233\236\346\272\257\347\256\227\346\263\225/89_\345\255\220\351\233\206 II/solution.md" @@ -0,0 +1,175 @@ +# 子集 II +

给你一个整数数组 nums ,其中可能包含重复元素,请你返回该数组所有可能的子集(幂集)。

解集 不能 包含重复的子集。返回的解集中,子集可以按 任意顺序 排列。

 

示例 1:

输入:nums = [1,2,2]
输出:
[[],[1],[1,2],[1,2,2],[2],[2,2]]

示例 2:

输入:nums = [0]
输出:
[[],[0]]

 

提示:

  • 1 <= nums.length <= 10
  • -10 <= nums[i] <= 10
+

以下错误的选项是?

+## aop +### before +```cpp +#include +using namespace std; +``` +### after +```cpp +int main() +{ + Solution sol; + vector> res; + vector 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> result; + vector> subsetsWithDup(vector &nums) + { + sort(nums.begin(), nums.end()); + vector temp; + result.push_back(temp); + getAns(0, nums, result, temp); + return result; + } + void getAns(int start, vector &nums, vector> &result, vector 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> subsetsWithDup(vector &nums) + { + + sort(nums.begin(), nums.end()); + vector> res; + vector cur; + for (int i = 0; i <= nums.size(); i++) + { + dfs(res, cur, nums, 0, i); + } + return res; + } + + void dfs(vector> &res, vector &cur, vector &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> ans; + vector cur; + vector 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> subsetsWithDup(vector &nums) + { + sort(nums.begin(), nums.end()); + v = nums; + dfs(0); + return ans; + } +}; +``` + +### C +```cpp +class Solution +{ +public: + vector> subsetsWithDup(vector &nums) + { + + vector> result; + vector item; + set> rset; + result.push_back(item); + sort(nums.begin(), nums.end()); + CreatSet(0, result, item, nums, rset); + return result; + } + void CreatSet(int i, vector> &result, + vector &item, vector &nums, + set> &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); + } +}; +``` diff --git "a/data_backup/1.leetcode/87_\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204/solution.md" "b/data_backup/1.leetcode/87_\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204/solution.md" index 73ab288487e9ab4bd5c67ae4c6a4f827e2940bdd..ae4c0751c111379a146090def216f2128f0631cf 100644 --- "a/data_backup/1.leetcode/87_\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204/solution.md" +++ "b/data_backup/1.leetcode/87_\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204/solution.md" @@ -4,30 +4,123 @@ ## aop ### before ```cpp - +#include +using namespace std; ``` ### after ```cpp +int main() +{ + Solution sol; + vector nums1 = {1, 2, 3, 0, 0, 0}; + int m = 3; + vector 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 &nums1, int m, vector &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 &nums1, int m, vector &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 &nums1, int m, vector &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 &nums1, int m, vector &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); + } +}; ``` diff --git "a/data_backup/1.leetcode/88_\346\240\274\351\233\267\347\274\226\347\240\201/solution.md" "b/data_backup/1.leetcode/88_\346\240\274\351\233\267\347\274\226\347\240\201/solution.md" index 2b1a1f8e51076f7068df09d23170542a2f404fdf..9fd8684c225b8d4e62430f0566a313f38e83f4ea 100644 --- "a/data_backup/1.leetcode/88_\346\240\274\351\233\267\347\274\226\347\240\201/solution.md" +++ "b/data_backup/1.leetcode/88_\346\240\274\351\233\267\347\274\226\347\240\201/solution.md" @@ -11,30 +11,113 @@ ## aop ### before ```cpp - +#include +using namespace std; ``` ### after ```cpp +int main() +{ + Solution sol; + vector res; + int n = 2; + res = sol.grayCode(n); + for (auto i : res) + cout << i << " "; + return 0; +} ``` ## 答案 ```cpp - +class Solution +{ +public: + vector grayCode(int n) + { + vector 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 grayCode(int n) + { + int size = 1 << n; + vector 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 grayCode(int n) + { + vector 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 grayCode(int n) + { + vector res; + for (int i = 0; i < (int)pow(2, n); i++) + res.push_back(i ^ (i >> 1)); + return res; + } +}; ``` diff --git "a/data_backup/1.leetcode/89_\345\255\220\351\233\206 II/solution.md" "b/data_backup/1.leetcode/89_\345\255\220\351\233\206 II/solution.md" index b0cfc4ef75f575cf8001b71ecd8a498f7d5a8b3b..b260bc3a74e999b99219ebf4572b5a026a66a24c 100644 --- "a/data_backup/1.leetcode/89_\345\255\220\351\233\206 II/solution.md" +++ "b/data_backup/1.leetcode/89_\345\255\220\351\233\206 II/solution.md" @@ -4,30 +4,172 @@ ## aop ### before ```cpp - +#include +using namespace std; ``` ### after ```cpp - +int main() +{ + Solution sol; + vector> res; + vector 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> result; + vector> subsetsWithDup(vector &nums) + { + sort(nums.begin(), nums.end()); + vector temp; + result.push_back(temp); + getAns(0, nums, result, temp); + return result; + } + void getAns(int start, vector &nums, vector> &result, vector 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> subsetsWithDup(vector &nums) + { + + sort(nums.begin(), nums.end()); + vector> res; + vector cur; + for (int i = 0; i <= nums.size(); i++) + { + dfs(res, cur, nums, 0, i); + } + return res; + } + void dfs(vector> &res, vector &cur, vector &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> ans; + vector cur; + vector 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> subsetsWithDup(vector &nums) + { + sort(nums.begin(), nums.end()); + v = nums; + dfs(0); + return ans; + } +}; ``` ### C ```cpp +class Solution +{ +public: + vector> subsetsWithDup(vector &nums) + { + vector> result; + vector item; + set> rset; + result.push_back(item); + sort(nums.begin(), nums.end()); + CreatSet(0, result, item, nums, rset); + return result; + } + void CreatSet(int i, vector> &result, + vector &item, vector &nums, + set> &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); + } +}; ``` diff --git "a/data_backup/1.leetcode/90_\350\247\243\347\240\201\346\226\271\346\263\225/solution.md" "b/data_backup/1.leetcode/90_\350\247\243\347\240\201\346\226\271\346\263\225/solution.md" index 693be83db52aee05abe712a683e955a96c4b6573..83949d4038856e9bfc934b2868a74addfa042ac8 100644 --- "a/data_backup/1.leetcode/90_\350\247\243\347\240\201\346\226\271\346\263\225/solution.md" +++ "b/data_backup/1.leetcode/90_\350\247\243\347\240\201\346\226\271\346\263\225/solution.md" @@ -30,30 +30,158 @@ ## aop ### before ```cpp - +#include +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 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 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 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]; + } +}; ```