diff --git "a/data/1.dailycode\345\210\235\351\230\266/1.cpp/78.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/78.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..0fbc6f9067e68e13ab36bbb7f6bb0fc251d09899 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/78.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-0dc5a625a3914f54be8981be75254bb9", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/1.cpp/78.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/78.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..8918e7e0a9f648348d38f09fa8729f6d1cfdfb4f --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/78.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "4f0286361e18400f8cd1dd9b365c0b2d" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/1.cpp/78.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/78.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..542a7df22b51a1fee0675ae78c9c8f9ca37b07fc --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/78.exercises/solution.md" @@ -0,0 +1,65 @@ +# 合并两个有序链表 + +

将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 

 

示例 1:

输入:l1 = [1,2,4], l2 = [1,3,4]
输出:
[1,1,2,3,4,4]

示例 2:

输入:l1 = [], l2 = []
输出:
[]

示例 3:

输入:l1 = [], l2 = [0]
输出:
[0]

 

提示:

+ +## template + +```cpp +struct ListNode +{ + int val; + ListNode *next; + ListNode(int x) : val(x), next(NULL) {} +}; +class Solution +{ +public: + ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) + { + ListNode h(0, nullptr); + ListNode *p = &h; + while (l1 && l2) + { + ListNode **t; + if (l1->val < l2->val) + t = &l1; + else + t = &l2; + p->next = *t; + p = *t; + *t = (*t)->next; + } + if (l1) + p->next = l1; + else + p->next = l2; + return h.next; + } +}; +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/1.cpp/79.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/79.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..4ba9ee53b387fbd0847bedcd70f299912b78d1f8 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/79.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-95b5f3ea417a4993b1d2de9d0e03333b", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/1.cpp/79.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/79.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..6dc798c4562a2880ad2f32cf399d91b0cedb4ec5 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/79.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "d8072053a01149a9aa42f6b7b0079d38" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/1.cpp/79.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/79.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..2392f37b61aa567f29e33a8242360ff6e63e21f1 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/79.exercises/solution.md" @@ -0,0 +1,63 @@ +# 最后一个单词的长度 + +

给你一个字符串 s,由若干单词组成,单词之间用空格隔开。返回字符串中最后一个单词的长度。如果不存在最后一个单词,请返回 0 。

单词 是指仅由字母组成、不包含任何空格字符的最大子字符串。

 

示例 1:

输入:s = "Hello World"
输出:
5

示例 2:

输入:s = " "
输出:
0

 

提示:

+ +## template + +```cpp +#include +#include +int lengthOfLastWord(char *s) +{ + int len = 0; + while (*s != '\0') + { + if (s[-1] == ' ' && s[0] != ' ') + { + len = 1; + } + else if (*s != ' ') + { + len++; + } + s++; + } + return len; +} +int main(int argc, char **argv) +{ + if (argc != 2) + { + fprintf(stderr, "Usage: ./test word\n"); + exit(-1); + } + printf("%d\n", lengthOfLastWord(argv[1])); + return 0; +} +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/1.cpp/80.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/80.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..46f783e93e1eacb6a0c81205c9ee7dc3a704787a --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/80.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-bdf88ba27c68490495382360648a7224", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/1.cpp/80.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/80.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..8abc09493568735c92353a8c8a788a18bd1ee8c7 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/80.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "39f3737953434bd5a1f2be8c5a98988f" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/1.cpp/80.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/80.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..43aa3a125c40d10005b06136948d20bef52ea812 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/80.exercises/solution.md" @@ -0,0 +1,68 @@ +# 删除排序链表中的重复元素 + +

存在一个按升序排列的链表,给你这个链表的头节点 head ,请你删除所有重复的元素,使每个元素 只出现一次

返回同样按升序排列的结果链表。

 

示例 1:

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

示例 2:

输入:head = [1,1,2,3,3]
输出:
[1,2,3]

 

提示:

+ +## template + +```cpp +#include +using namespace std; +struct ListNode +{ + int val; + ListNode *next; + ListNode() : val(0), next(nullptr) {} + ListNode(int x) : val(x), next(nullptr) {} + ListNode(int x, ListNode *next) : val(x), next(next) {} +}; +class Solution +{ +public: + ListNode *deleteDuplicates(ListNode *head) + { + if (head == nullptr) + { + return nullptr; + } + ListNode *prev = head; + ListNode *p = prev->next; + while (p != nullptr) + { + if (p->val != prev->val) + { + prev->next = p; + prev = p; + } + p = p->next; + } + prev->next = p; + return head; + } +}; +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/1.cpp/81.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/81.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..683924fd2befd260b9a43bf65652740d704b693f --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/81.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-cb2b55c067e043aebdcb06de78ba59b2", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/1.cpp/81.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/81.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..fd02dfc12ab05e16d19e9df8a4bdf5a5c8876292 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/81.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "215a84a365404b8e9661f0441bd654ec" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/1.cpp/81.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/81.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..c430ebe27dcab13ed940d390e5048a66afcb5eb7 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/81.exercises/solution.md" @@ -0,0 +1,48 @@ +# 整数反转 + +

给你一个 32 位的有符号整数 x ,返回将 x 中的数字部分反转后的结果。

如果反转后整数超过 32 位的有符号整数的范围 [−231,  231 − 1] ,就返回 0。

假设环境不允许存储 64 位整数(有符号或无符号)。

 

示例 1:

输入:x = 123
输出:
321

示例 2:

输入:x = -123
输出:
-321

示例 3:

输入:x = 120
输出:
21

示例 4:

输入:x = 0
输出:
0

 

提示:

  • -231 <= x <= 231 - 1
+ +## template + +```cpp +int reverse(int x) +{ + long long int r = 0; + while (x) + { + r = r * 10 + (x % 10); + x /= 10; + } + if (r > 2147483647) + return 0; + if (r < -2147483648) + return 0; + return (int)r; +} +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/1.cpp/82.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/82.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..b8242c2ca04770f38eeb420a29e3580264fbe37a --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/82.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-ef357d9dbd1c474f8e14c9783b786c60", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/1.cpp/82.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/82.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..a6dc68d23eff6949cc82b7ead601e6ab9386525c --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/82.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "90a1217143b94f56b82cb3186ad9c7ff" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/1.cpp/82.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/82.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..93bbad570a63af894156ec1e066986089ee48555 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/82.exercises/solution.md" @@ -0,0 +1,65 @@ +# 相同的树 + +

给你两棵二叉树的根节点 pq ,编写一个函数来检验这两棵树是否相同。

如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。

 

示例 1:

输入:p = [1,2,3], q = [1,2,3]
输出:
true

示例 2:

输入:p = [1,2], q = [1,null,2]
输出:
false

示例 3:

输入:p = [1,2,1], q = [1,1,2]
输出:
false

 

提示:

  • 两棵树上的节点数目都在范围 [0, 100]
  • -104 <= Node.val <= 104
+ +## template + +```cpp +#include +using namespace std; +struct TreeNode +{ + int val; + TreeNode *left; + TreeNode *right; + TreeNode() : val(0), left(nullptr), right(nullptr) {} + TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} +}; +class Solution +{ +public: + bool isSameTree(TreeNode *p, TreeNode *q) + { + if (p == nullptr && q == nullptr) + { + return true; + } + if (p == nullptr || q == nullptr) + { + return false; + } + if (p->val != q->val) + { + return false; + } + return isSameTree(p->left, q->left) && isSameTree(p->right, q->right); + } +}; +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/1.cpp/83.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/83.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..6a87ed81fc4d4849887d2699a60155fd05599ace --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/83.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-ed97f5d02dcb4060a214abf12ffeb9c9", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/1.cpp/83.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/83.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..2c656168251570265c8bebfbe22d0dc93e9237e4 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/83.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "5feffaf9331a467ba63a2b3f664a30e6" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/1.cpp/83.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/83.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..9ac551428d9bb8b8f79080aabeaa99fa9817db65 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/83.exercises/solution.md" @@ -0,0 +1,57 @@ +# 两数之和 + +

给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 的那 两个 整数,并返回它们的数组下标。

你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。

你可以按任意顺序返回答案。

 

示例 1:

输入:nums = [2,7,11,15], target = 9
输出:
[0,1]
解释:
因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。

示例 2:

输入:nums = [3,2,4], target = 6
输出:
[1,2]

示例 3:

输入:nums = [3,3], target = 6
输出:
[0,1]

 

提示:

  • 2 <= nums.length <= 103
  • -109 <= nums[i] <= 109
  • -109 <= target <= 109
  • 只会存在一个有效答案
+ +## template + +```cpp +#include +class Solution +{ +public: + vector twoSum(vector &nums, int target) + { + std::unordered_map hset; + vector r; + for (int i = 0; i < nums.size(); ++i) + { + int c = target - nums[i]; + auto iter = hset.find(c); + if (iter != hset.end() && iter->second != i) + { + r.push_back(i); + r.push_back(iter->second); + return r; + } + hset.insert(std::make_pair(nums[i], i)); + } + return r; + } +}; +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/1.cpp/84.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/84.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..619db902440dd42c0d5c6fc00ce15ff0592a027a --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/84.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-0e2b647ac84544f9920a79176a283316", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/1.cpp/84.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/84.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..15b55ce3dcc85e431dec91d06f9e45d9b6778918 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/84.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "b0ddeec257e645f7a5abbe2406ff3688" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/1.cpp/84.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/84.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..b2dd65796f3941e628252f87522ef5ec5ced7575 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/84.exercises/solution.md" @@ -0,0 +1,107 @@ +# 移除元素 + +
+

给你一个数组 nums 和一个值 val,你需要 原地 + 移除所有数值等于 val 的元素,并返回移除后数组的新长度。

+ +

不要使用额外的数组空间,你必须仅使用 O(1) 额外空间并 原地 修改输入数组

+ +

元素的顺序可以改变。你不需要考虑数组中超出新长度后面的元素。

+ +

 

+ +

说明:

+ +

为什么返回数值是整数,但输出的答案是数组呢?

+ +

请注意,输入数组是以「引用」方式传递的,这意味着在函数里修改输入数组对于调用者是可见的。

+ +

你可以想象内部操作如下:

+ +
// nums 是以“引用”方式传递的。也就是说,不对实参作任何拷贝
+int len = removeElement(nums, val);
+
+// 在函数里修改输入数组对于调用者是可见的。
+// 根据你的函数返回的长度, 它会打印出数组中 该长度范围内 的所有元素。
+for (int i = 0; i < len; i++) {
+    print(nums[i]);
+}
+
+ +

 

+ +

示例 1:

+ +
输入:nums = [3,2,2,3], val = 3
+
输出:
2, nums = [2,2] +
解释:
函数应该返回新的长度 2, 并且 nums 中的前两个元素均为 2。你不需要考虑数组中超出新长度后面的元素。例如,函数返回的新长度为 2 ,而 nums = [2,2,3,3] 或 nums = [2,2,0,0],也会被视作正确答案。 +
+ +

示例 2:

+ +
输入:nums = [0,1,2,2,3,0,4,2], val = 2
+
输出:
5, nums = [0,1,4,0,3] +
解释:
函数应该返回新的长度 5, 并且 nums 中的前五个元素为 0, 1, 3, 0, 4。注意这五个元素可为任意顺序。你不需要考虑数组中超出新长度后面的元素。 +
+ +

 

+ +

提示:

+ +
    +
  • 0 <= nums.length <= 100
  • +
  • 0 <= nums[i] <= 50
  • +
  • 0 <= val <= 100
  • +
+
+ +## template + +```cpp +#include +using namespace std; +class Solution +{ +public: + int removeElement(vector &nums, int val) + { + int count = 0; + for (int i = 0; i < nums.size(); i++) + { + if (nums[i] != val) + { + nums[count++] = nums[i]; + } + } + return count; + } +}; +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/1.cpp/85.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/85.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..5a6a7a1e47fc2cddf62b5fdf196a6758c7bf75cc --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/85.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-7ca3b3eb885c434aaf79d149fbb01125", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/1.cpp/85.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/85.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..249f2102bc3178b208ff17fbbbc2c786a2da9d0c --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/85.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "3be9aec2ef9941e3b0666d3241dbbf8c" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/1.cpp/85.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/85.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..55172f80d593794a8383f6628854d5adc035d218 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/85.exercises/solution.md" @@ -0,0 +1,135 @@ +# 二进制求和 + +

给你两个二进制字符串,返回它们的和(用二进制表示)。

输入为 非空 字符串且只包含数字 1 和 0

 

示例 1:

输入: a = "11", b = "1"
输出:
"100"

示例 2:

输入: a = "1010", b = "1011"
输出:
"10101"

 

提示:

  • 每个字符串仅由字符 '0''1' 组成。
  • 1 <= a.length, b.length <= 10^4
  • 字符串如果不是 "0" ,就都不含前导零。
+ +## template + +```cpp +#include +using namespace std; +class Solution +{ +public: + string addBinary(string a, string b) + { + string res; + int carry = 0; + int i = a.length() - 1; + int j = b.length() - 1; + for (; i >= 0 && j >= 0; i--, j--) + { + if (a[i] == '1' && b[j] == '1') + { + if (carry > 0) + { + res.push_back('1'); + } + else + { + res.push_back('0'); + } + carry = 1; + } + else if (a[i] == '0' && b[j] == '0') + { + if (carry > 0) + { + res.push_back('1'); + } + else + { + res.push_back('0'); + } + carry = 0; + } + else + { + if (carry > 0) + { + res.push_back('0'); + carry = 1; + } + else + { + res.push_back('1'); + carry = 0; + } + } + } + while (i >= 0) + { + if (a[i--] == '1') + { + if (carry > 0) + { + res.push_back('0'); + carry = 1; + } + else + { + res.push_back('1'); + carry = 0; + } + } + else + { + res.push_back(carry + '0'); + carry = 0; + } + } + while (j >= 0) + { + if (b[j--] == '1') + { + if (carry > 0) + { + res.push_back('0'); + carry = 1; + } + else + { + res.push_back('1'); + carry = 0; + } + } + else + { + res.push_back(carry + '0'); + carry = 0; + } + } + if (carry > 0) + { + res.push_back('1'); + } + reverse(res.begin(), res.end()); + return res; + } +}; +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/1.cpp/86.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/86.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..433f722e917e85c3503685e8b0aadf29f61e84c5 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/86.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-16292aaccf4c4364afac2d5a7422d944", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/1.cpp/86.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/86.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..b3471136de1f3add1ddad3a0eed6f2aef2021564 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/86.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "ff05c8260e354e8fb308ace3d4a979bc" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/1.cpp/86.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/86.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..ce43bfa044550ad1f2281a21a838984c39092817 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/86.exercises/solution.md" @@ -0,0 +1,72 @@ +# 二叉树的中序遍历 + +

给定一个二叉树的根节点 root ,返回它的 中序 遍历。

 

示例 1:

输入:root = [1,null,2,3]
输出:
[1,3,2]

示例 2:

输入:root = []
输出:
[]

示例 3:

输入:root = [1]
输出:
[1]

示例 4:

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

示例 5:

输入:root = [1,null,2]
输出:
[1,2]

 

提示:

  • 树中节点数目在范围 [0, 100]
  • -100 <= Node.val <= 100

 

进阶: 递归算法很简单,你可以通过迭代算法完成吗?

+ +## template + +```cpp +#include +#include +struct TreeNode +{ + int val; + struct TreeNode *left; + struct TreeNode *right; +}; +static void traverse(struct TreeNode *node, int *result, int *count) +{ + if (node == NULL) + { + return; + } + traverse(node->left, result, count); + result[*count] = node->val; + (*count)++; + traverse(node->right, result, count); +} +static int *inorderTraversal(struct TreeNode *root, int *returnSize) +{ + if (root == NULL) + { + *returnSize = 0; + return NULL; + } + int count = 0; + int *result = malloc(5000 * sizeof(int)); + traverse(root, result, &count); + *returnSize = count; + return result; +} +int main() +{ + int count = 0; + inorderTraversal(NULL, &count); + return 0; +} +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/1.cpp/87.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/87.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..822e649836b0d47843363e7a9c7a7ad2205d36b1 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/87.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-9b161201e7ea49c0ae6fc36917dc39f4", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/1.cpp/87.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/87.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..eacf9cc04244c419c2e64ee4cef50ac037114ce3 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/87.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "06e011e2d19146f197fc3e063b1675b9" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/1.cpp/87.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/87.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..c7b70b621eedd39bf955060ef1aef98d9e134cad --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/87.exercises/solution.md" @@ -0,0 +1,53 @@ +# 爬楼梯 + +

假设你正在爬楼梯。需要 n 阶你才能到达楼顶。

每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢?

注意:给定 n 是一个正整数。

示例 1:

输入: 2
输出:
2
解释:
有两种方法可以爬到楼顶。1. 1 阶 + 1 阶2. 2 阶

示例 2:

输入: 3
输出:
3
解释:
有三种方法可以爬到楼顶。1. 1 阶 + 1 阶 + 1 阶2. 1 阶 + 2 阶3. 2 阶 + 1 阶
+ +## template + +```cpp +#include +using namespace std; +class Solution +{ +public: + int climbStairs(int n) + { + int a = 1; + int b = 2; + int c = 0; + for (int i = 3; i <= n; i++) + { + c = a + b; + a = b; + b = c; + } + return n == 1 ? a : (n == 2 ? b : c); + } +}; +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/1.cpp/88.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/88.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..bac36d16c15530cabd21d88b614daa57237d84fa --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/88.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-ac8e483d856f4a0289ed197afb19dbfa", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/1.cpp/88.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/88.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..69bb55f02ded92fab33d8847f7fbef1926b4966b --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/88.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "62541471885b4c6da4789a2e6379ab55" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/1.cpp/88.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/88.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..71f11ea59ce839967615e25020c46324d9a3997a --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/88.exercises/solution.md" @@ -0,0 +1,140 @@ +# 罗马数字转整数 + +
+

罗马数字包含以下七种字符: I, V, X, LCD 和 M。 +

+ +
字符          数值
+I             1
+V             5
+X             10
+L             50
+C             100
+D             500
+M             1000
+ +

例如, 罗马数字 2 写做 II ,即为两个并列的 1。12 + 写做 XII ,即为 X + II 。 27 + 写做  XXVII, + 即为 XX + V + II 。

+ +

通常情况下,罗马数字中小的数字在大的数字的右边。但也存在特例,例如 4 不写做 IIII,而是 IV。数字 1 在数字 5 的左边,所表示的数等于大数 5 + 减小数 1 得到的数值 4 。同样地,数字 9 表示为 IX。这个特殊的规则只适用于以下六种情况:

+ +
    +
  • I 可以放在 V (5) 和 X (10) 的左边,来表示 4 和 9。
  • +
  • X 可以放在 L (50) 和 C (100) 的左边,来表示 40 + 和 90。 
  • +
  • C 可以放在 D (500) 和 M (1000) 的左边,来表示 400 + 和 900。
  • +
+ +

给你一个整数,将其转为罗马数字。

+ +

 

+ +

示例 1:

+ +
输入: num = 3
+
输出:
"III"
+ +

示例 2:

+ +
输入: num = 4
+
输出:
"IV"
+ +

示例 3:

+ +
输入: num = 9
+
输出:
"IX"
+ +

示例 4:

+ +
输入: num = 58
+
输出:
"LVIII" +
解释:
L = 50, V = 5, III = 3. +
+ +

示例 5:

+ +
输入: num = 1994
+
输出:
"MCMXCIV" +
解释:
M = 1000, CM = 900, XC = 90, IV = 4.
+ +

 

+ +

提示:

+ +
    +
  • 1 <= num <= 3999
  • +
+
+ +## template + +```cpp +struct rmap +{ + char *r; + int v; + int l; +} units[] = { + {"M", 1000, 1}, + {"CM", 900, 2}, + {"D", 500, 1}, + {"CD", 400, 2}, + {"C", 100, 1}, + {"XC", 90, 2}, + {"L", 50, 1}, + {"XL", 40, 2}, + {"X", 10, 1}, + {"IX", 9, 2}, + {"V", 5, 1}, + {"IV", 4, 2}, + {"I", 1, 1}}; +#include +int romanToInt(char *s) +{ + int len = strlen(s); + char *end = s + len; + int i = 0; + int r = 0; + while (i < 13) + { + if (end - s >= units[i].l && memcmp(s, units[i].r, units[i].l) == 0) + { + r += units[i].v; + s += units[i].l; + } + else + i++; + } + return r; +} +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/1.cpp/89.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/89.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..9621bcf6dea19a682e5862563c5fa3519bddc899 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/89.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-6fd7f55fc75346a79d86180dfced2647", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/1.cpp/89.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/89.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..6b1130d96a6c67fd149b6272253fddea0c3e46ef --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/89.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "ddc7227b307247a98c306ca0724f5104" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/1.cpp/89.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/89.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..9fde6043fcf36aa02950b94adcc2b44bb1fb17a6 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/89.exercises/solution.md" @@ -0,0 +1,57 @@ +# 加一 + +

给定一个由 整数 组成的 非空 数组所表示的非负整数,在该数的基础上加一。

最高位数字存放在数组的首位, 数组中每个元素只存储单个数字。

你可以假设除了整数 0 之外,这个整数不会以零开头。

 

示例 1:

输入:digits = [1,2,3]
输出:
[1,2,4]
解释:
输入数组表示数字 123。

示例 2:

输入:digits = [4,3,2,1]
输出:
[4,3,2,2]
解释:
输入数组表示数字 4321。

示例 3:

输入:digits = [0]
输出:
[1]

 

提示:

  • 1 <= digits.length <= 100
  • 0 <= digits[i] <= 9
+ +## template + +```cpp +#include +using namespace std; +class Solution +{ +public: + vector plusOne(vector &digits) + { + int carry = 1; + vector res; + for (int i = digits.size() - 1; i >= 0; i--) + { + int d = digits[i] + carry; + res.push_back(d % 10); + carry = d / 10; + } + if (carry > 0) + { + res.push_back(carry); + } + reverse(res.begin(), res.end()); + return res; + } +}; +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/1.cpp/90.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/90.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..2dc8e546df5d74bb557252c1d7f3e0ecf87801a1 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/90.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-af8c46da0fd44d8ea538ddd35ded9d31", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/1.cpp/90.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/90.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..b807afad4aae525958ac722eeb1f8f7eea0d8d68 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/90.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "d85116829b2a4fb2b1988b3574df01a8" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/1.cpp/90.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/90.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..5b769571899adc99f985fbcf4beb84a3f8a2f5bc --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/90.exercises/solution.md" @@ -0,0 +1,109 @@ +# 删除有序数组中的重复项 + +
+

给你一个有序数组 nums ,请你 + 原地 删除重复出现的元素,使每个元素 只出现一次 ,返回删除后数组的新长度。

+ +

不要使用额外的数组空间,你必须在 原地 + 修改输入数组 并在使用 O(1) 额外空间的条件下完成。

+ +

 

+ +

说明:

+ +

为什么返回数值是整数,但输出的答案是数组呢?

+ +

请注意,输入数组是以「引用」方式传递的,这意味着在函数里修改输入数组对于调用者是可见的。

+ +

你可以想象内部操作如下:

+ +
// nums 是以“引用”方式传递的。也就是说,不对实参做任何拷贝
+int len = removeDuplicates(nums);
+
+// 在函数里修改输入数组对于调用者是可见的。
+// 根据你的函数返回的长度, 它会打印出数组中 该长度范围内 的所有元素。
+for (int i = 0; i < len; i++) {
+    print(nums[i]);
+}
+
+   + +

示例 1:

+ +
输入:nums = [1,1,2]
+
输出:
2, nums = [1,2] +
解释:
函数应该返回新的长度 2 ,并且原数组 nums 的前两个元素被修改为 1, 2 。不需要考虑数组中超出新长度后面的元素。 +
+ +

示例 2:

+ +
输入:nums = [0,0,1,1,1,2,2,3,3,4]
+
输出:
5, nums = [0,1,2,3,4] +
解释:
函数应该返回新的长度 5 , 并且原数组 nums 的前五个元素被修改为 0, 1, 2, 3, 4 。不需要考虑数组中超出新长度后面的元素。 +
+ +

 

+ +

提示:

+ +
    +
  • 0 <= nums.length <= 3 * 104
  • +
  • -104 <= nums[i] <= 104
  • +
  • nums 已按升序排列
  • +
+ +

 

+
+ +## template + +```cpp +#include +using namespace std; +class Solution +{ +public: + int removeDuplicates(vector &nums) + { + if (nums.size() == 0) + { + return 0; + } + int count = 1; + for (int i = 1; i < nums.size(); i++) + { + if (nums[i - 1] != nums[i]) + { + nums[count++] = nums[i]; + } + } + return count; + } +}; +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/1.cpp/91.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/91.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..0bbd36f56b5d024690243f66e875d3959d286e34 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/91.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-57d9689619c24c7f9f8866dcba6abc07", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/1.cpp/91.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/91.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..214cb1b4859e88112a40419f7b35299da78c0dd7 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/91.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "b1ef2c6993234bfe9cc84664980879a8" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/1.cpp/91.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/91.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..d6ec15d663fbdbffb0550d6dd8810209fd87dd61 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/91.exercises/solution.md" @@ -0,0 +1,72 @@ +# 最长公共前缀 + +

编写一个函数来查找字符串数组中的最长公共前缀。

如果不存在公共前缀,返回空字符串 ""

 

示例 1:

输入:strs = ["flower","flow","flight"]
输出:
"fl"

示例 2:

输入:strs = ["dog","racecar","car"]
输出:
""
解释:
输入不存在公共前缀。

 

提示:

  • 0 <= strs.length <= 200
  • 0 <= strs[i].length <= 200
  • strs[i] 仅由小写英文字母组成
+ +## template + +```cpp +class Solution +{ +public: + string longestCommonPrefix(vector &strs) + { + string lcp; + if (strs.size() == 0) + return lcp; + int min_len = INT_MAX; + int min_idx = 0; + for (int i = 0; i < strs.size(); ++i) + { + auto &s = strs[i]; + if (s.size() < min_len) + { + min_len = s.size(); + min_idx = i; + } + } + auto &smin = strs[min_idx]; + for (int i = 0; i < min_len; ++i) + { + char c = smin[i]; + int j; + for (j = 0; j < strs.size(); ++j) + { + auto &cs = strs[j]; + if (c != cs[i]) + break; + } + if (j == strs.size()) + lcp += c; + else + break; + } + return lcp; + } +}; +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/1.cpp/92.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/92.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..fbbec97863e1541e3d3d66c3a2fb56ecf801887d --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/92.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-ba1669ba67774ce7b96566a3b440560e", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/1.cpp/92.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/92.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..e2a192f101a44f4e6152a146e772b5bcdc34e8e2 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/92.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "46f877111e4b48b48b5b00a2533251c7" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/1.cpp/92.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/92.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..dcdaa54af99d3203b9df9427de0df4cd76ada1e6 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/92.exercises/solution.md" @@ -0,0 +1,57 @@ +# 最大子序和 + +

给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。

 

示例 1:

输入:nums = [-2,1,-3,4,-1,2,1,-5,4]
输出:
6
解释:
连续子数组 [4,-1,2,1] 的和最大,为 6 。

示例 2:

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

示例 3:

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

示例 4:

输入:nums = [-1]
输出:
-1

示例 5:

输入:nums = [-100000]
输出:
-100000

 

提示:

  • 1 <= nums.length <= 3 * 104
  • -105 <= nums[i] <= 105

 

进阶:如果你已经实现复杂度为 O(n) 的解法,尝试使用更为精妙的 分治法 求解。

+ +## template + +```cpp +#include +using namespace std; +class Solution +{ +public: + int maxSubArray(vector &nums) + { + int sum = 0, max_sum = INT_MIN; + for (int i = 0; i < nums.size(); i++) + { + if (sum < 0) + { + sum = nums[i]; + } + else + { + sum += nums[i]; + } + max_sum = max(sum, max_sum); + } + return max_sum; + } +}; +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/1.cpp/93.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/93.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..b6302810382952206d78a261404f2550b1448c2b --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/93.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-90a4e4b0fd9f4c499d1c0243459e8fec", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/1.cpp/93.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/93.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..b2c1303074ec212546c8767e12ee21b8b6b1310d --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/93.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "61f11c4ed47441e58765ebdb7651c3dd" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/1.cpp/93.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/93.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..8bc9f6c8451f7a3a172918f70d72bd99d44bd31a --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/93.exercises/solution.md" @@ -0,0 +1,75 @@ +# x 的平方根 + +

实现 int sqrt(int x) 函数。

+

计算并返回 x 的平方根,其中 x 是非负整数。

+

由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去。

+

示例 1:

+
输入: 4
输出:
2
+

示例 2:

+
输入: 8
输出:
2
说明:
8 的平方根是 2.82842..., 由于返回类型是整数,小数部分将被舍去。
+ +## template + +```cpp +#include +using namespace std; +class Solution +{ +public: + int mySqrt(int x) + { + if (x == 0) + { + return 0; + } + unsigned int lo = 1, hi = x; + unsigned int mid = (lo + hi) / 2; + for (;;) + { + if (mid > x / mid) + { + hi = mid; + } + else + { + if (mid + 1 > x / (mid + 1)) + { + break; + } + else + { + lo = mid; + } + } + mid = (lo + hi) / 2; + } + return mid; + } +}; +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/1.cpp/94.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/94.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..a94d34a80d8f88305012b61835de552bdfcf0c48 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/94.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-87f69ccc9963410daeef13f9ebe40b62", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/1.cpp/94.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/94.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..76ba48c93cde36dd385c7f24a3d1cf6bd6556eb6 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/94.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "e17223f3f0914739909403b6c2c210bf" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/1.cpp/94.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/94.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..ff992c6a4eebe0075b8cf529dc9931d7b213ec78 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/94.exercises/solution.md" @@ -0,0 +1,61 @@ +# 合并两个有序数组 + +

给你两个有序整数数组 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
+ +## template + +```cpp +#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--]; + } + } +}; +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/1.cpp/95.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/95.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..b8dee970489aa23be9da404f2d58637b76a8b993 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/95.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-39b054895565431f9abb655290420ef6", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/1.cpp/95.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/95.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..702e8d8f542221c4be475ac0c3838e10f02a27f1 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/95.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "58dbe67b6b94425393240a137e84b6df" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/1.cpp/95.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/95.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..6066f2d7d2f36487dc3cbc50d50ab7798b9b66f5 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/95.exercises/solution.md" @@ -0,0 +1,58 @@ +# 搜索插入位置 + +

给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。

你可以假设数组中无重复元素。

示例 1:

输入: [1,3,5,6], 5
输出:
2

示例 2:

输入: [1,3,5,6], 2
输出:
1

示例 3:

输入: [1,3,5,6], 7
输出:
4

示例 4:

输入: [1,3,5,6], 0
输出:
0
+ +## template + +```cpp +#include +using namespace std; +class Solution +{ +public: + int searchInsert(vector &nums, int target) + { + int lo = -1; + int hi = nums.size(); + while (lo + 1 < hi) + { + int mid = lo + (hi - lo) / 2; + if (target > nums[mid]) + { + lo = mid; + } + else + { + hi = mid; + } + } + return hi; + } +}; +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/1.cpp/96.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/96.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..c50bc03dd6e2173de090431a9db5233be7696321 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/96.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-3f9bee77ffa14445bcbdeccdfc9d8999", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/1.cpp/96.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/96.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..228201efcd858ccb824a6e77dad6763d0ef32939 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/96.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "6bfdb268efd0451d815311a3e13ed748" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/1.cpp/96.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/96.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..6f9b8ad08b12df332613cdddeebc43a44ae70f5e --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/96.exercises/solution.md" @@ -0,0 +1,48 @@ +# 回文数 + +

给你一个整数 x ,如果 x 是一个回文整数,返回 true ;否则,返回 false

回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。例如,121 是回文,而 123 不是。

 

示例 1:

输入:x = 121
输出:
true

示例 2:

输入:x = -121
输出:
false
解释:
从左向右读, 为 -121 。 从右向左读, 为 121- 。因此它不是一个回文数。

示例 3:

输入:x = 10
输出:
false
解释:
从右向左读, 为 01 。因此它不是一个回文数。

示例 4:

输入:x = -101
输出:
false

 

提示:

  • -231 <= x <= 231 - 1

 

进阶:你能不将整数转为字符串来解决这个问题吗?

+ +## template + +```cpp +bool isPalindrome(int x) +{ + if (x < 0) + return false; + char r[11]; + int n = snprintf(r, 11, "%d", x); + int i; + for (i = 0; i < n / 2; i++) + { + if (r[i] != r[n - i - 1]) + return false; + } + return true; +} +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/1.cpp/97.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/97.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..5c7f6619d7149234a099a76518e562b70e54577f --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/97.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-7c185a60846747898557661fb914b567", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/1.cpp/97.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/97.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..e03cd3f67112c78bc5dca19cddd2e42c1f2bf29e --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/97.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "87b8a7b259d9495b8227d76d8a704436" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/1.cpp/97.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/97.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..4afdf6a04ab1b2b40038e316f2c4d41e3046ec90 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/1.cpp/97.exercises/solution.md" @@ -0,0 +1,69 @@ +# 有效的括号 + +

给定一个只包括 '('')''{''}''['']' 的字符串 s ,判断字符串是否有效。

有效字符串需满足:

  1. 左括号必须用相同类型的右括号闭合。
  2. 左括号必须以正确的顺序闭合。

 

示例 1:

输入:s = "()"
输出:
true

示例 2:

输入:s = "()[]{}"
输出:
true

示例 3:

输入:s = "(]"
输出:
false

示例 4:

输入:s = "([)]"
输出:
false

示例 5:

输入:s = "{[]}"
输出:
true

 

提示:

  • 1 <= s.length <= 104
  • s 仅由括号 '()[]{}' 组成
+ +## template + +```cpp +#include +char ascii_tab[128]; +class Solution +{ +public: + bool isValid(string s) + { + if (s.size() == 0) + return true; + std::stack st; + ascii_tab['('] = 11; + ascii_tab['{'] = 12; + ascii_tab['['] = 13; + ascii_tab[')'] = 21; + ascii_tab['}'] = 22; + ascii_tab[']'] = 23; + for (auto c : s) + { + char n = ascii_tab[c]; + if (n < 20) + st.push(n); + else + { + if (st.empty()) + return false; + if (n != st.top() + 10) + return false; + st.pop(); + } + } + if (st.empty()) + return true; + return false; + } +}; +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/29.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/29.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..1772848435a0390b0615c51e0d3adf6437b596ce --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/29.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-79825ba736e2491eba7fa79d17fee5cc", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/29.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/29.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..8c6758ca217e6690210bc9ad53e2293cc3ca98fb --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/29.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "1ba2a81cf8f54c199de23ea06e68388b" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/29.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/2.java/29.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..6211b94be3f97ecf1127c151249e78bc84546db3 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/29.exercises/solution.md" @@ -0,0 +1,70 @@ +# 合并两个有序链表 + +

将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 

 

示例 1:

输入:l1 = [1,2,4], l2 = [1,3,4]
输出:
[1,1,2,3,4,4]

示例 2:

输入:l1 = [], l2 = []
输出:
[]

示例 3:

输入:l1 = [], l2 = [0]
输出:
[0]

 

提示:

  • 两个链表的节点数目范围是 [0, 50]
  • -100 <= Node.val <= 100
  • l1l2 均按 非递减顺序 排列
+ +## template + +```java +public class ListNode { + int val; + ListNode next; + ListNode() { + } + ListNode(int val) { + this.val = val; + } + ListNode(int val, ListNode next) { + this.val = val; + this.next = next; + } +} +class Solution { + public ListNode mergeTwoLists(ListNode l1, ListNode l2) { + ListNode h = new ListNode(0, null); + ListNode p = h; + while (l1 != null && l2 != null) { + if (l1.val < l2.val) { + p.next = l1; + p = l1; + l1 = l1.next; + } else { + p.next = l2; + p = l2; + l2 = l2.next; + } + } + if (l1 != null) { + p.next = l1; + } else { + p.next = l2; + } + return h.next; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/30.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/30.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..07baa204718387e77da81e2bd1607582dde5b0dd --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/30.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-f183ba2c39864169b9a6ed97716e5cbe", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/30.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/30.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..e454867ee995b8680ce7c153a7c0536eb611f970 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/30.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "bf91a030cce14ed6a9000500c43d3444" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/30.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/2.java/30.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..264f5e6e8089432fc98ee33f09ac0944b4d5181b --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/30.exercises/solution.md" @@ -0,0 +1,47 @@ +# 最后一个单词的长度 + +

给你一个字符串 s,由若干单词组成,单词之间用空格隔开。返回字符串中最后一个单词的长度。如果不存在最后一个单词,请返回 0 。

单词 是指仅由字母组成、不包含任何空格字符的最大子字符串。

 

示例 1:

输入:s = "Hello World"
输出:
5

示例 2:

输入:s = " "
输出:
0

 

提示:

  • 1 <= s.length <= 104
  • s 仅有英文字母和空格 ' ' 组成
+ +## template + +```java +class Solution { + public int lengthOfLastWord(String s) { + int count = 0; + for (int i = s.length() - 1; i >= 0; i--) { + if (s.charAt(i) != ' ') { + count++; + } else if (count > 0) { + return count; + } + } + return count; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/31.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/31.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..bc01a7f97035700e59f6367fd156e21b61bfc987 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/31.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-2a13f5f56d9143b697bf4e9993fc43e0", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/31.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/31.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..88ee85c775e7452de38c26bbc0946f62e343c767 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/31.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "ce7fcea6474f4e9295f4de3fe4c3678b" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/31.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/2.java/31.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..9a8668d4e7e6586fc8a6e5e9894d1dbff73ceed1 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/31.exercises/solution.md" @@ -0,0 +1,52 @@ +# 删除排序链表中的重复元素 + +

存在一个按升序排列的链表,给你这个链表的头节点 head ,请你删除所有重复的元素,使每个元素 只出现一次

返回同样按升序排列的结果链表。

 

示例 1:

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

示例 2:

输入:head = [1,1,2,3,3]
输出:
[1,2,3]

 

提示:

  • 链表中节点数目在范围 [0, 300]
  • -100 <= Node.val <= 100
  • 题目数据保证链表已经按升序排列
+ +## template + +```java +public class ListNode { + int val; + ListNode next; + ListNode(int x) { + val = x; + } +} +class Solution { + public ListNode deleteDuplicates(ListNode head) { + if (head == null || head.next == null) { + return head; + } + head.next = deleteDuplicates(head.next); + if (head.val == head.next.val) + head = head.next; + return head; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/32.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/32.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..0e173041f5fa0d73a79fb34ad69e9362782d38c0 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/32.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-0bd05a83155447e3a502e650c18ce7d4", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/32.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/32.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..35a50a26873cb1c49ec6e023901e6a84425ce3aa --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/32.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "1b3f572ac66b4de3ae2cc64a31250347" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/32.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/2.java/32.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..0a9324eb3e1c4d85f75d90358e8ef69ca84446da --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/32.exercises/solution.md" @@ -0,0 +1,52 @@ +# 整数反转 + +

给你一个 32 位的有符号整数 x ,返回将 x 中的数字部分反转后的结果。

如果反转后整数超过 32 位的有符号整数的范围 [−231,  231 − 1] ,就返回 0。

假设环境不允许存储 64 位整数(有符号或无符号)。

 

示例 1:

输入:x = 123
输出:
321

示例 2:

输入:x = -123
输出:
-321

示例 3:

输入:x = 120
输出:
21

示例 4:

输入:x = 0
输出:
0

 

提示:

  • -231 <= x <= 231 - 1
+ +## template + +```java +class Solution { + public int reverse(int x) { + long xx = x; + long r; + long y = 0; + boolean sign = xx < 0; + while (xx != 0) { + r = xx % 10; + y = y * 10 + r; + if (sign) { + xx = (long) Math.ceil(xx / 10); + } else { + xx = (long) Math.floor(xx / 10); + } + } + return y > Integer.MAX_VALUE || y < Integer.MIN_VALUE ? 0 : (int) y; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/33.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/33.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..640a914010d60e28f115ac6cdaf0b01b78f83e1c --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/33.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-b79f19cc66174f4c9f98c05591dee625", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/33.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/33.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..b9b1e41a2e9370e52cd15fcedca159d28e045e5c --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/33.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "9481c9692be8440aa11e46926b482400" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/33.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/2.java/33.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..17db44cbe0a1c4fcedf9a24439f14a7884639f01 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/33.exercises/solution.md" @@ -0,0 +1,65 @@ +# 相同的树 + +

给你两棵二叉树的根节点 pq ,编写一个函数来检验这两棵树是否相同。

如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。

 

示例 1:

输入:p = [1,2,3], q = [1,2,3]
输出:
true

示例 2:

输入:p = [1,2], q = [1,null,2]
输出:
false

示例 3:

输入:p = [1,2,1], q = [1,1,2]
输出:
false

 

提示:

  • 两棵树上的节点数目都在范围 [0, 100]
  • -104 <= Node.val <= 104
+ +## template + +```java +public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode() { + } + + TreeNode(int val) { + this.val = val; + } + + TreeNode(int val, TreeNode left, TreeNode right) { + this.val = val; + this.left = left; + this.right = right; + } +} + +class Solution { + public boolean isSameTree(TreeNode p, TreeNode q) { + if (p == null && q == null) { + return true; + } + if (p != null && q != null && p.val == q.val) { + return isSameTree(p.left, q.left) && isSameTree(p.right, q.right); + } else { + return false; + } + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/34.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/34.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..91557384373eeffbc13bf9c54448ef6eeff0851e --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/34.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-d8565e4b363749c0940b45c69f3dcd97", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/34.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/34.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..c7b04b911ed6df260b79ddbc94de94a50e28da29 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/34.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "a2d8971de0804ac99686ba33d3ab7a60" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/34.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/2.java/34.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..c0910c5231a75651d9595d5b1aba4a4756aa1b53 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/34.exercises/solution.md" @@ -0,0 +1,48 @@ +# 两数之和 + +

给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 的那 两个 整数,并返回它们的数组下标。

你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。

你可以按任意顺序返回答案。

 

示例 1:

输入:nums = [2,7,11,15], target = 9
输出:
[0,1]
解释:
因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。

示例 2:

输入:nums = [3,2,4], target = 6
输出:
[1,2]

示例 3:

输入:nums = [3,3], target = 6
输出:
[0,1]

 

提示:

  • 2 <= nums.length <= 103
  • -109 <= nums[i] <= 109
  • -109 <= target <= 109
  • 只会存在一个有效答案
+ +## template + +```java +class Solution { + public int[] twoSum(int[] nums, int target) { + Map cache = new HashMap<>(); + for (int i = 0; i < nums.length; i++) { + int distance = target - nums[i]; + if (cache.containsKey(distance)) { + return new int[] { cache.get(distance), i }; + } else { + cache.put(nums[i], i); + } + } + return new int[] {}; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/35.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/35.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..2163a6d6a5266ab49868571668618a410a3315d0 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/35.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-bf0a6a2dd04c4a3ba85c57476e8b26db", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/35.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/35.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..e78b2d6451db793c04e98c9fbd69b9329d6ca8be --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/35.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "0f9d647aa15c49ec8d407eb7e5224214" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/35.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/2.java/35.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..83c0fa7e7bbae1c0c5157663f845437b5bab90d6 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/35.exercises/solution.md" @@ -0,0 +1,106 @@ +# 移除元素 + +
+

给你一个数组 nums 和一个值 val,你需要 原地 + 移除所有数值等于 val 的元素,并返回移除后数组的新长度。

+ +

不要使用额外的数组空间,你必须仅使用 O(1) 额外空间并 原地 修改输入数组

+ +

元素的顺序可以改变。你不需要考虑数组中超出新长度后面的元素。

+ +

 

+ +

说明:

+ +

为什么返回数值是整数,但输出的答案是数组呢?

+ +

请注意,输入数组是以「引用」方式传递的,这意味着在函数里修改输入数组对于调用者是可见的。

+ +

你可以想象内部操作如下:

+ +
// nums 是以“引用”方式传递的。也就是说,不对实参作任何拷贝
+int len = removeElement(nums, val);
+
+// 在函数里修改输入数组对于调用者是可见的。
+// 根据你的函数返回的长度, 它会打印出数组中 该长度范围内 的所有元素。
+for (int i = 0; i < len; i++) {
+    print(nums[i]);
+}
+
+ +

 

+ +

示例 1:

+ +
输入:nums = [3,2,2,3], val = 3
+
输出:
2, nums = [2,2] +
解释:
函数应该返回新的长度 2, 并且 nums 中的前两个元素均为 2。你不需要考虑数组中超出新长度后面的元素。例如,函数返回的新长度为 2 ,而 nums = [2,2,3,3] 或 nums = [2,2,0,0],也会被视作正确答案。 +
+ +

示例 2:

+ +
输入:nums = [0,1,2,2,3,0,4,2], val = 2
+
输出:
5, nums = [0,1,4,0,3] +
解释:
函数应该返回新的长度 5, 并且 nums 中的前五个元素为 0, 1, 3, 0, 4。注意这五个元素可为任意顺序。你不需要考虑数组中超出新长度后面的元素。 +
+ +

 

+ +

提示:

+ +
    +
  • 0 <= nums.length <= 100
  • +
  • 0 <= nums[i] <= 50
  • +
  • 0 <= val <= 100
  • +
+
+ +## template + +```java +class Solution { + public int removeElement(int[] nums, int val) { + + int len = nums.length; + for (int i = 0; i < len;) { + if (nums[i] == val) { + nums[i] = nums[len - 1]; + len--; + } else { + i++; + } + + } + return len; + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/36.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/36.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..fc42aa78ef090b295e28158c96e587d1fc42c3e7 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/36.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-9b564171d7c14b87b6b01b3fdb8e4ddc", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/36.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/36.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..5170aacd9e551736592a2ada354b8fbe83a49ce3 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/36.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "e73f678b535c4b27a53a97d413ae326b" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/36.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/2.java/36.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..e325ae1ce4edf98dc80c9ce1d7b9546ee88caa9f --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/36.exercises/solution.md" @@ -0,0 +1,88 @@ +# 二进制求和 + +

给你两个二进制字符串,返回它们的和(用二进制表示)。

输入为 非空 字符串且只包含数字 1 和 0

 

示例 1:

输入: a = "11", b = "1"
输出:
"100"

示例 2:

输入: a = "1010", b = "1011"
输出:
"10101"

 

提示:

  • 每个字符串仅由字符 '0''1' 组成。
  • 1 <= a.length, b.length <= 10^4
  • 字符串如果不是 "0" ,就都不含前导零。
+ +## template + +```java +class Solution { + public String addBinary(String a, String b) { + + StringBuffer s1 = new StringBuffer(a); + s1.reverse(); + StringBuffer s2 = new StringBuffer(b); + s2.reverse(); + + if (s1.length() > s2.length()) { + int n = s1.length() - s2.length(); + for (int i = 0; i < n; i++) { + s2.append('0'); + } + } else if (s1.length() < s2.length()) { + int n = s2.length() - s1.length(); + for (int i = 0; i < n; i++) { + s1.append('0'); + } + } + + StringBuffer stringBuffer = new StringBuffer(""); + int i = 0; + char flag = '0'; + while (i < s1.length() && i < s2.length()) { + if (flag == '0') { + if (s1.charAt(i) == s2.charAt(i) && s1.charAt(i) == '1') { + flag = '1'; + stringBuffer.append('0'); + } else if (s1.charAt(i) == s2.charAt(i) && s1.charAt(i) == '0') { + stringBuffer.append('0'); + } else { + stringBuffer.append('1'); + } + } else { + if (s1.charAt(i) == s2.charAt(i) && s1.charAt(i) == '1') { + flag = '1'; + stringBuffer.append('1'); + } else if (s1.charAt(i) == s2.charAt(i) && s1.charAt(i) == '0') { + flag = '0'; + stringBuffer.append('1'); + } else { + flag = '1'; + stringBuffer.append('0'); + } + } + i++; + } + if (flag == '1') { + stringBuffer.append(flag); + } + stringBuffer.reverse(); + return stringBuffer.toString(); + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/37.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/37.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..1d1f5d1ddb9183cb044f88ed9794d7b39796beed --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/37.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-80b8bca4019b47e482bfb940010535b1", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/37.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/37.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..035a03bb870974f515dd261f25a78e1b3779a9b9 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/37.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "d2415dab730e4e27ae1a40c9c99eb3d5" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/37.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/2.java/37.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..f153b13a7d005e0fed44c8caea8a22bbe9c3b10b --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/37.exercises/solution.md" @@ -0,0 +1,64 @@ +# 二叉树的中序遍历 + +

给定一个二叉树的根节点 root ,返回它的 中序 遍历。

 

示例 1:

输入:root = [1,null,2,3]
输出:
[1,3,2]

示例 2:

输入:root = []
输出:
[]

示例 3:

输入:root = [1]
输出:
[1]

示例 4:

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

示例 5:

输入:root = [1,null,2]
输出:
[1,2]

 

提示:

  • 树中节点数目在范围 [0, 100]
  • -100 <= Node.val <= 100

 

进阶: 递归算法很简单,你可以通过迭代算法完成吗?

+ +## template + +```java + +public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode(int x) { + val = x; + } +} + +class Solution { + public List inorderTraversal(TreeNode root) { + List list = new ArrayList<>(); + Stack stack = new Stack<>(); + TreeNode cur = root; + while (cur != null || !stack.isEmpty()) { + if (cur != null) { + stack.push(cur); + cur = cur.left; + } else { + cur = stack.pop(); + list.add(cur.val); + cur = cur.right; + } + } + return list; + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/38.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/38.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..305c2ea972181c011409bed7b23600f91ae67eac --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/38.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-750be61c5c3d4a8593e131e6bc01b265", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/38.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/38.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..670d4ef01932769f7dae5ad860b2e156d08cc358 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/38.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "fc6e5fcd26ec4e82af2abf51e85899ae" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/38.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/2.java/38.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..e00289c325742b8ae62e3fe87edc0d034a38b2d2 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/38.exercises/solution.md" @@ -0,0 +1,46 @@ +# 爬楼梯 + +

假设你正在爬楼梯。需要 n 阶你才能到达楼顶。

每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢?

注意:给定 n 是一个正整数。

示例 1:

输入: 2
输出:
2
解释:
有两种方法可以爬到楼顶。1. 1 阶 + 1 阶2. 2 阶

示例 2:

输入: 3
输出:
3
解释:
有三种方法可以爬到楼顶。1. 1 阶 + 1 阶 + 1 阶2. 1 阶 + 2 阶3. 2 阶 + 1 阶
+ +## template + +```java +class Solution { + public int climbStairs(int n) { + int[] num = new int[n + 1]; + num[0] = 1; + num[1] = 1; + for (int i = 2; i <= n; i++) { + num[i] = num[i - 1] + num[i - 2]; + } + return num[n]; + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/39.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/39.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..bcf6638f06e3d956df22c5bf1fd53eb0241e8fd1 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/39.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-9c76a186f2db4ad8a38b3168c0894d48", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/39.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/39.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..7ec5b9573624aed450c653c77dbf8a2aabb127f3 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/39.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "370027f8c2c749d4bbdd2131203bec37" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/39.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/2.java/39.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..fa3e8b1a5c5a21aeebaad6f9839841b27bb65c89 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/39.exercises/solution.md" @@ -0,0 +1,172 @@ +# 罗马数字转整数 + +
+

罗马数字包含以下七种字符: I, V, X, LCD 和 M。 +

+ +
字符          数值
+I             1
+V             5
+X             10
+L             50
+C             100
+D             500
+M             1000
+ +

例如, 罗马数字 2 写做 II ,即为两个并列的 1。12 + 写做 XII ,即为 X + II 。 27 + 写做  XXVII, + 即为 XX + V + II 。

+ +

通常情况下,罗马数字中小的数字在大的数字的右边。但也存在特例,例如 4 不写做 IIII,而是 IV。数字 1 在数字 5 的左边,所表示的数等于大数 5 + 减小数 1 得到的数值 4 。同样地,数字 9 表示为 IX。这个特殊的规则只适用于以下六种情况:

+ +
    +
  • I 可以放在 V (5) 和 X (10) 的左边,来表示 4 和 9。
  • +
  • X 可以放在 L (50) 和 C (100) 的左边,来表示 40 + 和 90。 
  • +
  • C 可以放在 D (500) 和 M (1000) 的左边,来表示 400 + 和 900。
  • +
+ +

给你一个整数,将其转为罗马数字。

+ +

 

+ +

示例 1:

+ +
输入: num = 3
+
输出:
"III"
+ +

示例 2:

+ +
输入: num = 4
+
输出:
"IV"
+ +

示例 3:

+ +
输入: num = 9
+
输出:
"IX"
+ +

示例 4:

+ +
输入: num = 58
+
输出:
"LVIII" +
解释:
L = 50, V = 5, III = 3. +
+ +

示例 5:

+ +
输入: num = 1994
+
输出:
"MCMXCIV" +
解释:
M = 1000, CM = 900, XC = 90, IV = 4.
+ +

 

+ +

提示:

+ +
    +
  • 1 <= num <= 3999
  • +
+
+ +## template + +```java +class Solution { + public int romanToInt(String s) { + int n = 0; + for (int i = 0; i < s.length();) { + char c = s.charAt(i); + if (c == 'I') { + if (i + 1 < s.length()) { + if (s.charAt(i + 1) == 'V') { + n += 4; + i += 2; + } else if (s.charAt(i + 1) == 'X') { + n += 9; + i += 2; + } else { + n += 1; + i++; + } + } else { + n += 1; + i++; + } + } else if (c == 'X') { + if (i + 1 < s.length()) { + if (s.charAt(i + 1) == 'L') { + n += 40; + i += 2; + } else if (s.charAt(i + 1) == 'C') { + n += 90; + i += 2; + } else { + n += 10; + i++; + } + } else { + n += 10; + i++; + } + } else if (c == 'C') { + if (i + 1 < s.length()) { + if (s.charAt(i + 1) == 'D') { + n += 400; + i += 2; + } else if (s.charAt(i + 1) == 'M') { + n += 900; + i += 2; + } else { + n += 100; + i++; + } + } else { + n += 100; + i++; + } + } else if (c == 'V') { + n += 5; + i++; + } else if (c == 'L') { + n += 50; + i++; + } else if (c == 'D') { + n += 500; + i++; + } else if (c == 'M') { + n += 1000; + i++; + } + } + return n; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/40.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/40.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..e4b33774095471714defb23187d19c734d4b8643 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/40.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-91beb8ab1dc344a7b37703775f6c8509", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/40.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/40.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..6ca8476135c41bf23624a862af573438e68b3046 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/40.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "ec09729c69c242748331fedfaff60803" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/40.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/2.java/40.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..30a28edeaa8a783ff9a28a5e81237566ed71b447 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/40.exercises/solution.md" @@ -0,0 +1,49 @@ +# 加一 + +

给定一个由 整数 组成的 非空 数组所表示的非负整数,在该数的基础上加一。

最高位数字存放在数组的首位, 数组中每个元素只存储单个数字。

你可以假设除了整数 0 之外,这个整数不会以零开头。

 

示例 1:

输入:digits = [1,2,3]
输出:
[1,2,4]
解释:
输入数组表示数字 123。

示例 2:

输入:digits = [4,3,2,1]
输出:
[4,3,2,2]
解释:
输入数组表示数字 4321。

示例 3:

输入:digits = [0]
输出:
[1]

 

提示:

  • 1 <= digits.length <= 100
  • 0 <= digits[i] <= 9
+ +## template + +```java +class Solution { + public int[] plusOne(int[] digits) { + int i = digits.length - 1; + while (i >= 0 && (digits[i] = digits[i] + 1) == 10) { + digits[i] = 0; + i--; + } + if (digits[0] == 0) { + int[] temp = new int[digits.length + 1]; + temp[0] = 1; + return temp; + } + return digits; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/41.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/41.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..acef2f718583c99ec5b3617a4432b9236661db81 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/41.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-849fbcc215a6416db0db45225ec3d51a", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/41.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/41.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..ce0342b60452ad4ecbcaa0c15240b8d6680c2770 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/41.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "2d8b0c5a16364b9c92c4df3758a2b4e7" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/41.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/2.java/41.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..ff21da3458ee5f7de2ca4508ee08464bea73d2de --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/41.exercises/solution.md" @@ -0,0 +1,103 @@ +# 删除有序数组中的重复项 + +
+

给你一个有序数组 nums ,请你 + 原地 删除重复出现的元素,使每个元素 只出现一次 ,返回删除后数组的新长度。

+ +

不要使用额外的数组空间,你必须在 原地 + 修改输入数组 并在使用 O(1) 额外空间的条件下完成。

+ +

 

+ +

说明:

+ +

为什么返回数值是整数,但输出的答案是数组呢?

+ +

请注意,输入数组是以「引用」方式传递的,这意味着在函数里修改输入数组对于调用者是可见的。

+ +

你可以想象内部操作如下:

+ +
// nums 是以“引用”方式传递的。也就是说,不对实参做任何拷贝
+int len = removeDuplicates(nums);
+
+// 在函数里修改输入数组对于调用者是可见的。
+// 根据你的函数返回的长度, 它会打印出数组中 该长度范围内 的所有元素。
+for (int i = 0; i < len; i++) {
+    print(nums[i]);
+}
+
+   + +

示例 1:

+ +
输入:nums = [1,1,2]
+
输出:
2, nums = [1,2] +
解释:
函数应该返回新的长度 2 ,并且原数组 nums 的前两个元素被修改为 1, 2 。不需要考虑数组中超出新长度后面的元素。 +
+ +

示例 2:

+ +
输入:nums = [0,0,1,1,1,2,2,3,3,4]
+
输出:
5, nums = [0,1,2,3,4] +
解释:
函数应该返回新的长度 5 , 并且原数组 nums 的前五个元素被修改为 0, 1, 2, 3, 4 。不需要考虑数组中超出新长度后面的元素。 +
+ +

 

+ +

提示:

+ +
    +
  • 0 <= nums.length <= 3 * 104
  • +
  • -104 <= nums[i] <= 104
  • +
  • nums 已按升序排列
  • +
+ +

 

+
+ +## template + +```java +class Solution { + public int removeDuplicates(int[] nums) { + if (nums == null || nums.length == 0) { + return 0; + } + int a = 0; + for (int b = 1; b < nums.length; b++) { + if (nums[a] != nums[b]) { + a++; + nums[a] = nums[b]; + } + } + return a + 1; + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/42.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/42.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..0ee185aad0f8a49fef1cdbc08fd3c0265d6fa1a3 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/42.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-f70a1d0780f44b21b13937a341ef804b", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/42.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/42.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..95089762e9c2a6269ffab34128ca6b2fd4666570 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/42.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "22045603e96b4eb5b60c7ae379c173fe" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/42.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/2.java/42.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..0c3bdbcf11df4a35d74dedee540b82610650a802 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/42.exercises/solution.md" @@ -0,0 +1,67 @@ +# 最长公共前缀 + +

编写一个函数来查找字符串数组中的最长公共前缀。

如果不存在公共前缀,返回空字符串 ""

 

示例 1:

输入:strs = ["flower","flow","flight"]
输出:
"fl"

示例 2:

输入:strs = ["dog","racecar","car"]
输出:
""
解释:
输入不存在公共前缀。

 

提示:

  • 0 <= strs.length <= 200
  • 0 <= strs[i].length <= 200
  • strs[i] 仅由小写英文字母组成
+ +## template + +```java +class Solution { + public String longestCommonPrefix(String[] strs) { + if (strs.length == 0) { + return ""; + } + int i = 0; + StringBuilder lcp = new StringBuilder(); + while (true) { + boolean done = false; + if (i >= strs[0].length()) { + break; + } + for (int j = 0; j < strs.length; j++) { + if (i < strs[j].length()) { + if (strs[j].charAt(i) != strs[0].charAt(i)) { + done = true; + break; + } + } else { + done = true; + break; + } + } + if (done) { + break; + } else { + lcp.append(strs[0].charAt(i)); + i++; + } + } + return lcp.toString(); + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/43.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/43.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..2100c9c2a4564b048358484e720fc9a7f789fe95 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/43.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-47ac6316608e474eb468169158bb6234", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/43.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/43.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..7ab3877b521b2600eebe556d739393ccaa0b5274 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/43.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "03c7d4bad05a400fb5543e4f55a11a00" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/43.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/2.java/43.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..2b00043131eba65b220d5a1a428b8ef284d043d1 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/43.exercises/solution.md" @@ -0,0 +1,53 @@ +# 最大子序和 + +

给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。

 

示例 1:

输入:nums = [-2,1,-3,4,-1,2,1,-5,4]
输出:
6
解释:
连续子数组 [4,-1,2,1] 的和最大,为 6 。

示例 2:

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

示例 3:

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

示例 4:

输入:nums = [-1]
输出:
-1

示例 5:

输入:nums = [-100000]
输出:
-100000

 

提示:

  • 1 <= nums.length <= 3 * 104
  • -105 <= nums[i] <= 105

 

进阶:如果你已经实现复杂度为 O(n) 的解法,尝试使用更为精妙的 分治法 求解。

+ +## template + +```java +class Solution { + public int maxSubArray(int[] nums) { + int maxSum = nums[0]; + int curSum = 0; + + for (int n : nums) { + curSum += n; + if (curSum > maxSum) { + maxSum = curSum; + } + if (curSum < 0) { + curSum = 0; + } + } + + return maxSum; + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/44.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/44.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..78751f682bcb82b6bc058b73b9425e60f8616363 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/44.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-eb76ebfc451b4461a818a12166c5b644", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/44.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/44.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..62aa7c487e4efb65b65fd5c1b777eff89ab55ce6 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/44.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "00b03f5f13924146b69894c24b0188f0" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/44.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/2.java/44.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..35650093ef2773661a0f3d70bc854753bca663f6 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/44.exercises/solution.md" @@ -0,0 +1,61 @@ +# x 的平方根 + +

实现 int sqrt(int x) 函数。

+

计算并返回 x 的平方根,其中 x 是非负整数。

+

由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去。

+

示例 1:

+
输入: 4
输出:
2
+

示例 2:

+
输入: 8
输出:
2
说明:
8 的平方根是 2.82842..., 由于返回类型是整数,小数部分将被舍去。
+ +## template + +```java +class Solution { + public int mySqrt(int x) { + int left = 0, right = 46340; + while (left < right) { + int mid = (left + right) / 2; + if (mid * mid < x) + left = mid + 1; + else if (mid * mid > x) + if ((mid - 1) * (mid - 1) <= x) + return mid - 1; + else + right = mid - 1; + else + return mid; + } + if (left * left > x) + return left - 1; + return left; + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/45.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/45.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..5eb995c7883e75698dbb2a5c82ab053fde4d23cb --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/45.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-1e82a70fe9dd4a2a9ec9ad782a4891e6", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/45.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/45.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..7c4f03d0568bde4b3a42ee97b654f6d3545d803b --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/45.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "32b2d48a82614ff58466347b3cec326f" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/45.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/2.java/45.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..8c76aef969c9043ca7544fde6d3b862086e62b92 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/45.exercises/solution.md" @@ -0,0 +1,53 @@ +# 合并两个有序数组 + +

给你两个有序整数数组 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
+ +## template + +```java +class Solution { + public void merge(int[] nums1, int m, int[] nums2, int n) { + for (int i = 0; i < n; i++) { + nums1[m] = nums2[i]; + m++; + } + int temp = 0; + for (int i = 0; i < m; i++) { + for (int j = i; j < m; j++) { + if (nums1[i] > nums1[j]) { + temp = nums1[j]; + nums1[j] = nums1[i]; + nums1[i] = temp; + } + } + } + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/46.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/46.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..a6ff1eb25c8be893ec7ee2cdc30441b0686a1041 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/46.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-1a646441d91a419eac7c68ccfd3cc569", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/46.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/46.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..6fb36973f0f215f3d6bc1df736f2517ccf14ad2f --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/46.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "e3993840731d473d8c5d2144b4974048" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/46.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/2.java/46.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..d54360d04463cafa2c5ed1eff1f1e69e174d612d --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/46.exercises/solution.md" @@ -0,0 +1,55 @@ +# 搜索插入位置 + +

给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。

你可以假设数组中无重复元素。

示例 1:

输入: [1,3,5,6], 5
输出:
2

示例 2:

输入: [1,3,5,6], 2
输出:
1

示例 3:

输入: [1,3,5,6], 7
输出:
4

示例 4:

输入: [1,3,5,6], 0
输出:
0
+ +## template + +```java +class Solution { + public int searchInsert(int[] nums, int target) { + int left = 0, right = nums.length - 1; + if (target < nums[left]) + return 0; + if (target > nums[right]) + return nums.length; + while (left <= right) { + int mid = (right - left) / 2 + left; + if (target < nums[mid]) { + right = mid - 1; + } else if (target > nums[mid]) { + left = mid + 1; + } else { + return mid; + } + } + return left; + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/47.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/47.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..e6873141ee3a69c164a556d136d33500f5ceac67 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/47.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-1cc1538390d2437f86634913100e84b6", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/47.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/47.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..ed9d6ec27ea7a5816cd9640fd5aee33ed872ccd4 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/47.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "8c82fa58410d4a7bb7789ab04b5d42c4" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/47.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/2.java/47.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..c33583d77c0acb7af7bb367ee77c78ba7cb6349c --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/47.exercises/solution.md" @@ -0,0 +1,47 @@ +# 回文数 + +

给你一个整数 x ,如果 x 是一个回文整数,返回 true ;否则,返回 false

回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。例如,121 是回文,而 123 不是。

 

示例 1:

输入:x = 121
输出:
true

示例 2:

输入:x = -121
输出:
false
解释:
从左向右读, 为 -121 。 从右向左读, 为 121- 。因此它不是一个回文数。

示例 3:

输入:x = 10
输出:
false
解释:
从右向左读, 为 01 。因此它不是一个回文数。

示例 4:

输入:x = -101
输出:
false

 

提示:

  • -231 <= x <= 231 - 1

 

进阶:你能不将整数转为字符串来解决这个问题吗?

+ +## template + +```java +class Solution { + public boolean isPalindrome(int x) { + long r; + long o = x; + long y = 0; + while (x > 0) { + r = x % 10; + y = y * 10 + r; + x = (int) Math.floor(x / 10); + } + return y == o; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/48.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/48.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..ecc778f102a329ad552442322b6af5fc1da1b425 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/48.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-efa3c227bff04d2e80f118c98740ddd2", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/48.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/48.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..ea6d48497e6937e670d13e3749d4209a55cb77b4 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/48.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "7fad009f41864c7a9113bc6baeadba34" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/48.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/2.java/48.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..5cbc0277d94a56058a30a28010f4c616c1dc35d7 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/48.exercises/solution.md" @@ -0,0 +1,66 @@ +# 有效的括号 + +

给定一个只包括 '('')''{''}''['']' 的字符串 s ,判断字符串是否有效。

有效字符串需满足:

  1. 左括号必须用相同类型的右括号闭合。
  2. 左括号必须以正确的顺序闭合。

 

示例 1:

输入:s = "()"
输出:
true

示例 2:

输入:s = "()[]{}"
输出:
true

示例 3:

输入:s = "(]"
输出:
false

示例 4:

输入:s = "([)]"
输出:
false

示例 5:

输入:s = "{[]}"
输出:
true

 

提示:

  • 1 <= s.length <= 104
  • s 仅由括号 '()[]{}' 组成
+ +## template + +```java +class Solution { + public boolean isValid(String s) { + char[] parentheses = { '(', '[', '{', ')', ']', '}' }; + int i = 0; + char c; + int[] sum = { 0, 0, 0 }; + Stack top = new Stack(); + while (i < s.length()) { + c = s.charAt(i); + for (int j = 0; j <= 2; j++) { + if (c == parentheses[j]) { + top.push(j); + sum[j]++; + } else if (c == parentheses[j + 3]) { + if (top.size() == 0 || top.peek() != j) { + return false; + } + top.pop(); + sum[j]--; + } else { + } + } + i++; + } + for (int j = 0; j <= 2; j++) { + if (sum[j] != 0) { + return false; + } + } + return true; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/3.python/29.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/3.python/29.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..f5d58b062ca129b563d5d560f8b9450fcfbaf7ea --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/3.python/29.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-4f3c7b15887f459a9f5ed357a5a2d2c5", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/3.python/29.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/3.python/29.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..64e8d20b404a2aff742b2606efa93adde2f275b1 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/3.python/29.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "9ac3287c096a477da064f21a23419fa9" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/3.python/29.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/3.python/29.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..7db8a0debe9464eb7a58db9219707711b68016b4 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/3.python/29.exercises/solution.md" @@ -0,0 +1,85 @@ +# 合并两个有序链表 + +

将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 

 

示例 1:

输入:l1 = [1,2,4], l2 = [1,3,4]
输出:
[1,1,2,3,4,4]

示例 2:

输入:l1 = [], l2 = []
输出:
[]

示例 3:

输入:l1 = [], l2 = [0]
输出:
[0]

 

提示:

  • 两个链表的节点数目范围是 [0, 50]
  • -100 <= Node.val <= 100
  • l1l2 均按 非递减顺序 排列
+ +## template + +```python +class ListNode: + def __init__(self, val=0, next=None): + self.val = val + self.next = next +class LinkList: + def __init__(self): + self.head=None + def initList(self, data): + self.head = ListNode(data[0]) + r=self.head + p = self.head + for i in data[1:]: + node = ListNode(i) + p.next = node + p = p.next + return r + def convert_list(self,head): + ret = [] + if head == None: + return + node = head + while node != None: + ret.append(node.val) + node = node.next + return ret +class Solution: + def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode: + h = ListNode(0, None) + p = h + while l1 and l2: + if l1.val < l2.val: + p.next = l1 + p = l1 + l1 = l1.next + else: + p.next = l2 + p = l2 + l2 = l2.next + if l1: + p.next = l1 + else: + p.next = l2 + return h.next +# %% +l = LinkList() +list1 = [1,2,4] +list2 = [1,3,4] +l1 = l.initList(list1) +l2 = l.initList(list2) +s = Solution() +print(l.convert_list(s.mergeTwoLists(l1, l2))) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/3.python/30.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/3.python/30.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..c009f8f087a771a76c70123505f3617ac3e37af4 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/3.python/30.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-00bfb01d3da44cca9cfe76a6531eac76", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/3.python/30.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/3.python/30.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..5463d84c453b247d4282c7bcc1c7843c9905f2a4 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/3.python/30.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "3261b48810d048e987bcc2fcbb159bb2" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/3.python/30.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/3.python/30.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..2f9e69d9b057d343e45301fff8be25238a641ead --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/3.python/30.exercises/solution.md" @@ -0,0 +1,51 @@ +# 最后一个单词的长度 + +

给你一个字符串 s,由若干单词组成,单词之间用空格隔开。返回字符串中最后一个单词的长度。如果不存在最后一个单词,请返回 0 。

单词 是指仅由字母组成、不包含任何空格字符的最大子字符串。

 

示例 1:

输入:s = "Hello World"
输出:
5

示例 2:

输入:s = " "
输出:
0

 

提示:

  • 1 <= s.length <= 104
  • s 仅有英文字母和空格 ' ' 组成
+ +## template + +```python +class Solution(object): + def lengthOfLastWord(self, s): + """ + :type s: str + :rtype: int + """ + if len(s) == 0: + return 0 + temp = s.split(' ') + temp = [t for t in temp if len(t) > 0] + if len(temp) == 0: + return 0 + else: + return len(temp[-1]) +# %% +s = Solution() +print(s.lengthOfLastWord(s = "Hello World")) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/3.python/31.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/3.python/31.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..aa6a828dbf9a414cbc7833e9553962c4a5de2172 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/3.python/31.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-7956ce00cccd4a3999c0d9cfde90dbbe", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/3.python/31.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/3.python/31.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..0f4dc7aa9340cd45cedb1fe0421febedf3e8e4f6 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/3.python/31.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "2ff1b7903a4b48cb8575ce8793869078" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/3.python/31.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/3.python/31.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..f722fa51af297a1d9ba2874da4fa3d57198fa99b --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/3.python/31.exercises/solution.md" @@ -0,0 +1,76 @@ +# 删除排序链表中的重复元素 + +

存在一个按升序排列的链表,给你这个链表的头节点 head ,请你删除所有重复的元素,使每个元素 只出现一次

返回同样按升序排列的结果链表。

 

示例 1:

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

示例 2:

输入:head = [1,1,2,3,3]
输出:
[1,2,3]

 

提示:

  • 链表中节点数目在范围 [0, 300]
  • -100 <= Node.val <= 100
  • 题目数据保证链表已经按升序排列
+ +## template + +```python +class ListNode(object): + def __init__(self, x): + self.val = x + self.next = None +class LinkList: + def __init__(self): + self.head=None + def initList(self, data): + self.head = ListNode(data[0]) + r=self.head + p = self.head + for i in data[1:]: + node = ListNode(i) + p.next = node + p = p.next + return r + def convert_list(self,head): + ret = [] + if head == None: + return + node = head + while node != None: + ret.append(node.val) + node = node.next + return ret +class Solution(object): + def deleteDuplicates(self, head): + if head is None: + return None + pos = head + while pos is not None and pos.next is not None: + if pos.val == pos.next.val: + pos.next = pos.next.next + else: + pos = pos.next + return head +# %% +l = LinkList() +list1 = [1,1,2] +l1 = l.initList(list1) +s = Solution() +print(l.convert_list(s.deleteDuplicates(l1))) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/3.python/32.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/3.python/32.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..42201c9d8529ee633985a802306609e978715e2e --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/3.python/32.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-001f31de7edb42d78c8846370eb347c4", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/3.python/32.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/3.python/32.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..f03b258635f68c437aeba662d46fb80bc0755df1 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/3.python/32.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "30e3695ecdfc42c4a6f6bd6280d31976" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/3.python/32.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/3.python/32.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..6ef63f9bf7ada853bed44c6f7ce1d87e3e7bebdc --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/3.python/32.exercises/solution.md" @@ -0,0 +1,52 @@ +# 整数反转 + +

给你一个 32 位的有符号整数 x ,返回将 x 中的数字部分反转后的结果。

如果反转后整数超过 32 位的有符号整数的范围 [−231,  231 − 1] ,就返回 0。

假设环境不允许存储 64 位整数(有符号或无符号)。

 

示例 1:

输入:x = 123
输出:
321

示例 2:

输入:x = -123
输出:
-321

示例 3:

输入:x = 120
输出:
21

示例 4:

输入:x = 0
输出:
0

 

提示:

  • -231 <= x <= 231 - 1
+ +## template + +```python +import math +class Solution: + def reverse(self, x: int) -> int: + r = 0 + y = 0 + abs_x = abs(x) + negative = x < 0 + while abs_x != 0: + r = abs_x % 10 + y = y*10+r + abs_x = int(math.floor(abs_x/10)) + if negative: + y = -y + return 0 if (y > 2147483647 or y < -2147483648) else y +# %% +s = Solution() +print(s.reverse(x = 123)) +print(s.reverse(x = -123)) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/3.python/33.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/3.python/33.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..32c57aeb2c02601e7b008593a3bdfa5e4c3aec28 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/3.python/33.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-1de916c760364a33ade2a9c1f9fa16e4", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/3.python/33.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/3.python/33.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..fc4870184050a074657db58ce639d675f2cab7ce --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/3.python/33.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "d590d2be2d394bb1a5f628b86d61d2ca" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/3.python/33.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/3.python/33.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..78b7525a4efe6dc8b21da521508a4ef3a8ed0e39 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/3.python/33.exercises/solution.md" @@ -0,0 +1,60 @@ +# 相同的树 + +

给你两棵二叉树的根节点 pq ,编写一个函数来检验这两棵树是否相同。

如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。

 

示例 1:

输入:p = [1,2,3], q = [1,2,3]
输出:
true

示例 2:

输入:p = [1,2], q = [1,null,2]
输出:
false

示例 3:

输入:p = [1,2,1], q = [1,1,2]
输出:
false

 

提示:

  • 两棵树上的节点数目都在范围 [0, 100]
  • -104 <= Node.val <= 104
+ +## template + +```python +class TreeNode(object): + def __init__(self, x): + self.val = x + self.left = None + self.right = None +class Solution(object): + def isSameTree(self, p, q): + """ + :type p: TreeNode + :type q: TreeNode + :rtype: bool + """ + if p == q: + return True + try: + left = right = True + if p.val == q.val: + left = self.isSameTree(p.left, q.left) + right = self.isSameTree(p.right, q.right) + return (left and right) + except: + return False + return False +# %% +s = Solution() +print(s.isSameTree(p = [1,2,3], q = [1,2,3])) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/3.python/34.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/3.python/34.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..a63fda450260d7aa5833f246134e2a56620717a5 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/3.python/34.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-204407449dbf419887a4a1fdd5c2b289", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/3.python/34.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/3.python/34.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..6de04884cde41229d748601b3087a53e5a5b1bd3 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/3.python/34.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "c407b9a49c674f908dc63e4faf454a74" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/3.python/34.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/3.python/34.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..b979030a6fed5a499ea0a3832318cf8cb27c857e --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/3.python/34.exercises/solution.md" @@ -0,0 +1,48 @@ +# 两数之和 + +

给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 的那 两个 整数,并返回它们的数组下标。

你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。

你可以按任意顺序返回答案。

 

示例 1:

输入:nums = [2,7,11,15], target = 9
输出:
[0,1]
解释:
因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。

示例 2:

输入:nums = [3,2,4], target = 6
输出:
[1,2]

示例 3:

输入:nums = [3,3], target = 6
输出:
[0,1]

 

提示:

  • 2 <= nums.length <= 103
  • -109 <= nums[i] <= 109
  • -109 <= target <= 109
  • 只会存在一个有效答案
+ +## template + +```python +def twoSum(nums, target): + cache = {} + i = 0 + while i < len(nums): + right = target-nums[i] + if cache.get(right) is not None: + return [cache[right], i] + else: + cache[nums[i]] = i + i += 1 + return [] +# %% +print(twoSum([2,7,11,15], 9)) +print(twoSum([3,2,4], 6)) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/3.python/35.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/3.python/35.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..49630a88ce57095c99fecfe1741a1d2b71bee29f --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/3.python/35.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-5e8ca5819d1c488cb2bad8b6555eff46", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/3.python/35.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/3.python/35.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..cb8fc01a0b7c1b04c136d1497922a533a6b34c7f --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/3.python/35.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "f2791852919f44519b1a39e3a9ffbed5" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/3.python/35.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/3.python/35.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..7265bb72bff35b6cd8d3c9ac64732c743b28559c --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/3.python/35.exercises/solution.md" @@ -0,0 +1,106 @@ +# 移除元素 + +
+

给你一个数组 nums 和一个值 val,你需要 原地 + 移除所有数值等于 val 的元素,并返回移除后数组的新长度。

+ +

不要使用额外的数组空间,你必须仅使用 O(1) 额外空间并 原地 修改输入数组

+ +

元素的顺序可以改变。你不需要考虑数组中超出新长度后面的元素。

+ +

 

+ +

说明:

+ +

为什么返回数值是整数,但输出的答案是数组呢?

+ +

请注意,输入数组是以「引用」方式传递的,这意味着在函数里修改输入数组对于调用者是可见的。

+ +

你可以想象内部操作如下:

+ +
// nums 是以“引用”方式传递的。也就是说,不对实参作任何拷贝
+int len = removeElement(nums, val);
+
+// 在函数里修改输入数组对于调用者是可见的。
+// 根据你的函数返回的长度, 它会打印出数组中 该长度范围内 的所有元素。
+for (int i = 0; i < len; i++) {
+    print(nums[i]);
+}
+
+ +

 

+ +

示例 1:

+ +
输入:nums = [3,2,2,3], val = 3
+
输出:
2, nums = [2,2] +
解释:
函数应该返回新的长度 2, 并且 nums 中的前两个元素均为 2。你不需要考虑数组中超出新长度后面的元素。例如,函数返回的新长度为 2 ,而 nums = [2,2,3,3] 或 nums = [2,2,0,0],也会被视作正确答案。 +
+ +

示例 2:

+ +
输入:nums = [0,1,2,2,3,0,4,2], val = 2
+
输出:
5, nums = [0,1,4,0,3] +
解释:
函数应该返回新的长度 5, 并且 nums 中的前五个元素为 0, 1, 3, 0, 4。注意这五个元素可为任意顺序。你不需要考虑数组中超出新长度后面的元素。 +
+ +

 

+ +

提示:

+ +
    +
  • 0 <= nums.length <= 100
  • +
  • 0 <= nums[i] <= 50
  • +
  • 0 <= val <= 100
  • +
+
+ +## template + +```python +class Solution(object): + def removeElement(self, nums, val): + ls = len(nums) + if ls == 0: + return ls + count = 0 + index = 0 + while index < ls - count: + if nums[index] == val: + nums[index] = nums[ls - 1 - count] + count += 1 + else: + index += 1 + return ls - count +if __name__ == '__main__': + s = Solution() + print(s.removeElement([3,2,2,3], 3)) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/3.python/36.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/3.python/36.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..cf9ebca37feab4beb61da4435f1c6e51f69680c3 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/3.python/36.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-5872e964ef354a5dba3dcf635d8fae48", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/3.python/36.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/3.python/36.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..5689adfb8aebc1c4f4d5da2bfc688db517da9482 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/3.python/36.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "b95a9bde668b4f40b5994713190283d7" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/3.python/36.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/3.python/36.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..600e48138c68e202452cc8a37585e93de68ab51d --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/3.python/36.exercises/solution.md" @@ -0,0 +1,53 @@ +# 二进制求和 + +

给你两个二进制字符串,返回它们的和(用二进制表示)。

输入为 非空 字符串且只包含数字 1 和 0

 

示例 1:

输入: a = "11", b = "1"
输出:
"100"

示例 2:

输入: a = "1010", b = "1011"
输出:
"10101"

 

提示:

  • 每个字符串仅由字符 '0''1' 组成。
  • 1 <= a.length, b.length <= 10^4
  • 字符串如果不是 "0" ,就都不含前导零。
+ +## template + +```python +class Solution(object): + def addBinary(self, a, b): + res = '' + lsa, lsb = len(a), len(b) + pos, plus, curr = -1, 0, 0 + while (lsa + pos) >= 0 or (lsb + pos) >= 0: + if (lsa + pos) >= 0: + curr += int(a[pos]) + if (lsb + pos) >= 0: + curr += int(b[pos]) + res = str(curr % 2) + res + curr //= 2 + pos -= 1 + if curr == 1: + res = '1' + res + return res +# %% +s = Solution() +print(s.addBinary(a = "1010", b = "1011")) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/3.python/37.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/3.python/37.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..5f8ce838455d97cfae65a05e06f3c30ee7868b39 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/3.python/37.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-18a0ca8bcade419ba764e62293cbdc1b", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/3.python/37.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/3.python/37.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..23add5f1ef7fb9d1a767ead3b4ef60ebd11dcfce --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/3.python/37.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "2f70ab6ca0624535856442ef72276287" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/3.python/37.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/3.python/37.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..a14cb25ea894637a29b4914b4dabd9cf2c206555 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/3.python/37.exercises/solution.md" @@ -0,0 +1,101 @@ +# 二叉树的中序遍历 + +

给定一个二叉树的根节点 root ,返回它的 中序 遍历。

 

示例 1:

输入:root = [1,null,2,3]
输出:
[1,3,2]

示例 2:

输入:root = []
输出:
[]

示例 3:

输入:root = [1]
输出:
[1]

示例 4:

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

示例 5:

输入:root = [1,null,2]
输出:
[1,2]

 

提示:

  • 树中节点数目在范围 [0, 100]
  • -100 <= Node.val <= 100

 

进阶: 递归算法很简单,你可以通过迭代算法完成吗?

+ +## template + +```python +class TreeNode(object): + def __init__(self, x): + self.val = x + self.left = None + self.right = None +class List2Tree(object): + def __init__(self, nums: list): + self.nums = nums + self.queue = [] + if len(nums) == 1: + self.root = TreeNode(self.nums.pop(0)) + else: + a = self.nums.pop(0) + b = self.nums.pop(0) + c = self.nums.pop(0) + self.root = TreeNode(a) + if b is not None: + self.root.left = TreeNode(b) + else: + self.root.left = b + if c is not None: + self.root.right = TreeNode(c) + else: + self.root.right = c + self.queue.append(self.root.left) + self.queue.append(self.root.right) + def convert(self): + while len(self.nums) > 0 and len(self.queue)> 0: + node = self.queue.pop(0) + if node is not None: + num= self.nums.pop(0) + if num is not None: + node.left = TreeNode(num) + else: + node.left = num + if len(self.nums) > 0: + num = self.nums.pop(0) + else: + num = None + if num is not None: + node.right = TreeNode(num) + else: + node.right = num + self.queue.append(node.left) + self.queue.append(node.right) + return self.root +class Solution(object): + def inorderTraversal(self, root): + if root is None: + return [] + root = List2Tree(root).convert() + res = [] + stack = [root] + while len(stack) > 0: + curr = stack.pop() + if not isinstance(curr, TreeNode): + res.append(curr) + continue + if curr.right is not None: + stack.append(curr.right) + stack.append(curr.val) + if curr.left is not None: + stack.append(curr.left) + return res +# %% +s = Solution() +print(s.inorderTraversal(root = [1,None,2,3])) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/3.python/38.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/3.python/38.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..de9304fac20c7f3356e8f0e07c6dc87b8fc50164 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/3.python/38.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-b4b61470dce44ff38498ca9b9ad4d0a6", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/3.python/38.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/3.python/38.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..650545a921c08224a00f15fac9c3335bf569281c --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/3.python/38.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "31603bf0cd9940eaba90854ce5a386be" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/3.python/38.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/3.python/38.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..58e00d2bfce57b93290b7f67fa2c3884ebba8749 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/3.python/38.exercises/solution.md" @@ -0,0 +1,45 @@ +# 爬楼梯 + +

假设你正在爬楼梯。需要 n 阶你才能到达楼顶。

每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢?

注意:给定 n 是一个正整数。

示例 1:

输入: 2
输出:
2
解释:
有两种方法可以爬到楼顶。1. 1 阶 + 1 阶2. 2 阶

示例 2:

输入: 3
输出:
3
解释:
有三种方法可以爬到楼顶。1. 1 阶 + 1 阶 + 1 阶2. 1 阶 + 2 阶3. 2 阶 + 1 阶
+ +## template + +```python +class Solution(object): + def climbStairs(self, n): + if n <= 1: + return 1 + dp = [1] * 2 + for i in range(2, n + 1): + dp[1], dp[0] = dp[1] + dp[0], dp[1] + return dp[1] +# %% +s = Solution() +print(s.climbStairs(2)) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/3.python/39.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/3.python/39.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..462fb550061d81904f26ab280e569d5f867ae7fd --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/3.python/39.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-09a8f5ced6c9482da440800ce5e1f566", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/3.python/39.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/3.python/39.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..e1cf338146cdc5ba7df2b41829ecfde21f51322d --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/3.python/39.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "31aeaffaae294a77bd8036cd3ae7b48d" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/3.python/39.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/3.python/39.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..d9aae195f0727d5de5417bccbd6360b439ec0c53 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/3.python/39.exercises/solution.md" @@ -0,0 +1,136 @@ +# 罗马数字转整数 + +
+

罗马数字包含以下七种字符: I, V, X, LCD 和 M。 +

+ +
字符          数值
+I             1
+V             5
+X             10
+L             50
+C             100
+D             500
+M             1000
+ +

例如, 罗马数字 2 写做 II ,即为两个并列的 1。12 + 写做 XII ,即为 X + II 。 27 + 写做  XXVII, + 即为 XX + V + II 。

+ +

通常情况下,罗马数字中小的数字在大的数字的右边。但也存在特例,例如 4 不写做 IIII,而是 IV。数字 1 在数字 5 的左边,所表示的数等于大数 5 + 减小数 1 得到的数值 4 。同样地,数字 9 表示为 IX。这个特殊的规则只适用于以下六种情况:

+ +
    +
  • I 可以放在 V (5) 和 X (10) 的左边,来表示 4 和 9。
  • +
  • X 可以放在 L (50) 和 C (100) 的左边,来表示 40 + 和 90。 
  • +
  • C 可以放在 D (500) 和 M (1000) 的左边,来表示 400 + 和 900。
  • +
+ +

给你一个整数,将其转为罗马数字。

+ +

 

+ +

示例 1:

+ +
输入: num = 3
+
输出:
"III"
+ +

示例 2:

+ +
输入: num = 4
+
输出:
"IV"
+ +

示例 3:

+ +
输入: num = 9
+
输出:
"IX"
+ +

示例 4:

+ +
输入: num = 58
+
输出:
"LVIII" +
解释:
L = 50, V = 5, III = 3. +
+ +

示例 5:

+ +
输入: num = 1994
+
输出:
"MCMXCIV" +
解释:
M = 1000, CM = 900, XC = 90, IV = 4.
+ +

 

+ +

提示:

+ +
    +
  • 1 <= num <= 3999
  • +
+
+ +## template + +```python +class Solution: + def romanToInt(self, s: str) -> int: + units = [ + ["M", 1000, 1], + ["CM", 900, 2], + ["D", 500, 1], + ["CD", 400, 2], + ["C", 100, 1], + ["XC", 90, 2], + ["L", 50, 1], + ["XL", 40, 2], + ["X", 10, 1], + ["IX", 9, 2], + ["V", 5, 1], + ["IV", 4, 2], + ["I", 1, 1] + ] + end = len(s) + start = 0 + i = 0 + r = 0 + while i < len(units): + unit = units[i][0] + value = units[i][1] + step = units[i][2] + if end-start >= step and s[start:start+step] == unit: + r += value + start += step + else: + i += 1 + return r +# %% +s = Solution() +print(s.romanToInt("III")) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/3.python/40.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/3.python/40.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..2c8cdef3307ecbde03105bd077b1bf4ccf2c1dfe --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/3.python/40.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-2cae6cbebf3f43dd89ee15031f530371", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/3.python/40.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/3.python/40.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..562bf1de91e5f9569eb0d11d459d63990515217b --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/3.python/40.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "adc28151612f44f1918ede0bf2c0ae51" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/3.python/40.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/3.python/40.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..ffe957a5f95379667921f30c827f461dc816d83d --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/3.python/40.exercises/solution.md" @@ -0,0 +1,48 @@ +# 加一 + +

给定一个由 整数 组成的 非空 数组所表示的非负整数,在该数的基础上加一。

最高位数字存放在数组的首位, 数组中每个元素只存储单个数字。

你可以假设除了整数 0 之外,这个整数不会以零开头。

 

示例 1:

输入:digits = [1,2,3]
输出:
[1,2,4]
解释:
输入数组表示数字 123。

示例 2:

输入:digits = [4,3,2,1]
输出:
[4,3,2,2]
解释:
输入数组表示数字 4321。

示例 3:

输入:digits = [0]
输出:
[1]

 

提示:

  • 1 <= digits.length <= 100
  • 0 <= digits[i] <= 9
+ +## template + +```python +class Solution(object): + def plusOne(self, digits): + ls = len(digits) + for index in reversed(range(ls)): + if digits[index] < 9: + digits[index] += 1 + return digits + else: + digits[index] = 0 + digits.insert(0, 1) + return digits +# %% +s = Solution() +print(s.plusOne(digits = [1,2,3])) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/3.python/41.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/3.python/41.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..8b437b43e24816be6f0b3520f3d8c652479db1cd --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/3.python/41.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-4c6bf3ab14be4b04976c103c08a46cfe", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/3.python/41.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/3.python/41.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..81b4fc96c46b1e068129075a7391568a0fd3b1b3 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/3.python/41.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "e066a6c1e2824ca7921dac06541efb00" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/3.python/41.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/3.python/41.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..d6ed9a3ebc672fb7b9f3efc9857314ade6d1160c --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/3.python/41.exercises/solution.md" @@ -0,0 +1,102 @@ +# 删除有序数组中的重复项 + +
+

给你一个有序数组 nums ,请你 + 原地 删除重复出现的元素,使每个元素 只出现一次 ,返回删除后数组的新长度。

+ +

不要使用额外的数组空间,你必须在 原地 + 修改输入数组 并在使用 O(1) 额外空间的条件下完成。

+ +

 

+ +

说明:

+ +

为什么返回数值是整数,但输出的答案是数组呢?

+ +

请注意,输入数组是以「引用」方式传递的,这意味着在函数里修改输入数组对于调用者是可见的。

+ +

你可以想象内部操作如下:

+ +
// nums 是以“引用”方式传递的。也就是说,不对实参做任何拷贝
+int len = removeDuplicates(nums);
+
+// 在函数里修改输入数组对于调用者是可见的。
+// 根据你的函数返回的长度, 它会打印出数组中 该长度范围内 的所有元素。
+for (int i = 0; i < len; i++) {
+    print(nums[i]);
+}
+
+   + +

示例 1:

+ +
输入:nums = [1,1,2]
+
输出:
2, nums = [1,2] +
解释:
函数应该返回新的长度 2 ,并且原数组 nums 的前两个元素被修改为 1, 2 。不需要考虑数组中超出新长度后面的元素。 +
+ +

示例 2:

+ +
输入:nums = [0,0,1,1,1,2,2,3,3,4]
+
输出:
5, nums = [0,1,2,3,4] +
解释:
函数应该返回新的长度 5 , 并且原数组 nums 的前五个元素被修改为 0, 1, 2, 3, 4 。不需要考虑数组中超出新长度后面的元素。 +
+ +

 

+ +

提示:

+ +
    +
  • 0 <= nums.length <= 3 * 104
  • +
  • -104 <= nums[i] <= 104
  • +
  • nums 已按升序排列
  • +
+ +

 

+
+ +## template + +```python +class Solution(object): + def removeDuplicates(self, nums): + if len(nums) == 0: + return 0 + left = 0 + for i in range(1, len(nums)): + if nums[left] == nums[i]: + continue + else: + left += 1 + nums[left] = nums[i] + return left + 1 +# %% +s = Solution() +print(s.removeDuplicates(nums = [1,1,2])) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/3.python/42.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/3.python/42.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..3f0b2d0b2ca4499edd4ae50616e8538655568ef6 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/3.python/42.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-ec4849bcc16343d5980a35dccf7c929f", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/3.python/42.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/3.python/42.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..23b94f11d9ae12445ddb85a0506d785cd3c2e9d8 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/3.python/42.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "3621f294bf1f4859bfc3c8906dd95499" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/3.python/42.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/3.python/42.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..5f8bc6052d26475eef21170a0ce02e3d0c2ad654 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/3.python/42.exercises/solution.md" @@ -0,0 +1,64 @@ +# 最长公共前缀 + +

编写一个函数来查找字符串数组中的最长公共前缀。

如果不存在公共前缀,返回空字符串 ""

 

示例 1:

输入:strs = ["flower","flow","flight"]
输出:
"fl"

示例 2:

输入:strs = ["dog","racecar","car"]
输出:
""
解释:
输入不存在公共前缀。

 

提示:

  • 0 <= strs.length <= 200
  • 0 <= strs[i].length <= 200
  • strs[i] 仅由小写英文字母组成
+ +## template + +```python +from typing import List +class Solution: + def longestCommonPrefix(self, strs: List[str]) -> str: + if len(strs) == 0: + return '' + i = 0 + lcp = [] + while True: + done = False + if i >= len(strs[0]): + break + j = 0 + while j < len(strs): + if i < len(strs[j]): + if strs[j][i] != strs[0][i]: + done = True + break + else: + done = True + break + j += 1 + if not done: + lcp.append(strs[0][i]) + i += 1 + else: + break + return ''.join(lcp) +# %% +s = Solution() +print(s.longestCommonPrefix(strs = ["flower","flow","flight"])) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/3.python/43.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/3.python/43.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..4b251a38c0897e13343300f30428945def3cd1ff --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/3.python/43.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-2ece2e38afba49ff90a4608ad37c820e", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/3.python/43.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/3.python/43.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..df7b626d8632d223bacbd204debd38b11f9333de --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/3.python/43.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "6cf30522972249b1998cdbff69d7937f" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/3.python/43.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/3.python/43.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..9ea9d2c19a1c115e0503a7da00194278afa1b62e --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/3.python/43.exercises/solution.md" @@ -0,0 +1,44 @@ +# 最大子序和 + +

给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。

 

示例 1:

输入:nums = [-2,1,-3,4,-1,2,1,-5,4]
输出:
6
解释:
连续子数组 [4,-1,2,1] 的和最大,为 6 。

示例 2:

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

示例 3:

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

示例 4:

输入:nums = [-1]
输出:
-1

示例 5:

输入:nums = [-100000]
输出:
-100000

 

提示:

  • 1 <= nums.length <= 3 * 104
  • -105 <= nums[i] <= 105

 

进阶:如果你已经实现复杂度为 O(n) 的解法,尝试使用更为精妙的 分治法 求解。

+ +## template + +```python +class Solution(object): + def maxSubArray(self, nums): + maxEndingHere = maxSofFar = nums[0] + for i in range(1, len(nums)): + maxEndingHere = max(maxEndingHere + nums[i], nums[i]) + maxSofFar = max(maxEndingHere, maxSofFar) + return maxSofFar +# %% +s = Solution() +print(s.maxSubArray(nums = [-2,1,-3,4,-1,2,1,-5,4])) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/3.python/44.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/3.python/44.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..8c88860474aff4cda3b63ed3cd087770214097c2 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/3.python/44.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-15d3636528c242ffa88a424c545b62fa", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/3.python/44.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/3.python/44.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..50136e497cc988070dd15203ad7f8189c62e574d --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/3.python/44.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "af61d928fa1d4e3abaebdc4f31e82491" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/3.python/44.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/3.python/44.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..4efdc3e0b60bacd69113b0a617e821ea4f5791e6 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/3.python/44.exercises/solution.md" @@ -0,0 +1,53 @@ +# x 的平方根 + +

实现 int sqrt(int x) 函数。

+

计算并返回 x 的平方根,其中 x 是非负整数。

+

由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去。

+

示例 1:

+
输入: 4
输出:
2
+

示例 2:

+
输入: 8
输出:
2
说明:
8 的平方根是 2.82842..., 由于返回类型是整数,小数部分将被舍去。
+ +## template + +```python +class Solution: + def mySqrt(self, x): + if x == 0: + return 0 + if x < 4: + return 1 + res = 2 * self.mySqrt(x / 4) + if (res + 1) * (res + 1) <= x and (res + 1) * (res + 1) >= 0: + return res + 1 + return res +# %% +s = Solution() +print(s.mySqrt(4)) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/3.python/45.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/3.python/45.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..dd53d4adebd0127b5aab20c8e08945b93287a547 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/3.python/45.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-b0eaa36ecf6b4283a1816a7af434be8f", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/3.python/45.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/3.python/45.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..d0d9e00e58c34ec36f2e1fb5b49fd0f8d7612f21 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/3.python/45.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "08ebef8b6e194a40af8a496863fd133b" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/3.python/45.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/3.python/45.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..c75db96d49951054aaed30119dd8c0f8ad74c840 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/3.python/45.exercises/solution.md" @@ -0,0 +1,61 @@ +# 合并两个有序数组 + +

给你两个有序整数数组 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
+ +## template + +```python +class Solution(object): + def merge(self, nums1, m, nums2, n): + """ + :type nums1: List[int] + :type m: int + :type nums2: List[int] + :type n: int + :rtype: void Do not return anything, modify nums1 in-place instead. + """ + p1, p2 = m - 1, n - 1 + pos = m + n - 1 + while p1 >= 0 and p2 >= 0: + if nums1[p1] >= nums2[p2]: + nums1[pos] = nums1[p1] + p1 -= 1 + else: + nums1[pos] = nums2[p2] + p2 -= 1 + pos -= 1 + while p2 >= 0: + nums1[pos] = nums2[p2] + p2 -= 1 + pos -= 1 + return nums1 +# %% +s = Solution() +print(s.merge(nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3)) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/3.python/46.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/3.python/46.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..a317864a9a532c083d9442e1cdbd5dfb8fb672e7 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/3.python/46.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-45af9780672b457dbe082fbe45f84648", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/3.python/46.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/3.python/46.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..79f261e743fde5ff481050253bfbd49562f71000 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/3.python/46.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "48c33d365de549de901a6fd1c89721b4" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/3.python/46.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/3.python/46.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..11c890f5389b36b4581241aa9357d27a46aed3c2 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/3.python/46.exercises/solution.md" @@ -0,0 +1,49 @@ +# 搜索插入位置 + +

给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。

你可以假设数组中无重复元素。

示例 1:

输入: [1,3,5,6], 5
输出:
2

示例 2:

输入: [1,3,5,6], 2
输出:
1

示例 3:

输入: [1,3,5,6], 7
输出:
4

示例 4:

输入: [1,3,5,6], 0
输出:
0
+ +## template + +```python +class Solution: + def searchInsert(self, nums, target): + l, r = int(0), len(nums) - 1 + while l < r: + mid = int((l + r) / 2) + if nums[mid] < target: + l = mid + 1 + else: + r = mid + if nums[l] < target: + return l + 1 + return l +if __name__ == '__main__': + s = Solution() + print (s.searchInsert([1,3,5,6],5)) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/3.python/47.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/3.python/47.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..7cd34431d73ee7ead0eec759f546de695ed9655c --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/3.python/47.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-caacbea4ef72485bb69a4ccb76b38add", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/3.python/47.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/3.python/47.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..9afec6b13733e3eebafe766e6dc6a2df63f6cc89 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/3.python/47.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "50473072072a428383f918538f4dab36" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/3.python/47.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/3.python/47.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..47619fdb1a112e9d315a4dfa663502aa1a892db3 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/3.python/47.exercises/solution.md" @@ -0,0 +1,47 @@ +# 回文数 + +

给你一个整数 x ,如果 x 是一个回文整数,返回 true ;否则,返回 false

回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。例如,121 是回文,而 123 不是。

 

示例 1:

输入:x = 121
输出:
true

示例 2:

输入:x = -121
输出:
false
解释:
从左向右读, 为 -121 。 从右向左读, 为 121- 。因此它不是一个回文数。

示例 3:

输入:x = 10
输出:
false
解释:
从右向左读, 为 01 。因此它不是一个回文数。

示例 4:

输入:x = -101
输出:
false

 

提示:

  • -231 <= x <= 231 - 1

 

进阶:你能不将整数转为字符串来解决这个问题吗?

+ +## template + +```python +import math +class Solution: + def isPalindrome(self, x: int) -> bool: + o = x + y = 0 + while x > 0: + r = x % 10 + y = y*10+r + x = int(math.floor(x/10)) + return y == o +# %% +s = Solution() +print(s.isPalindrome(x = 121)) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/3.python/48.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/3.python/48.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..bbce88ffb99aa78e940b74f984e9f32b8daa3403 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/3.python/48.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-faea9649ace444d8becd03ed79d8e758", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/3.python/48.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/3.python/48.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..c3266421bd6c333c9b14edd997ce0d4203e45241 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/3.python/48.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "ef130924f9d34936a921870237cb7337" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/3.python/48.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/3.python/48.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..0ef2aed035b2f6e8c94cfaf111374c210e70d37b --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/3.python/48.exercises/solution.md" @@ -0,0 +1,64 @@ +# 有效的括号 + +

给定一个只包括 '('')''{''}''['']' 的字符串 s ,判断字符串是否有效。

有效字符串需满足:

  1. 左括号必须用相同类型的右括号闭合。
  2. 左括号必须以正确的顺序闭合。

 

示例 1:

输入:s = "()"
输出:
true

示例 2:

输入:s = "()[]{}"
输出:
true

示例 3:

输入:s = "(]"
输出:
false

示例 4:

输入:s = "([)]"
输出:
false

示例 5:

输入:s = "{[]}"
输出:
true

 

提示:

  • 1 <= s.length <= 104
  • s 仅由括号 '()[]{}' 组成
+ +## template + +```python +class Solution: + def isValid(self, s: str) -> bool: + parentheses = [ + '(', '[', '{', + ')', ']', '}' + ] + i = 0 + sum = [0, 0, 0] + top = [] + while i < len(s): + c = s[i] + j = 0 + while j <= 2: + if c == parentheses[j]: + top.append(j) + sum[j] += 1 + elif c == parentheses[j+3]: + if len(top) == 0 or top[len(top)-1] != j: + return False + top.pop() + sum[j] -= 1 + j += 1 + i += 1 + if sum[0] != 0 or sum[1] != 0 or sum[2] != 0: + return False + else: + return True +# %% +s = Solution() +print(s.isValid(s = "()[]{}")) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/29.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/29.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..f92b84b97589f4a38a8ac038ec89d1fbe680b88d --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/29.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-276245dff1aa4411ac4f234d25a197c2", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/29.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/29.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..31d69d5c921eb998c936e2bc21ed34a2b6cf696c --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/29.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "fbe4ad5122b34be0bd9bec8212abd8f6" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/29.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/29.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..5f1deb351a9c5c477bf223ca314f2783c3a6acf8 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/29.exercises/solution.md" @@ -0,0 +1,83 @@ +# 恢复二叉搜索树 + +

给你二叉搜索树的根节点 root ,该树中的两个节点被错误地交换。请在不改变其结构的情况下,恢复这棵树。

进阶:使用 O(n) 空间复杂度的解法很容易实现。你能想出一个只使用常数空间的解决方案吗?

 

示例 1:

输入:root = [1,3,null,null,2]
输出:
[3,1,null,null,2]
解释:
3 不能是 1 左孩子,因为 3 > 1 。交换 1 和 3 使二叉搜索树有效。

示例 2:

输入:root = [3,1,4,null,null,2]
输出:
[2,1,4,null,null,3]
解释:
2 不能在 3 的右子树中,因为 2 < 3 。交换 2 和 3 使二叉搜索树有效。

 

提示:

  • 树上节点的数目在范围 [2, 1000]
  • -231 <= Node.val <= 231 - 1
+ +## template + +```cpp +#include +using namespace std; +struct TreeNode +{ + int val; + TreeNode *left; + TreeNode *right; + TreeNode() : val(0), left(nullptr), right(nullptr) {} + TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} +}; +class Solution +{ +public: + void recoverTree(TreeNode *root) + { + dfs(root); + int tmp = p0_->val; + p0_->val = p1_->val; + p1_->val = tmp; + } +private: + int wrong_ = 0; + TreeNode *prev_ = nullptr; + TreeNode *p0_ = nullptr; + TreeNode *p1_ = nullptr; + void dfs(TreeNode *root) + { + if (root == nullptr || wrong_ == 2) + { + return; + } + dfs(root->left); + if (prev_ != nullptr && prev_->val > root->val) + { + if (++wrong_ == 1) + { + p0_ = prev_; + p1_ = root; + } + else if (wrong_ == 2) + { + p1_ = root; + } + } + prev_ = root; + dfs(root->right); + } +}; +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/30.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/30.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..b961cd8fdada1562a8853ae3c28119e51db1e788 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/30.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-0b6297d3889e4ff4ad745ba952b661fd", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/30.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/30.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..331043c85587f93e5d133db63b23401c41e73415 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/30.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "f1e21c54bf394f01929948edff768447" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/30.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/30.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..655c0a40bdedee82ecc04bf4837de9a853df77fe --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/30.exercises/solution.md" @@ -0,0 +1,58 @@ +# 子集 + +

给你一个整数数组 nums ,数组中的元素 互不相同 。返回该数组所有可能的子集(幂集)。

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

 

示例 1:

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

示例 2:

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

 

提示:

  • 1 <= nums.length <= 10
  • -10 <= nums[i] <= 10
  • nums 中的所有元素 互不相同
+ +## template + +```cpp +#include +using namespace std; +class Solution +{ +public: + vector> subsets(vector &nums) + { + vector> res; + dfs(nums, 0, res); + return res; + } +private: + vector stack; + void dfs(vector &nums, int start, vector> &res) + { + res.push_back(stack); + for (int i = start; i < nums.size(); i++) + { + stack.push_back(nums[i]); + dfs(nums, i + 1, res); + stack.pop_back(); + } + } +}; +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/31.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/31.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..d9ee80fa8cc7bedfb115963c4f77e30523b4d4a2 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/31.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-e54a55862bdd4ec38996b20ec307ee4d", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/31.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/31.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..451b5a3a94df9f35a92e0505e475b17a09559c28 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/31.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "192a675b38e04d5bb25ab7dbb27a5ae9" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/31.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/31.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..65b86e62902cf463b2506b4023a487c32caee289 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/31.exercises/solution.md" @@ -0,0 +1,85 @@ +# 不同路径 + +

一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为 “Start” )。

+

机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为 “Finish” )。

+

问总共有多少条不同的路径?

+

 

+

示例 1:

+
输入:m = 3, n = 7
输出:
28
+

示例 2:

+
输入:m = 3, n = 2
输出:
3
解释:
从左上角开始,总共有 3 条路径可以到达右下角。
1. 向右 -> 向下 -> 向下
2. 向下 -> 向下 -> 向右
3. 向下 -> 向右 -> 向下
+

示例 3:

+
输入:m = 7, n = 3
输出:
28
+

示例 4:

+
输入:m = 3, n = 3
输出:
6
+

 

+

提示:

+
    +
  • 1 <= m, n <= 100
  • +
  • 题目数据保证答案小于等于 2 * 109
  • +
+ +## template + +```cpp +#include +#include +static int uniquePaths(int m, int n) +{ + int row, col; + int *grids = malloc(m * n * sizeof(int)); + for (col = 0; col < m; col++) + { + grids[col] = 1; + } + for (row = 0; row < n; row++) + { + grids[row * m] = 1; + } + for (row = 1; row < n; row++) + { + for (col = 1; col < m; col++) + { + grids[row * m + col] = grids[row * m + col - 1] + grids[(row - 1) * m + col]; + } + } + return grids[m * n - 1]; +} +int main(int argc, char **argv) +{ + if (argc != 3) + { + fprintf(stderr, "Usage: ./test m n\n"); + exit(-1); + } + printf("%d\n", uniquePaths(atoi(argv[1]), atoi(argv[2]))); + return 0; +} +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/32.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/32.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..46fba09c913458664f086d5fb057f3c2da2ba2eb --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/32.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-c8e4de98cd7d4adaa3289c2e2f530120", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/32.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/32.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..f604b4ac3d20c68eff86f280d5b6a4af2e9805f2 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/32.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "9b3da2e42b6046e684c9f5f9f499fb03" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/32.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/32.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..f1b9d8e4333bd66bc9300eb3b41ea272d20a2f48 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/32.exercises/solution.md" @@ -0,0 +1,86 @@ +# 电话号码的字母组合 + +

给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。答案可以按 任意顺序 返回。

给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。

 

示例 1:

输入:digits = "23"
输出:
["ad","ae","af","bd","be","bf","cd","ce","cf"]

示例 2:

输入:digits = ""
输出:
[]

示例 3:

输入:digits = "2"
输出:
["a","b","c"]

 

提示:

  • 0 <= digits.length <= 4
  • digits[i] 是范围 ['2', '9'] 的一个数字。
+ +## template + +```cpp +class Solution +{ +public: + vector letterCombinations(string digits) + { + vector nummap({" ", + "", + "abc", + "def", + "ghi", + "jkl", + "mno", + "pqrs", + "tuv", + "wxyz"}); + vector rs; + vector empty; + if (digits.size() == 0) + return empty; + for (auto d : digits) + { + if (d == '0') + return empty; + if (d == '1') + return empty; + auto &s = nummap[d - '0']; + if (s.size() == 0) + continue; + if (rs.size() == 0) + for (auto c : s) + { + string t; + t.push_back(c); + rs.emplace_back(t); + } + else + { + vector rn; + for (auto c : s) + { + for (auto r : rs) + { + r.push_back(c); + rn.emplace_back(std::move(r)); + } + } + std::swap(rs, rn); + } + } + return rs; + } +}; +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/33.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/33.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..f17683b5ad6aeba5fe5d778c0309bf5ccf195e3a --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/33.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-2c69030fc9f444ddbcd886ad9a0548a7", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/33.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/33.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..5175e9c3e9afcc9ea362fc06fb124dc1cb97b3b1 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/33.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "91ebaa372a4547c48625658aca52610f" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/33.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/33.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..0815bca91c8652e472a72b3530151e2d431caa74 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/33.exercises/solution.md" @@ -0,0 +1,172 @@ +# 不同的二叉搜索树 II + +
+

给你一个整数 n ,请你生成并返回所有由 n 个节点组成且节点值从 1n 互不相同的不同 + 二叉搜索树 。可以按 任意顺序 返回答案。 +

+ +

 

+ +
+
+

示例 1:

+ +
输入:n = 3
+输出:[[1,null,2,null,3],[1,null,3,2],[2,1,3],[3,1,null,null,2],[3,2,null,1]]
+    
+ +

示例 2:

+ +
输入:n = 1
+输出:[[1]]
+    
+ +

 

+ +

提示:

+ +
    +
  • 1 <= n <= 8
  • +
+
+
+
+ +## template + +```cpp +#include +#include +struct TreeNode +{ + int val; + struct TreeNode *left; + struct TreeNode *right; +}; +static struct TreeNode *dfs(int low, int high, int *count) +{ + int i, j, k; + if (low > high) + { + *count = 0; + return NULL; + } + else if (low == high) + { + struct TreeNode *node = malloc(sizeof(*node)); + node->val = low; + node->left = NULL; + node->right = NULL; + *count = 1; + return node; + } + else + { + *count = 0; + int capacity = 5; + struct TreeNode *roots = malloc(capacity * sizeof(struct TreeNode)); + for (i = low; i <= high; i++) + { + int left_cnt, right_cnt; + struct TreeNode *left_subs = dfs(low, i - 1, &left_cnt); + struct TreeNode *right_subs = dfs(i + 1, high, &right_cnt); + if (left_cnt == 0) + left_cnt = 1; + if (right_cnt == 0) + right_cnt = 1; + if (*count + (left_cnt * right_cnt) >= capacity) + { + capacity *= 2; + capacity += left_cnt * right_cnt; + roots = realloc(roots, capacity * sizeof(struct TreeNode)); + } + for (j = 0; j < left_cnt; j++) + { + for (k = 0; k < right_cnt; k++) + { + roots[*count].val = i; + roots[*count].left = left_subs == NULL ? NULL : &left_subs[j]; + roots[*count].right = right_subs == NULL ? NULL : &right_subs[k]; + (*count)++; + } + } + } + return roots; + } +} +static struct TreeNode **generateTrees(int n, int *returnSize) +{ + int i, count = 0; + struct TreeNode *roots = dfs(1, n, &count); + struct TreeNode **results = malloc(count * sizeof(struct TreeNode *)); + for (i = 0; i < count; i++) + { + results[i] = &roots[i]; + } + *returnSize = count; + return results; +} +static void dump(struct TreeNode *node) +{ + printf("%d ", node->val); + if (node->left != NULL) + { + dump(node->left); + } + else + { + printf("# "); + } + if (node->right != NULL) + { + dump(node->right); + } + else + { + printf("# "); + } +} +int main(int argc, char **argv) +{ + if (argc != 2) + { + fprintf(stderr, "Usage: ./test n\n"); + exit(-1); + } + int i, count = 0; + struct TreeNode **results = generateTrees(atoi(argv[1]), &count); + for (i = 0; i < count; i++) + { + dump(results[i]); + printf("\n"); + } + return 0; +} +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/34.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/34.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..b88410e1bfcfdf3b746931e1bc547d2605c2a13c --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/34.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-5101e6dff09443eaa8ce6666c67934e5", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/34.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/34.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..dbe8c55d7b65472cc54d10312f6952e3ace2e2ba --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/34.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "79c63c0e1ee745ada53f148385e4df8f" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/34.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/34.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..2bc9942e671947a1187f45ea72e5e024a28c35c7 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/34.exercises/solution.md" @@ -0,0 +1,183 @@ +# 字符串转换整数 (atoi) + +
+

请你来实现一个 myAtoi(string s) 函数,使其能将字符串转换成一个 32 位有符号整数(类似 C/C++ 中的 atoi 函数)。

+ +

函数 myAtoi(string s) 的算法如下:

+ +
    +
  • 读入字符串并丢弃无用的前导空格
  • +
  • 检查下一个字符(假设还未到字符末尾)为正还是负号,读取该字符(如果有)。 确定最终结果是负数还是正数。 如果两者都不存在,则假定结果为正。
  • +
  • 读入下一个字符,直到到达下一个非数字字符或到达输入的结尾。字符串的其余部分将被忽略。
  • +
  • 将前面步骤读入的这些数字转换为整数(即,"123" -> 123, "0032" -> 32)。如果没有读入数字,则整数为 0 。必要时更改符号(从步骤 2 开始)。
  • +
  • 如果整数数超过 32 位有符号整数范围 [−231,  231 − 1] + ,需要截断这个整数,使其保持在这个范围内。具体来说,小于 −231 的整数应该被固定为 −231 ,大于 + 231 − 1 的整数应该被固定为 231 − 1 。 +
  • +
  • 返回整数作为最终结果。
  • +
+ +

注意:

+ +
    +
  • 本题中的空白字符只包括空格字符 ' '
  • +
  • 除前导空格或数字后的其余字符串外,请勿忽略 任何其他字符。
  • +
+ +

 

+ +

示例 1:

+ +
输入:s = "42"
+输出:42
+解释:加粗的字符串为已经读入的字符,插入符号是当前读取的字符。
+第 1 步:"42"(当前没有读入字符,因为没有前导空格)
+		 ^
+第 2 步:"42"(当前没有读入字符,因为这里不存在 '-' 或者 '+')
+		 ^
+第 3 步:"42"(读入 "42")
+		   ^
+解析得到整数 42 。
+由于 "42" 在范围 [-231, 231 - 1] 内,最终结果为 42 。
+ +

示例 2:

+ +
输入:s = "   -42"
+输出:-42
+解释:
+第 1 步:"   -42"(读入前导空格,但忽视掉)
+			^
+第 2 步:"   -42"(读入 '-' 字符,所以结果应该是负数)
+			 ^
+第 3 步:"   -42"(读入 "42")
+			   ^
+解析得到整数 -42 。
+由于 "-42" 在范围 [-231, 231 - 1] 内,最终结果为 -42 。
+
+ +

示例 3:

+ +
输入:s = "4193 with words"
+输出:4193
+解释:
+第 1 步:"4193 with words"(当前没有读入字符,因为没有前导空格)
+		 ^
+第 2 步:"4193 with words"(当前没有读入字符,因为这里不存在 '-' 或者 '+')
+		 ^
+第 3 步:"4193 with words"(读入 "4193";由于下一个字符不是一个数字,所以读入停止)
+		     ^
+解析得到整数 4193 。
+由于 "4193" 在范围 [-231, 231 - 1] 内,最终结果为 4193 。
+
+ +

示例 4:

+ +
输入:s = "words and 987"
+输出:0
+解释:
+第 1 步:"words and 987"(当前没有读入字符,因为没有前导空格)
+		 ^
+第 2 步:"words and 987"(当前没有读入字符,因为这里不存在 '-' 或者 '+')
+		 ^
+第 3 步:"words and 987"(由于当前字符 'w' 不是一个数字,所以读入停止)
+		 ^
+解析得到整数 0 ,因为没有读入任何数字。
+由于 0 在范围 [-231, 231 - 1] 内,最终结果为 0 。
+ +

示例 5:

+ +
输入:s = "-91283472332"
+输出:-2147483648
+解释:
+第 1 步:"-91283472332"(当前没有读入字符,因为没有前导空格)
+		 ^
+第 2 步:"-91283472332"(读入 '-' 字符,所以结果应该是负数)
+		  ^
+第 3 步:"-91283472332"(读入 "91283472332")
+		            ^
+解析得到整数 -91283472332 。
+由于 -91283472332 小于范围 [-231, 231 - 1] 的下界,最终结果被截断为 -231 = -2147483648 。
+ +

 

+ +

提示:

+ +
    +
  • 0 <= s.length <= 200
  • +
  • s 由英文字母(大写和小写)、数字(0-9)、' ''+''-' 和 + '.' 组成 +
  • +
+
+ +## template + +```cpp +int myAtoi(char *str) +{ + int i = 0; + int sign = 0; + while (str[i] && str[i] == ' ') + i++; + if (str[i] == NULL) + return 0; + if (str[i] == '-') + { + sign = 1; + i++; + } + else if (str[i] == '+') + { + sign = 0; + i++; + } + else if (str[i] < '0') + return 0; + else if (str[i] > '9') + return 0; + long long int r = 0; + while (str[i]) + { + if (str[i] < '0') + break; + else if (str[i] > '9') + break; + else + r = r * 10 + str[i++] - '0'; + if (r > INT_MAX) + break; + } + r = sign ? -r : r; + if (r < INT_MIN) + return INT_MIN; + if (r > INT_MAX) + return INT_MAX; + return (int)r; +} +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/35.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/35.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..984a83a2ca74fc4162087a4b20f82703f8192524 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/35.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-aa02e318150d41798d2da8389dc1c5f9", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/35.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/35.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..c4287f4cdc07b98bdabb745a3353c2499e5ef3fc --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/35.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "e8800b288bb348b0b569fb29d2682c69" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/35.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/35.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..239be455bc9d00e86fd5d91ee56d1026e9e06706 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/35.exercises/solution.md" @@ -0,0 +1,79 @@ +# 不同的二叉搜索树 + +
+

给你一个整数 n ,求恰由 n 个节点组成且节点值从 1n 互不相同的 二叉搜索树 + 有多少种?返回满足题意的二叉搜索树的种数。

+ +

 

+ +

示例 1:

+ +
输入:n = 3
+输出:5
+    
+ +

示例 2:

+ +
输入:n = 1
+输出:1
+    
+ +

 

+ +

提示:

+ +
    +
  • 1 <= n <= 19
  • +
+
+ +## template + +```cpp +#include +using namespace std; +class Solution +{ +public: + int numTrees(int n) + { + vector sum(n + 1); + sum[0] = 1; + for (int i = 1; i <= n; i++) + { + for (int j = 0; j < i; j++) + { + sum[i] += sum[j] * sum[i - j - 1]; + } + } + return sum[n]; + } +} +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/36.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/36.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..7fe5f6a315a9713144d1f3d4b1d422b2c3ca9b4c --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/36.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-f7e05e82ec564540adbd3aa01626b6fb", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/36.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/36.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..fa41e0b6c625d754075c5e051e3bcca5de2c2c53 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/36.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "e9aab0311df3445d84549457f68c7711" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/36.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/36.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..58e6b08dc0ca9ed24f36574de64ace9db3acd3f4 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/36.exercises/solution.md" @@ -0,0 +1,129 @@ +# 插入区间 + +

给你一个 无重叠的按照区间起始端点排序的区间列表。

+

在列表中插入一个新的区间,你需要确保列表中的区间仍然有序且不重叠(如果有必要的话,可以合并区间)。

+

 

+

示例 1:

+
输入:intervals = [[1,3],[6,9]], newInterval = [2,5]
输出:
[[1,5],[6,9]]
+

示例 2:

+
输入:intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8]
输出:
[[1,2],[3,10],[12,16]]
解释:
这是因为新的区间 [4,8] 与 [3,5],[6,7],[8,10] 重叠。
+

示例 3:

+
输入:intervals = [], newInterval = [5,7]
输出:
[[5,7]]
+

示例 4:

+
输入:intervals = [[1,5]], newInterval = [2,3]
输出:
[[1,5]]
+

示例 5:

+
输入:intervals = [[1,5]], newInterval = [2,7]
输出:
[[1,7]]
+

 

+

提示:

+
    +
  • 0 <= intervals.length <= 104
  • +
  • intervals[i].length == 2
  • +
  • 0 <= intervals[i][0] <= intervals[i][1] <= 105
  • +
  • intervals 根据 intervals[i][0]升序 排列
  • +
  • newInterval.length == 2
  • +
  • 0 <= newInterval[0] <= newInterval[1] <= 105
  • +
+ +## template + +```cpp +#include +#include +static int compare(const void *a, const void *b) +{ + return ((int *)a)[0] - ((int *)b)[0]; +} +int **insert(int **intervals, int intervalsSize, int *intervalsColSize, int *newInterval, + int newIntervalSize, int *returnSize, int **returnColumnSizes) +{ + int i, len = 0; + int *tmp = malloc((intervalsSize + 1) * 2 * sizeof(int)); + for (i = 0; i < intervalsSize; i++) + { + tmp[i * 2] = intervals[i][0]; + tmp[i * 2 + 1] = intervals[i][1]; + } + tmp[i * 2] = newInterval[0]; + tmp[i * 2 + 1] = newInterval[1]; + qsort(tmp, intervalsSize + 1, 2 * sizeof(int), compare); + int **results = malloc((intervalsSize + 1) * sizeof(int *)); + results[0] = malloc(2 * sizeof(int)); + results[0][0] = tmp[0]; + results[0][1] = tmp[1]; + for (i = 1; i < intervalsSize + 1; i++) + { + results[i] = malloc(2 * sizeof(int)); + if (tmp[i * 2] > results[len][1]) + { + len++; + results[len][0] = tmp[i * 2]; + results[len][1] = tmp[i * 2 + 1]; + } + else if (tmp[i * 2 + 1] > results[len][1]) + { + results[len][1] = tmp[i * 2 + 1]; + } + } + len += 1; + *returnSize = len; + *returnColumnSizes = malloc(len * sizeof(int)); + for (i = 0; i < len; i++) + { + (*returnColumnSizes)[i] = 2; + } + return results; +} +int main(int argc, char **argv) +{ + if (argc < 3 || argc % 2 == 0) + { + fprintf(stderr, "Usage: ./test new_s new_e s0 e0 s1 e1..."); + exit(-1); + } + int new_interv[2]; + new_interv[0] = atoi(argv[1]); + new_interv[1] = atoi(argv[2]); + int i, count = 0; + int *size = malloc((argc - 3) / 2 * sizeof(int)); + int **intervals = malloc((argc - 3) / 2 * sizeof(int *)); + for (i = 0; i < (argc - 3) / 2; i++) + { + intervals[i] = malloc(2 * sizeof(int)); + intervals[i][0] = atoi(argv[i * 2 + 3]); + intervals[i][1] = atoi(argv[i * 2 + 4]); + } + int *col_sizes; + int **results = insert(intervals, (argc - 3) / 2, size, new_interv, 2, &count, &col_sizes); + for (i = 0; i < count; i++) + { + printf("[%d,%d]\n", results[i][0], results[i][1]); + } + return 0; +} +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/37.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/37.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..68d4fc402e1e81806a4560aa85e62ba607ad6c78 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/37.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-212af846610b43c289eefa3509db2bfd", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/37.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/37.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..5cbd2e8269802ae8b6646bbf6a1b81d94c41ec32 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/37.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "5e28030ec2ce423d959793def8556046" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/37.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/37.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..641be4fa70952b9dcee505df5a168980ba2fd928 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/37.exercises/solution.md" @@ -0,0 +1,75 @@ +# 四数之和 + +

给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满足条件且不重复的四元组。

注意:答案中不可以包含重复的四元组。

 

示例 1:

输入:nums = [1,0,-1,0,-2,2], target = 0
输出:
[[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]

示例 2:

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

 

提示:

  • 0 <= nums.length <= 200
  • -109 <= nums[i] <= 109
  • -109 <= target <= 109
+ +## template + +```cpp +class Solution +{ +public: + vector> fourSum(vector &nums, int target) + { + long long l_target = target; + sort(nums.begin(), nums.end()); + vector> results; + int N = nums.size(); + for (int i = 0; i < N - 3; i++) + { + if (i > 0 && nums[i] == nums[i - 1]) + continue; + for (int j = i + 1; j < N - 2; j++) + { + if (j > i + 1 && nums[j] == nums[j - 1]) + continue; + for (int k = j + 1, l = N - 1; k < l; k++) + { + if (k > j + 1 && nums[k] == nums[k - 1]) + continue; + while (k < l && + (l_target - nums[i] - nums[j] - nums[k] - nums[l]) < 0) + { + l--; + } + if (k >= l) + { + break; + } + if ((target - nums[i] - nums[j] - nums[k] - nums[l]) == 0) + { + results.emplace_back( + vector({nums[i], nums[j], nums[k], nums[l]})); + } + } + } + } + return results; + } +}; +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/38.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/38.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..bb0b0ed8d206cc376ec9323f85aaaa2c42770475 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/38.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-de5fcdf4ae694c9faa792da822161ac5", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/38.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/38.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..68b037983cc5252665eebf969157d8525f6b02de --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/38.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "ef3012b1b938467686270795dc5139f1" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/38.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/38.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..7bf0f15dac4428bb7f4b24a67ba3e9874c876b3e --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/38.exercises/solution.md" @@ -0,0 +1,169 @@ +# 外观数列 + +
+

给定一个正整数 n ,输出外观数列的第 n 项。

+ +

「外观数列」是一个整数序列,从数字 1 开始,序列中的每一项都是对前一项的描述。

+ +

你可以将其视作是由递归公式定义的数字字符串序列:

+ +
    +
  • countAndSay(1) = "1"
  • +
  • countAndSay(n) 是对 countAndSay(n-1) 的描述,然后转换成另一个数字字符串。
  • +
+ +

前五项如下:

+ +
+    1.     1
+    2.     11
+    3.     21
+    4.     1211
+    5.     111221
+    第一项是数字 1 
+    描述前一项,这个数是 1 即 “ 一 个 1 ”,记作 "11"
+    描述前一项,这个数是 11 即 “ 二 个 1 ” ,记作 "21"
+    描述前一项,这个数是 21 即 “ 一 个 2 + 一 个 1 ” ,记作 "1211"
+    描述前一项,这个数是 1211 即 “ 一 个 1 + 一 个 2 + 二 个 1 ” ,记作 "111221"
+    
+ +

描述 一个数字字符串,首先要将字符串分割为 最小 数量的组,每个组都由连续的最多 相同字符 + 组成。然后对于每个组,先描述字符的数量,然后描述字符,形成一个描述组。要将描述转换为数字字符串,先将每组中的字符数量用数字替换,再将所有描述组连接起来。

+ +

例如,数字字符串 "3322251" 的描述如下图:

+ +
    +
+ +

 

+ +

示例 1:

+ +
输入:n = 1
+输出:"1"
+解释:这是一个基本样例。
+
+ +

示例 2:

+ +
输入:n = 4
+输出:"1211"
+解释:
+countAndSay(1) = "1"
+countAndSay(2) = 读 "1" = 一 个 1 = "11"
+countAndSay(3) = 读 "11" = 二 个 1 = "21"
+countAndSay(4) = 读 "21" = 一 个 2 + 一 个 1 = "12" + "11" = "1211"
+
+ +

 

+ +

提示:

+ +
    +
  • 1 <= n <= 30
  • +
+
+ +## template + +```cpp +#include +#include +#include +static void parse(char *input, char *output) +{ + char *p = input; + char *q = output; + while (*p != '\0') + { + int count = 1; + while (p[0] == p[1]) + { + count++; + p++; + } + int n = 0; + while (count > 0) + { + n += count % 10; + count /= 10; + } + while (n > 0) + { + *q++ = (n % 10) + '0'; + n /= 10; + } + *q++ = p[0]; + p++; + } + *q = '\0'; +} +static char *countAndSay(int n) +{ + if (n < 1) + { + return NULL; + } + char *result; + char *prev = malloc(10000); + char *next = malloc(10000); + strcpy(prev, "1"); + if (n == 1) + { + return prev; + } + int i; + for (i = 2; i <= n; i++) + { + if (i & 0x1) + { + parse(next, prev); + result = prev; + } + else + { + parse(prev, next); + result = next; + } + } + return result; +} +int main(int argc, char **argv) +{ + if (argc != 2) + { + fprintf(stderr, "Usage: ./test n\n"); + exit(-1); + } + printf("%s\n", countAndSay(atoi(argv[1]))); + return 0; +} +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/39.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/39.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..f6c9b5d3702b396cd1becc631e096cda3449f6c3 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/39.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-1ff251bb346248bf9d6fc2b77074c272", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/39.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/39.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..7a7390d80489844157099c00dff2790e62f7f5c1 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/39.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "81024ea85c804bb7b3326d871ac6ff0f" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/39.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/39.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..3e18230697a0c5d228b81f7f2c73027249610f7e --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/39.exercises/solution.md" @@ -0,0 +1,79 @@ +# 旋转链表 + +

给你一个链表的头节点 head ,旋转链表,将链表每个节点向右移动 k 个位置。

 

示例 1:

输入:head = [1,2,3,4,5], k = 2
输出:
[4,5,1,2,3]

示例 2:

输入:head = [0,1,2], k = 4
输出:
[2,0,1]

 

提示:

  • 链表中节点的数目在范围 [0, 500]
  • -100 <= Node.val <= 100
  • 0 <= k <= 2 * 109
+ +## template + +```cpp +#include +using namespace std; +struct ListNode +{ + int val; + ListNode *next; + ListNode() : val(0), next(nullptr) {} + ListNode(int x) : val(x), next(nullptr) {} + ListNode(int x, ListNode *next) : val(x), next(next) {} +}; +class Solution +{ +public: + ListNode *rotateRight(ListNode *head, int k) + { + if (head == nullptr) + { + return head; + } + int len = 0; + ListNode dummy; + dummy.next = head; + ListNode *tail = &dummy; + while (tail->next != nullptr) + { + len++; + tail = tail->next; + } + ListNode *prev = &dummy; + ListNode *p = head; + k = k % len; + for (int i = 0; i < len - k; i++) + { + prev = p; + p = p->next; + } + if (p != nullptr) + { + prev->next = tail->next; + tail->next = head; + head = p; + } + return head; + } +}; +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/40.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/40.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..efedca2813d76efc6802d019a5a936a5d4385bdd --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/40.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-b747b501547c4c33a615b1c426f41f8d", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/40.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/40.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..389f50ba029467e4cf54fb4bf2a93c722715c66c --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/40.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "e76d20bc167a43eda623d873cff44fdd" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/40.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/40.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..27bc5a602818c49650076e7bbc113377c134cc3a --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/40.exercises/solution.md" @@ -0,0 +1,66 @@ +# 组合 + +

给定两个整数 nk,返回 1 ... n 中所有可能的 k 个数的组合。

+

示例:

+
输入: n = 4, k = 2
输出:
[[2,4],[3,4],[2,3],[1,2],[1,3],[1,4],]
+ +## template + +```cpp +#include +using namespace std; +class Solution +{ +public: + vector> combine(int n, int k) + { + vector> res; + dfs(n, k, 1, res); + return res; + } +private: + vector stack; + void dfs(int n, int k, int start, vector> &res) + { + if (stack.size() == k) + { + res.push_back(stack); + } + else + { + for (int i = start; i <= n; i++) + { + stack.push_back(i); + dfs(n, k, i + 1, res); + stack.pop_back(); + } + } + } +}; +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/41.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/41.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..060f915ae9dbe32f2580f92cea9c4bc84abe84d5 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/41.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-b8e1807615a649e0bf801138606aea6c", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/41.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/41.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..5f1e2d930b119f1d656a18b7158f58f75d73a1f3 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/41.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "b034be7db3cc4b088019e7f1bc39928e" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/41.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/41.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..b9645095ea163e2588c9a31e9dc1f5c7f97262a9 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/41.exercises/solution.md" @@ -0,0 +1,69 @@ +# 括号生成 + +

数字 n 代表生成括号的对数,请你设计一个函数,用于能够生成所有可能的并且 有效的 括号组合。

 

示例 1:

输入:n = 3
输出:
["((()))","(()())","(())()","()(())","()()()"]

示例 2:

输入:n = 1
输出:
["()"]

 

提示:

  • 1 <= n <= 8
+ +## template + +```cpp +class Solution +{ +public: + void gen(string &p, int lc, int rc, vector &r, int n) + { + if (lc > n) + return; + if (lc == n && rc == n) + { + r.push_back(p); + return; + } + p.push_back('('); + lc++; + gen(p, lc, rc, r, n); + p.pop_back(); + lc--; + if (lc > rc) + { + p.push_back(')'); + rc++; + gen(p, lc, rc, r, n); + p.pop_back(); + rc--; + } + } + vector generateParenthesis(int n) + { + string p; + int lc = 0, rc = 0; + vector r; + gen(p, lc, rc, r, n); + return r; + } +}; +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/42.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/42.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..a7a7d84cdda976a4df948ecce6f677889c5ab816 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/42.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-7f83ccbcde5f4ec7902d76fe98f43624", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/42.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/42.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..2bec08e41e315d24ad113fbdba2efa1e38c47037 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/42.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "bd5fb0c1343a4a268467dae5c5a37565" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/42.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/42.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..8953283626bffb2352d7995095c0b56205801457 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/42.exercises/solution.md" @@ -0,0 +1,56 @@ +# 旋转图像 + +

给定一个 × n 的二维矩阵 matrix 表示一个图像。请你将图像顺时针旋转 90 度。

你必须在 原地 旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要 使用另一个矩阵来旋转图像。

 

示例 1:

输入:matrix = [[1,2,3],[4,5,6],[7,8,9]]
输出:
[[7,4,1],[8,5,2],[9,6,3]]

示例 2:

输入:matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]
输出:
[[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]

示例 3:

输入:matrix = [[1]]
输出:
[[1]]

示例 4:

输入:matrix = [[1,2],[3,4]]
输出:
[[3,1],[4,2]]

 

提示:

  • matrix.length == n
  • matrix[i].length == n
  • 1 <= n <= 20
  • -1000 <= matrix[i][j] <= 1000
+ +## template + +```cpp +#include +using namespace std; +class Solution +{ +public: + void rotate(vector> &matrix) + { + int size = matrix.size(); + for (int i = 0; i < size / 2; i++) + { + int low = i, high = size - i - 1; + for (int j = low; j < high; j++) + { + int tmp = matrix[i][j]; + matrix[i][j] = matrix[size - 1 - j][i]; + matrix[size - 1 - j][i] = matrix[size - 1 - i][size - 1 - j]; + matrix[size - 1 - i][size - 1 - j] = matrix[j][size - 1 - i]; + matrix[j][size - 1 - i] = tmp; + } + } + } +}; +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/43.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/43.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..0bdeb95e695bdf1d8d789778362e90577a4f1b8a --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/43.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-84b4b0d336d94b62ba1d797acc3baa15", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/43.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/43.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..e9d4d7019d7ea92654a34dc1e8b984aa5dcd2056 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/43.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "2d4b0e8669e5447dbfe1d80ba8c37bb9" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/43.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/43.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..1f592737744276305cc7ee4e988d783f409b6746 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/43.exercises/solution.md" @@ -0,0 +1,52 @@ +# 盛最多水的容器 + +

给你 n 个非负整数 a1,a2,...,an每个数代表坐标中的一个点 (i, ai) 。在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai)(i, 0) 。找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。

说明:你不能倾斜容器。

 

示例 1:

输入:[1,8,6,2,5,4,8,3,7]
输出:
49
解释:
图中垂直线代表输入数组 [1,8,6,2,5,4,8,3,7]。在此情况下,容器能够容纳水(表示为蓝色部分)的最大值为 49。

示例 2:

输入:height = [1,1]
输出:
1

示例 3:

输入:height = [4,3,2,1,4]
输出:
16

示例 4:

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

 

提示:

  • n = height.length
  • 2 <= n <= 3 * 104
  • 0 <= height[i] <= 3 * 104
+ +## template + +```cpp +#define MAX(a, b) (((a) < (b)) ? (b) : (a)) +#define MIN(a, b) (((a) > (b)) ? (b) : (a)) +int maxArea(int *height, int heightSize) +{ + int max = 0; + int i = 0, j = heightSize - 1; + int a; + while (i < j) + { + a = MIN(height[i], height[j]) * (j - i); + max = MAX(max, a); + if (height[i] > height[j]) + --j; + else + ++i; + } + return max; +} +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/44.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/44.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..4427b6d7c507876b60c30a5be69269e942791ba5 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/44.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-fd8c8c1c423746668bd05dbacae2ebf3", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/44.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/44.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..8c3cf962b77a4ab567463740dd39632d272a5a65 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/44.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "47174537a938405ebfa1778b36b1eb1f" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/44.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/44.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..61fd5994530c8c20b915fdc3b56d6f887b74ea03 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/44.exercises/solution.md" @@ -0,0 +1,121 @@ +# 复原 IP 地址 + +

给定一个只包含数字的字符串,用以表示一个 IP 地址,返回所有可能从 s 获得的 有效 IP 地址 。你可以按任何顺序返回答案。

有效 IP 地址 正好由四个整数(每个整数位于 0 到 255 之间组成,且不能含有前导 0),整数之间用 '.' 分隔。

例如:"0.1.2.201" 和 "192.168.1.1" 是 有效 IP 地址,但是 "0.011.255.245"、"192.168.1.312" 和 "192.168@1.1" 是 无效 IP 地址。

 

示例 1:

输入:s = "25525511135"
输出:
["255.255.11.135","255.255.111.35"]

示例 2:

输入:s = "0000"
输出:
["0.0.0.0"]

示例 3:

输入:s = "1111"
输出:
["1.1.1.1"]

示例 4:

输入:s = "010010"
输出:
["0.10.0.10","0.100.1.0"]

示例 5:

输入:s = "101023"
输出:
["1.0.10.23","1.0.102.3","10.1.0.23","10.10.2.3","101.0.2.3"]

 

提示:

  • 0 <= s.length <= 3000
  • s 仅由数字组成
+ +## template + +```cpp +#include +#include +#include +#include +static bool valid(char *ip, int len) +{ + if (len > 1 && ip[0] == '0') + { + return false; + } + if (len == 3) + { + int n = (ip[0] - '0') * 100 + (ip[1] - '0') * 10 + (ip[2] - '0'); + if (n > 255) + { + return false; + } + } + return true; +} +#define WIDTH 4 +static void dfs(char *s, int start, char *stack, int num, char **results, int *count) +{ + int i, j; + if (num == 4) + { + if (s[start] == '\0') + { + results[*count] = malloc(3 * 4 + 3 + 1); + char *p = results[*count]; + for (j = 0; j < num; j++) + { + char *q = stack + j * WIDTH; + while ((*p++ = *q++) != '\0') + { + } + if (j != 3) + { + *(p - 1) = '.'; + } + } + (*count)++; + } + } + else + { + char *p = stack + num * WIDTH; + char *q = p; + for (i = start; s[i] != '\0' && i < start + 3; i++) + { + *q++ = s[i]; + *q = '\0'; + if (!valid(p, q - p)) + { + return; + } + dfs(s, i + 1, stack, num + 1, results, count); + if (num + 1 < 4) + { + memset(stack + (num + 1) * WIDTH, 0, WIDTH); + } + } + } +} +static char **restoreIpAddresses(char *s, int *returnSize) +{ + int count = 0; + char **results = malloc(100 * sizeof(char *)); + char addr[16] = {'\0'}; + dfs(s, 0, addr, 0, results, &count); + *returnSize = count; + return results; +} +int main(int argc, char **argv) +{ + if (argc != 2) + { + fprintf(stderr, "Usage: ./test num\n"); + exit(-1); + } + int i, count = 0; + char **list = restoreIpAddresses(argv[1], &count); + for (i = 0; i < count; i++) + { + printf("%s\n", list[i]); + } +} +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/45.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/45.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..4a7e01a9f068622164a11099318c5c053103841d --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/45.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-51619e0a715f40458c8e536cf1ef034f", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/45.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/45.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..148bd2a80023325c3fa14cefe17a0fb530018c07 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/45.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "23ea1b3a9fde45e098ddded74e88b722" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/45.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/45.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..c10601abb0ebef6fd1555a6b29b8e18cdf0dc178 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/45.exercises/solution.md" @@ -0,0 +1,74 @@ +# 格雷编码 + +

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

+

给定一个代表编码总位数的非负整数 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]。
+ +## template + +```cpp +#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; +} +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/46.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/46.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..b53dee15359e090550ecda10c82fd0af9a2045fd --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/46.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-9b7485d651084adc90eaeca0d1dda61c", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/46.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/46.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..15dfcd05ab6b70910f62e061c5900f19d5c20f28 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/46.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "5705e480b1c847a28c814667896686b4" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/46.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/46.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..602191f9b4dcc8243d4721d34d64dcea41e8f054 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/46.exercises/solution.md" @@ -0,0 +1,62 @@ +# 下一个排列 + +

实现获取 下一个排列 的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列。

如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列)。

必须 原地 修改,只允许使用额外常数空间。

 

示例 1:

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

示例 2:

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

示例 3:

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

示例 4:

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

 

提示:

  • 1 <= nums.length <= 100
  • 0 <= nums[i] <= 100
+ +## template + +```cpp +#include +using namespace std; +class Solution +{ +public: + void nextPermutation(vector &nums) + { + if (nums.size() < 2) + { + return; + } + int i = nums.size() - 2; + while (i >= 0 && nums[i] >= nums[i + 1]) + { + i--; + } + if (i >= 0) + { + int j = nums.size() - 1; + while (j >= 0 && nums[j] >= nums[i]) + { + j--; + } + swap(nums.begin() + i, nums.begin() + j); + } + reverse(nums.begin() + i + 1, nums.end()); + } +}; +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/47.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/47.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..b0c17c2a452f834a2f5ccca8f82412ef5bd47ab1 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/47.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-1f951e2415de4a9e88c8c83b6d087d09", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/47.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/47.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..221b79378a7f56ed6813098fb04d795a74cc9801 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/47.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "885f94ac40214c9d8ace1cdbf1601f30" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/47.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/47.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..2b74d69fb38b8608a825a3bf74b7f15ba98b8d16 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/47.exercises/solution.md" @@ -0,0 +1,64 @@ +# 子集 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
+ +## template + +```cpp +#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]; + } + } +}; +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/48.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/48.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..2ca46cd88aa12b89555d574f7f62d3a2fb6703e7 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/48.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-1fc9992c7e8d4ff9aa56fed85c774328", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/48.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/48.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..ac7624f472cd48c1c63b16bb180aa7ecebd99a42 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/48.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "19cc14a0b9c049ce88be3e3e84adf922" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/48.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/48.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..c969c8812529b6e2ac0ce8225b1da00b84bd5a03 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/48.exercises/solution.md" @@ -0,0 +1,127 @@ +# 简化路径 + +

给你一个字符串 path ,表示指向某一文件或目录的 Unix 风格 绝对路径 (以 '/' 开头),请你将其转化为更加简洁的规范路径。

在 Unix 风格的文件系统中,一个点(.)表示当前目录本身;此外,两个点 (..) 表示将目录切换到上一级(指向父目录);两者都可以是复杂相对路径的组成部分。任意多个连续的斜杠(即,'//')都被视为单个斜杠 '/' 。 对于此问题,任何其他格式的点(例如,'...')均被视为文件/目录名称。

请注意,返回的 规范路径 必须遵循下述格式:

  • 始终以斜杠 '/' 开头。
  • 两个目录名之间必须只有一个斜杠 '/'
  • 最后一个目录名(如果存在)不能 '/' 结尾。
  • 此外,路径仅包含从根目录到目标文件或目录的路径上的目录(即,不含 '.''..')。

返回简化后得到的 规范路径

 

示例 1:

输入:path = "/home/"
输出:
"/home"
解释:
注意,最后一个目录名后面没有斜杠。

示例 2:

输入:path = "/../"
输出:
"/"
解释:
从根目录向上一级是不可行的,因为根目录是你可以到达的最高级。

示例 3:

输入:path = "/home//foo/"
输出:
"/home/foo"
解释:
在规范路径中,多个连续斜杠需要用一个斜杠替换。

示例 4:

输入:path = "/a/./b/../../c/"
输出:
"/c"

 

提示:

  • 1 <= path.length <= 3000
  • path 由英文字母,数字,'.''/''_' 组成。
  • path 是一个有效的 Unix 风格绝对路径。
+ +## template + +```cpp +#include +#include +#include +static char *simplifyPath(char *path) +{ + int len = strlen(path); + if (len == 0) + { + return path; + } + char *p = path; + int *indexes = malloc(len * sizeof(int)); + int depth = 0; + int name_start = 1; + while (*p != '\0') + { + if (*p == '/') + { + if (p > path && *(p - 1) != '/' && *(p - 1) != '.') + { + name_start = 1; + } + } + else if (*p == '.') + { + if (*(p + 1) == '\0' || *(p + 1) == '/') + { + p += 1; + } + else if (*(p + 1) == '.' && (*(p + 2) == '\0' || *(p + 2) == '/')) + { + if (depth > 0) + { + depth--; + name_start = 1; + } + p += 2; + } + else + { + indexes[depth++] = p - path; + while (*p != '/' && *p != '\0') + { + p++; + } + } + if (*p == '\0') + { + break; + } + } + else + { + if (name_start && depth >= 0) + { + indexes[depth++] = p - path; + name_start = 0; + } + } + p++; + } + int i; + char *result = malloc(len + 1); + char *q = result; + if (depth <= 0) + { + *q++ = '/'; + } + else + { + for (i = 0; i < depth; i++) + { + p = path + indexes[i]; + *q++ = '/'; + while (*p != '/') + { + *q++ = *p++; + } + } + } + *q = '\0'; + return result; +} +int main(int argc, char **argv) +{ + if (argc != 2) + { + fprintf(stderr, "Usage: ./test path\n"); + exit(-1); + } + printf("%s\n", simplifyPath(argv[1])); + return 0; +} +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/49.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/49.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..9c9244e358c7f3403cd1124dd6edac857500d095 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/49.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-928c6b0794724b70bc3dc240e8733cff", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/49.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/49.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..8afe0aab23f55be93b3c1982ff4986fd14c5db38 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/49.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "1028764173774a40a773e47eba18d5e6" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/49.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/49.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..06da7723b930721d860250609f8cc214abc92bc2 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/49.exercises/solution.md" @@ -0,0 +1,84 @@ +# 两数相加 + +

给你两个 非空 的链表,表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的,并且每个节点只能存储 一位 数字。

请你将两个数相加,并以相同形式返回一个表示和的链表。

你可以假设除了数字 0 之外,这两个数都不会以 0 开头。

 

示例 1:

输入:l1 = [2,4,3], l2 = [5,6,4]
输出:
[7,0,8]
解释:
342 + 465 = 807.

示例 2:

输入:l1 = [0], l2 = [0]
输出:
[0]

示例 3:

输入:l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
输出:
[8,9,9,9,0,0,0,1]

 

提示:

  • 每个链表中的节点数在范围 [1, 100]
  • 0 <= Node.val <= 9
  • 题目数据保证列表表示的数字不含前导零
+ +## template + +```cpp +struct ListNode +{ + int val; + struct ListNode *next; +}; +struct ListNode *addTwoNumbers(struct ListNode *l1, struct ListNode *l2) +{ + struct ListNode *pp = NULL, *p = l1; + struct ListNode *qp = NULL, *q = l2; + int carry = 0; + while (p != NULL && q != NULL) + { + p->val += q->val + carry; + carry = 0; + if (p->val >= 10) + { + carry = 1; + p->val -= 10; + } + pp = p; + p = p->next; + qp = q; + q = q->next; + } + if (q) + { + pp->next = p = q; + qp->next = NULL; + } + while (carry && p) + { + p->val += carry; + carry = 0; + if (p->val >= 10) + { + carry = 1; + p->val -= 10; + } + pp = p; + p = p->next; + } + if (carry) + { + struct ListNode *n = (struct ListNode *)malloc(sizeof(struct ListNode)); + n->val = 1; + n->next = NULL; + pp->next = n; + } + return l1; +} +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/50.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/50.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..c8533ddc7ff6a7dcd7aa81711b220d891f16eaca --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/50.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-cf91b72dc8c348aba6f4a67ec2bdc3d4", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/50.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/50.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..f3307c7e896b33a2784e611b9e58c43f56a72ef3 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/50.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "d88d359a403742f58ef43ed77f9d6f07" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/50.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/50.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..919bbf9ad7639910e187eaba8159a718032b6633 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/50.exercises/solution.md" @@ -0,0 +1,110 @@ +# 分隔链表 + +

给你一个链表的头节点 head 和一个特定值 x ,请你对链表进行分隔,使得所有 小于 x 的节点都出现在 大于或等于 x 的节点之前。

你应当 保留 两个分区中每个节点的初始相对位置。

 

示例 1:

输入:head = [1,4,3,2,5,2], x = 3
输出
:[1,2,2,4,3,5]

示例 2:

输入:head = [2,1], x = 2
输出
:[1,2]

 

提示:

  • 链表中节点的数目在范围 [0, 200]
  • -100 <= Node.val <= 100
  • -200 <= x <= 200
+ +## template + +```cpp +#include +#include +struct ListNode +{ + int val; + struct ListNode *next; +}; +struct ListNode *partition(struct ListNode *head, int x) +{ + struct ListNode dummy; + struct ListNode *prev1 = &dummy, *pivot; + dummy.next = head; + for (pivot = head; pivot != NULL; pivot = pivot->next) + { + if (pivot->val >= x) + { + break; + } + prev1 = pivot; + } + struct ListNode *p = pivot->next; + struct ListNode *prev2 = pivot; + while (p != NULL) + { + if (p->val < x) + { + prev2->next = p->next; + p->next = prev1->next; + prev1->next = p; + prev1 = p; + p = prev2->next; + } + else + { + prev2 = p; + p = p->next; + } + } + return dummy.next; +} +int main(int argc, char **argv) +{ + if (argc < 2) + { + fprintf(stderr, "Usage: ./test target n1 n2 n3...\n"); + exit(-1); + } + int i, target = atoi(argv[1]); + struct ListNode *head = NULL; + struct ListNode *prev = NULL; + struct ListNode *p; + for (i = 0; i < argc - 2; i++) + { + p = malloc(sizeof(*p)); + p->val = atoi(argv[i + 2]); + p->next = NULL; + if (head == NULL) + { + head = p; + prev = head; + } + else + { + prev->next = p; + prev = p; + } + } + p = partition(head, target); + while (p != NULL) + { + printf("%d ", p->val); + p = p->next; + } + printf("\n"); + return 0; +} +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/51.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/51.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..d4e1376a5b6c6b876b6776454908eb5ccc19dbd9 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/51.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-8373188349fb4a47bf7ad57840dde54e", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/51.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/51.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..a6b24694223fad7563e9c3e5b18c19eb77a5cb2f --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/51.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "02a5a650f836419aa1368f3eab1f7b53" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/51.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/51.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..1c98e39f490d2f21f1a760260efc10162d92c9e4 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/51.exercises/solution.md" @@ -0,0 +1,141 @@ +# 整数转罗马数字 + +
+

罗马数字包含以下七种字符: I, V, X, LCD 和 M。 +

+ +
字符          数值
+I             1
+V             5
+X             10
+L             50
+C             100
+D             500
+M             1000
+ +

例如, 罗马数字 2 写做 II ,即为两个并列的 1。12 + 写做 XII ,即为 X + II 。 27 + 写做  XXVII, + 即为 XX + V + II 。

+ +

通常情况下,罗马数字中小的数字在大的数字的右边。但也存在特例,例如 4 不写做 IIII,而是 IV。数字 1 在数字 5 的左边,所表示的数等于大数 5 + 减小数 1 得到的数值 4 。同样地,数字 9 表示为 IX。这个特殊的规则只适用于以下六种情况:

+ +
    +
  • I 可以放在 V (5) 和 X (10) 的左边,来表示 4 和 9。
  • +
  • X 可以放在 L (50) 和 C (100) 的左边,来表示 40 + 和 90。 
  • +
  • C 可以放在 D (500) 和 M (1000) 的左边,来表示 400 + 和 900。
  • +
+ +

给你一个整数,将其转为罗马数字。

+ +

 

+ +

示例 1:

+ +
输入: num = 3
+
输出:
"III"
+ +

示例 2:

+ +
输入: num = 4
+
输出:
"IV"
+ +

示例 3:

+ +
输入: num = 9
+
输出:
"IX"
+ +

示例 4:

+ +
输入: num = 58
+
输出:
"LVIII" +
解释:
L = 50, V = 5, III = 3. +
+ +

示例 5:

+ +
输入: num = 1994
+
输出:
"MCMXCIV" +
解释:
M = 1000, CM = 900, XC = 90, IV = 4.
+ +

 

+ +

提示:

+ +
    +
  • 1 <= num <= 3999
  • +
+
+ +## template + +```cpp +struct rmap +{ + char *r; + int v; +} units[] = { + {"M", 1000}, + {"CM", 900}, + {"D", 500}, + {"CD", 400}, + {"C", 100}, + {"XC", 90}, + {"L", 50}, + {"XL", 40}, + {"X", 10}, + {"IX", 9}, + {"V", 5}, + {"IV", 4}, + {"I", 1}}; +#include +char result[64]; +char *intToRoman(int num) +{ + result[0] = 0; + int ri = 0; + int i = 0; + while (num) + { + if (num >= units[i].v) + { + strcat(result, units[i].r); + num -= units[i].v; + } + else + { + i++; + } + } + return result; +} +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/52.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/52.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..1b3ec170dd7a1feb04c92db71192ea3286b85fde --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/52.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-f7a5060bde2647e09318669616a51712", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/52.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/52.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..920f0cf9965bfac5728481fc2bbf152bab1ffae5 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/52.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "5bfd7c1c6f5440c28e818037a634e6be" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/52.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/52.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..82c60e881f9eb49517c090fc3827d7ab6e8460d2 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/52.exercises/solution.md" @@ -0,0 +1,75 @@ +# 全排列 II + +

给定一个可包含重复数字的序列 nums按任意顺序 返回所有不重复的全排列。

 

示例 1:

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

示例 2:

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

 

提示:

  • 1 <= nums.length <= 8
  • -10 <= nums[i] <= 10
+ +## template + +```cpp +#include +using namespace std; +class Solution +{ +public: + vector> permuteUnique(vector &nums) + { + vector> res; + vector used(nums.size()); + sort(nums.begin(), nums.end()); + dfs(nums, used, res); + return res; + } +private: + vector stack; + void dfs(vector &nums, vector &used, vector> &res) + { + if (stack.size() == nums.size()) + { + res.push_back(stack); + } + else + { + for (int i = 0; i < nums.size(); i++) + { + if (!used[i]) + { + if (i > 0 && !used[i - 1] && nums[i - 1] == nums[i]) + { + continue; + } + stack.push_back(nums[i]); + used[i] = true; + dfs(nums, used, res); + stack.pop_back(); + used[i] = false; + } + } + } + } +}; +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/53.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/53.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..7979efed33c6439e1e45f5da4792396d5b0d219d --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/53.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-3cb01d219ea94075aa818ffdb924eafc", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/53.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/53.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..bdbe0f748c3c2fdbce6e8ece428d0079309b30c5 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/53.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "659e43e9c9834c6ebcbea71d52dc8700" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/53.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/53.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..9bcad532dd31bb66baacf70980a5e5f6e54599bb --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/53.exercises/solution.md" @@ -0,0 +1,49 @@ +# 无重复字符的最长子串 + +

给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。

 

示例 1:

输入: s = "abcabcbb"
输出:
3
解释:
因为无重复字符的最长子串是 "abc",所以其长度为 3。

示例 2:

输入: s = "bbbbb"
输出:
1
解释:
因为无重复字符的最长子串是 "b",所以其长度为 1。

示例 3:

输入: s = "pwwkew"
输出:
3
解释:
因为无重复字符的最长子串是 "wke",所以其长度为 3。  +请注意,你的答案必须是 子串 的长度,"pwke" 是一个子序列,不是子串。

示例 4:

输入: s = ""
输出:
0

 

提示:

  • 0 <= s.length <= 5 * 104
  • s 由英文字母、数字、符号和空格组成
+ +## template + +```cpp +int hset[128]; +int lengthOfLongestSubstring(char *s) +{ + int i = 0, j = 0; + int m = 0; + memset(hset, 0, sizeof hset); + for (; s[j]; j++) + { + i = hset[s[j]] > i ? hset[s[j]] : i; + m = m > j - i + 1 ? m : j - i + 1; + hset[s[j]] = j + 1; + } + return m; +} +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/54.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/54.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..3c6f0e04b63d0f4d3a1a8037778780f7098faebf --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/54.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-ea4fd5babea7407aa08f1126f8513d6a", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/54.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/54.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..db2cf310c6808116b6bd97cedb90d2315552c859 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/54.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "319e83085f474d9faba6d4c57cb750ab" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/54.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/54.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..ce47e902e3166a16f7b2c6a305ac9183dceb90a6 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/54.exercises/solution.md" @@ -0,0 +1,96 @@ +# 解码方法 + +

一条包含字母 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 只包含数字,并且可能包含前导零。
  • +
+ +## template + +```cpp +#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; +} +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/55.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/55.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..ff60a2704fe958dbf4f9785d69a6e632bf37c65f --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/55.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-f7829033277c41ff91ba6c0cd74e8b2d", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/55.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/55.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..745c583bd6bbbab4e38df19d7f873b59b6011f5b --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/55.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "90bc4ad92b1b410892f344b0947ad1a6" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/55.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/55.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..f6b2abf3351e0e836dc42b185bab23a50c764fd7 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/55.exercises/solution.md" @@ -0,0 +1,79 @@ +# 两数相除 + +

给定两个整数,被除数 dividend 和除数 divisor。将两数相除,要求不使用乘法、除法和 mod 运算符。

返回被除数 dividend 除以除数 divisor 得到的商。

整数除法的结果应当截去(truncate)其小数部分,例如:truncate(8.345) = 8 以及 truncate(-2.7335) = -2

 

示例 1:

输入: dividend = 10, divisor = 3
输出:
3
解释:
10/3 = truncate(3.33333..) = truncate(3) = 3

示例 2:

输入: dividend = 7, divisor = -3
输出:
-2
解释:
7/-3 = truncate(-2.33333..) = -2

 

提示:

  • 被除数和除数均为 32 位有符号整数。
  • 除数不为 0。
  • 假设我们的环境只能存储 32 位有符号整数,其数值范围是 [−231,  231 − 1]。本题中,如果除法结果溢出,则返回 231 − 1。
+ +## template + +```cpp +#include +using namespace std; +class Solution +{ +public: + int divide(int dividend, int divisor) + { + int signal = 1; + unsigned int dvd = dividend; + if (dividend < 0) + { + signal *= -1; + dvd = ~dvd + 1; + } + unsigned int dvs = divisor; + if (divisor < 0) + { + signal *= -1; + dvs = ~dvs + 1; + } + int shift = 0; + while (dvd > dvs << shift) + { + shift++; + } + unsigned int res = 0; + while (dvd >= dvs) + { + while (dvd < dvs << shift) + { + shift--; + } + res |= (unsigned int)1 << shift; + dvd -= dvs << shift; + } + if (signal == 1 && res >= INT_MAX) + { + return INT_MAX; + } + else + { + return res * signal; + } + } +}; +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/56.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/56.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..d639eb06b877c588d9ed0226e88c53e9021b4f27 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/56.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-bf307c2ca5f5441da2ef689a1defa3d5", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/56.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/56.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..e62b494c74546338f48a24befdd2eb770d7f4cf6 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/56.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "187f2a49ad424c6dac4fae50290eb29f" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/56.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/56.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..f4e37b081aac84b331a11a9b3f1ef47d3334bff0 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/56.exercises/solution.md" @@ -0,0 +1,88 @@ +# 螺旋矩阵 + +

给你一个 mn 列的矩阵 matrix ,请按照 顺时针螺旋顺序 ,返回矩阵中的所有元素。

 

示例 1:

输入:matrix = [[1,2,3],[4,5,6],[7,8,9]]
输出:
[1,2,3,6,9,8,7,4,5]

示例 2:

输入:matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
输出:
[1,2,3,4,8,12,11,10,9,5,6,7]

 

提示:

  • m == matrix.length
  • n == matrix[i].length
  • 1 <= m, n <= 10
  • -100 <= matrix[i][j] <= 100
+ +## template + +```cpp +#include +using namespace std; +class Solution +{ +public: + vector spiralOrder(vector> &matrix) + { + vector res; + int hor_top = 0; + int hor_bottom = matrix.size() - 1; + int ver_left = 0; + int ver_right = matrix[0].size() - 1; + int direction = 0; + while (hor_top <= hor_bottom && ver_left <= ver_right) + { + switch (direction) + { + case 0: + for (int i = ver_left; i <= ver_right; i++) + { + res.push_back(matrix[hor_top][i]); + } + hor_top++; + break; + case 1: + for (int i = hor_top; i <= hor_bottom; i++) + { + res.push_back(matrix[i][ver_right]); + } + ver_right--; + break; + case 2: + for (int i = ver_right; i >= ver_left; i--) + { + res.push_back(matrix[hor_bottom][i]); + } + hor_bottom--; + break; + case 3: + for (int i = hor_bottom; i >= hor_top; i--) + { + res.push_back(matrix[i][ver_left]); + } + ver_left++; + break; + default: + break; + } + direction++; + direction %= 4; + } + return res; + } +}; +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/57.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/57.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..67258e38be3803a2fd10682b1a1470eb71fe4e2f --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/57.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-80598659b17144ad9da321739cc45bd3", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/57.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/57.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..86effe30477b804207a785bd3ac9b51801ecfb65 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/57.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "4984a5949e9b456b8d2c3e4647e2a3ac" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/57.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/57.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..5e1f20b607e7d28bf5dd4dcd342ba2ff7d5a3dd1 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/57.exercises/solution.md" @@ -0,0 +1,70 @@ +# 全排列 + +

给定一个 没有重复 数字的序列,返回其所有可能的全排列。

示例:

输入: [1,2,3]
输出:
[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]]
+ +## template + +```cpp +#include +using namespace std; +class Solution +{ +public: + vector> permute(vector &nums) + { + vector> res; + vector used(nums.size()); + dfs(nums, used, res); + return res; + } +private: + vector stack; + void dfs(vector &nums, vector &used, vector> &res) + { + if (stack.size() == nums.size()) + { + res.push_back(stack); + } + else + { + for (int i = 0; i < nums.size(); i++) + { + if (!used[i]) + { + used[i] = true; + stack.push_back(nums[i]); + dfs(nums, used, res); + stack.pop_back(); + used[i] = false; + } + } + } + } +}; +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/58.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/58.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..680b83e6d2c86f716d3a0c40fff084f093d4727f --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/58.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-bdf3f551b3894151b170852c30440ad0", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/58.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/58.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..b0fa06788ba64d4849b6496f1d780ac2fd079745 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/58.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "e064adbef94341589c7a189c0cdced7d" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/58.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/58.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..2d7053c4a40197414bd88ccdb757cc45e8e52bdf --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/58.exercises/solution.md" @@ -0,0 +1,101 @@ +# 搜索旋转排序数组 + +

整数数组 nums 按升序排列,数组中的值 互不相同

+

在传递给函数之前,nums 在预先未知的某个下标 k0 <= k < nums.length)上进行了 旋转,使数组变为 + [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]](下标 从 0 开始 + 计数)。例如, [0,1,2,4,5,6,7] 在下标 3 处经旋转后可能变为 [4,5,6,7,0,1,2] 。 +

+

给你 旋转后 的数组 nums 和一个整数 target ,如果 nums 中存在这个目标值 + target ,则返回它的下标,否则返回 -1 。 +

+

 

+

示例 1:

+
输入:nums = [4,5,6,7,0,1,2], target = 0
输出:
4
+

示例 2:

+
输入:nums = [4,5,6,7,0,1,2], target = 3
输出:
-1
+

示例 3:

+
输入:nums = [1], target = 0
输出:
-1
+

 

+

提示:

+
    +
  • 1 <= nums.length <= 5000
  • +
  • -10^4 <= nums[i] <= 10^4
  • +
  • nums 中的每个值都 独一无二
  • +
  • 题目数据保证 nums 在预先未知的某个下标上进行了旋转
  • +
  • -10^4 <= target <= 10^4
  • +
+

 

+

进阶:你可以设计一个时间复杂度为 O(log n) 的解决方案吗?

+ +## template + +```cpp +#include +using namespace std; +class Solution +{ +public: + int search(vector &nums, int target) + { + int lo = 0; + int hi = nums.size() - 1; + for (lo <= hi) + { + int mid = lo + (hi - lo) / 2; + if (nums[mid] == target) + { + return mid; + } + if (nums[lo] <= nums[mid]) + { + if (nums[lo] <= target && target < nums[mid]) + { + hi = mid - 1; + } + else + { + lo = mid + 1; + } + } + else + { + if (nums[mid] < target && target <= nums[hi]) + { + lo = mid + 1; + } + else + { + hi = mid - 1; + } + } + } + return -1; + } +}; +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/59.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/59.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..7903a8bfdc2b9a8e6911e804a160b0c40eed5f91 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/59.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-f88b0f4c34bd4cc8bb4ed8bff872eea2", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/59.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/59.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..e30087990a19bc1580a735a073707a6e08814e50 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/59.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "3ddcdae838f24548950e8152dbc8ef5d" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/59.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/59.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..28e467904269723a214837b3acc9aff674d91c27 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/59.exercises/solution.md" @@ -0,0 +1,125 @@ +# 搜索二维矩阵 + +

编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值。该矩阵具有如下特性:

  • 每行中的整数从左到右按升序排列。
  • 每行的第一个整数大于前一行的最后一个整数。

 

示例 1:

输入:matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3
输出:
true

示例 2:

输入:matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13
输出:
false

 

提示:

  • m == matrix.length
  • n == matrix[i].length
  • 1 <= m, n <= 100
  • -104 <= matrix[i][j], target <= 104
+ +## template + +```cpp +#include +#include +#include +static int binary_search(int *nums, int len, int target) +{ + int low = -1; + int high = len; + while (low + 1 < high) + { + int mid = low + (high - low) / 2; + if (target > nums[mid]) + { + low = mid; + } + else + { + high = mid; + } + } + if (high == len || nums[high] != target) + { + return -high - 1; + } + else + { + return high; + } +} +static bool searchMatrix(int **matrix, int matrixRowSize, int matrixColSize, int target) +{ + if (matrixRowSize == 0 || matrixColSize == 0) + { + return false; + } + if (target < matrix[0][0] || target > matrix[matrixRowSize - 1][matrixColSize - 1]) + { + return false; + } + int row = 0; + int *nums = NULL; + if (matrixRowSize > 0) + { + nums = malloc(matrixRowSize * sizeof(int)); + for (row = 0; row < matrixRowSize; row++) + { + nums[row] = matrix[row][0]; + } + row = binary_search(nums, matrixRowSize, target); + if (row >= 0) + { + return true; + } + else + { + row = -row - 1; + if (row == 0) + { + return false; + } + else + { + row--; + } + } + } + int col = binary_search(matrix[row], matrixColSize, target); + return col >= 0; +} +int main(int argc, char **argv) +{ + int row = 3; + int col = 4; + int **mat = malloc(row * sizeof(int *)); + mat[0] = malloc(col * sizeof(int)); + mat[0][0] = 1; + mat[0][1] = 3; + mat[0][2] = 5; + mat[0][3] = 7; + mat[1] = malloc(col * sizeof(int)); + mat[1][0] = 10; + mat[1][1] = 11; + mat[1][2] = 16; + mat[1][3] = 20; + mat[2] = malloc(col * sizeof(int)); + mat[2][0] = 23; + mat[2][1] = 30; + mat[2][2] = 34; + mat[2][3] = 50; + printf("%s\n", searchMatrix(mat, row, col, atoi(argv[1])) ? "true" : "false"); + return 0; +} +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/60.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/60.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..079f898bb0746d1b1a1da07c01684561da684b4c --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/60.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-ec4d5b7bf8324d3cbaaab556e3dcd2c6", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/60.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/60.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..402bf0aae69f987c7b9c776daeb4cf97a08ea98f --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/60.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "a3a7e6dbce284af5a886e41dc168e68d" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/60.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/60.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..68d0b45ce516bbb6461f43f0fa474d0d8d26d2e9 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/60.exercises/solution.md" @@ -0,0 +1,69 @@ +# Pow(x, n) + +

实现 pow(x, n) ,即计算 x 的 n 次幂函数(即,xn)。

 

示例 1:

输入:x = 2.00000, n = 10
输出:
1024.00000

示例 2:

输入:x = 2.10000, n = 3
输出:
9.26100

示例 3:

输入:x = 2.00000, n = -2
输出:
0.25000
解释:
2-2 = 1/22 = 1/4 = 0.25

 

提示:

  • -100.0 < x < 100.0
  • -231 <= n <= 231-1
  • -104 <= xn <= 104
+ +## template + +```cpp +#include +using namespace std; +class Solution +{ +public: + double myPow(double x, int n) + { + if (n == INT_MIN) + { + double t = dfs(x, -(n / 2)); + return 1 / t * 1 / t; + } + else + { + return n < 0 ? 1 / dfs(x, -n) : dfs(x, n); + } + } +private: + double dfs(double x, int n) + { + if (n == 0) + { + return 1; + } + else if (n == 1) + { + return x; + } + else + { + double t = dfs(x, n / 2); + return (n % 2) ? (x * t * t) : (t * t); + } + } +}; +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/61.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/61.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..b57a7a76c09f4ae0aa78ccb19804e566487a3505 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/61.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-d0eb65c248e441d6af43e9d3a69232d0", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/61.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/61.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..1182b28948b88896b71099d698ad275653518f49 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/61.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "d8cc99ee4afc43b09b1f974be33bfb98" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/61.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/61.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..8b514eca174daab96af6374baf23e142c3a3f23f --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/61.exercises/solution.md" @@ -0,0 +1,72 @@ +# 字母异位词分组 + +

给定一个字符串数组,将字母异位词组合在一起。字母异位词指字母相同,但排列不同的字符串。

+

示例:

+
输入:[eat", "tea", "tan", "ate", "nat", "bat"]
输出:
[[ate","eat","tea"],["nat","tan"],["bat"]]
+

说明:

+
    +
  • 所有输入均为小写字母。
  • +
  • 不考虑答案输出的顺序。
  • +
+ +## template + +```cpp +#include +using namespace std; +class Solution +{ +public: + vector> groupAnagrams(vector &strs) + { + vector> res; + unordered_map> ht; + for (const auto &str : strs) + { + int counts[26] = {0}; + for (char c : str) + { + counts[c - 'a']++; + } + string key; + for (int i : counts) + { + key.push_back('#'); + key.push_back(i + '0'); + } + ht[key].push_back(str); + } + for (const auto &t : ht) + { + res.push_back(t.second); + } + return res; + } +}; +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/62.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/62.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..19d79bb910aa91e3638eff211b42c376b69970c8 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/62.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-2d76c5623b5f412f811d07ad8fa0afa2", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/62.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/62.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..4c85f5cbdf9bf798eefe09a75f07d96fa0725d2d --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/62.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "4846f615a38143eab13aff6f861bfd91" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/62.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/62.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..561e95a62a4e52780ddee404e0987a68baf6c7a7 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/62.exercises/solution.md" @@ -0,0 +1,85 @@ +# 矩阵置零 + +

给定一个 m x n 的矩阵,如果一个元素为 0 ,则将其所在行和列的所有元素都设为 0 。请使用 原地 算法

进阶:

  • 一个直观的解决方案是使用  O(mn) 的额外空间,但这并不是一个好的解决方案。
  • 一个简单的改进方案是使用 O(m + n) 的额外空间,但这仍然不是最好的解决方案。
  • 你能想出一个仅使用常量空间的解决方案吗?

 

示例 1:

输入:matrix = [[1,1,1],[1,0,1],[1,1,1]]
输出:
[[1,0,1],[0,0,0],[1,0,1]]

示例 2:

输入:matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]]
输出:
[[0,0,0,0],[0,4,5,0],[0,3,1,0]]

 

提示:

  • m == matrix.length
  • n == matrix[0].length
  • 1 <= m, n <= 200
  • -231 <= matrix[i][j] <= 231 - 1
+ +## template + +```cpp +#include +using namespace std; +public: +void setZeroes(vector> &matrix) +{ + bool bRow = false, bCol = false; + for (int row = 0; row < matrix.size(); row++) + { + for (int col = 0; col < matrix[row].size(); col++) + { + if (matrix[row][col] == 0) + { + if (row == 0) + { + bRow = true; + } + if (col == 0) + { + bCol = true; + } + matrix[0][col] = matrix[row][0] = 0; + } + } + } + for (int row = 1; row < matrix.size(); row++) + { + for (int col = 1; col < matrix[row].size(); col++) + { + if (matrix[0][col] == 0 || matrix[row][0] == 0) + { + matrix[row][col] = 0; + } + } + } + if (bRow) + { + for (auto &m : matrix[0]) + { + m = 0; + } + } + if (bCol) + { + for (int row = 0; row < matrix.size(); row++) + { + matrix[row][0] = 0; + } + } +} +} +; +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/63.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/63.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..314061160883dc70016086dc40b3eb64e21ab969 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/63.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-3f77a681904f4599aa76fe9d85baac80", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/63.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/63.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..4170ec135e950bf9c4c061d34de3c97ddb257a9d --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/63.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "19fafe8645294fe4b4d20c1122482404" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/63.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/63.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..a9dd4649e051374a506341e08c19cc7d6485d7bf --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/63.exercises/solution.md" @@ -0,0 +1,118 @@ +# 在排序数组中查找元素的第一个和最后一个位置 + +

给定一个按照升序排列的整数数组 nums,和一个目标值 target。找出给定目标值在数组中的开始位置和结束位置。

+

如果数组中不存在目标值 target,返回 [-1, -1]

+

进阶:

+
    +
  • 你可以设计并实现时间复杂度为 O(log n) 的算法解决此问题吗?
  • +
+

 

+

示例 1:

+
输入:nums = [5,7,7,8,8,10], target = 8
输出:
[3,4]
+

示例 2:

+
输入:nums = [5,7,7,8,8,10], target = 6
输出:
[-1,-1]
+

示例 3:

+
输入:nums = [], target = 0
输出:
[-1,-1]
+

 

+

提示:

+
    +
  • 0 <= nums.length <= 105
  • +
  • -109 <= nums[i] <= 109
  • +
  • nums 是一个非递减数组
  • +
  • -109 <= target <= 109
  • +
+ +## template + +```cpp +#include +using namespace std; +class Solution +{ +public: + vector searchRange(vector &nums, int target) + { + vector res; + res.push_back(binary_search_begin(nums, target)); + res.push_back(binary_search_end(nums, target)); + return res; + } +private: + int binary_search_begin(vector nums, int target) + { + int lo = -1; + int hi = nums.size(); + while (lo + 1 < hi) + { + int mid = lo + (hi - lo) / 2; + if (target > nums[mid]) + { + lo = mid; + } + else + { + hi = mid; + } + } + if (hi == nums.size() || nums[hi] != target) + { + return -1; + } + else + { + return hi; + } + } + int binary_search_end(vector nums, int target) + { + int lo = -1; + int hi = nums.size(); + while (lo + 1 < hi) + { + int mid = lo + (hi - lo) / 2; + if (target < nums[mid]) + { + hi = mid; + } + else + { + lo = mid; + } + } + if (lo == -1 || nums[lo] != target) + { + return -1; + } + else + { + return lo; + } + } +}; +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/64.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/64.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..37488922b2dea07c52257d48f4a9512a11df58da --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/64.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-8819f23a2aa64fbfa2222cd407056311", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/64.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/64.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..acec684b6cf3d556cc8b7a1f905770b67e89823f --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/64.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "ba53f24875754536aa23cb21d995c1de" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/64.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/64.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..bc92a5fe460c6388128bbf13ca8ea01c31bd9c80 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/64.exercises/solution.md" @@ -0,0 +1,63 @@ +# 跳跃游戏 II + +

给定一个非负整数数组,你最初位于数组的第一个位置。

+

数组中的每个元素代表你在该位置可以跳跃的最大长度。

+

你的目标是使用最少的跳跃次数到达数组的最后一个位置。

+

示例:

+
输入: [2,3,1,1,4]
输出:
2
解释:
跳到最后一个位置的最小跳跃数是 2。从下标为 0 跳到下标为 1 的位置,跳 1 步,然后跳 3 步到达数组的最后一个位置。
+

说明:

+

假设你总是可以到达数组的最后一个位置。

+ +## template + +```cpp +#include +using namespace std; +class Solution +{ +public: + int jump(vector &nums) + { + int steps = 0; + int lo = 0, hi = 0; + while (hi < nums.size() - 1) + { + int right = 0; + for (int i = lo; i <= hi; i++) + { + right = max(i + nums[i], right); + } + lo = hi + 1; + hi = right; + steps++; + } + return steps; + } +}; +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/65.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/65.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..9b4a3ad14f9a92a3b6ada32399a03126cc8db92c --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/65.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-7956f8daa342400bba061dafb6fd3178", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/65.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/65.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..d24c3814a6c71840037ebf79c9f8119d3e68861b --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/65.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "b167b3dbf557410caad7dc7c9dc7adc2" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/65.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/65.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..8a2e15e425c912b4dc0338212dbcf0051cb353f1 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/65.exercises/solution.md" @@ -0,0 +1,90 @@ +# 反转链表 II + +给你单链表的头指针 head 和两个整数 leftright ,其中 left <= right 。请你反转从位置 left 到位置 right 的链表节点,返回 反转后的链表

 

示例 1:

输入:head = [1,2,3,4,5], left = 2, right = 4
输出:
[1,4,3,2,5]

示例 2:

输入:head = [5], left = 1, right = 1
输出:
[5]

 

提示:

  • 链表中节点数目为 n
  • 1 <= n <= 500
  • -500 <= Node.val <= 500
  • 1 <= left <= right <= n

 

进阶: 你可以使用一趟扫描完成反转吗?

+ +## template + +```cpp +#include +#include +struct ListNode +{ + int val; + struct ListNode *next; +}; +static struct ListNode *reverseBetween(struct ListNode *head, int m, int n) +{ + int i; + struct ListNode dummy; + struct ListNode *prev = &dummy; + prev->next = head; + for (i = 1; i < m; i++) + { + prev = prev->next; + } + struct ListNode *p = prev->next; + for (i = m; i < n; i++) + { + struct ListNode *q = p->next; + p->next = q->next; + q->next = prev->next; + prev->next = q; + } + return dummy.next; +} +int main(int argc, char **argv) +{ + if (argc < 3) + { + fprintf(stderr, "Usage: ./test m n 1 2 3...\n"); + exit(-1); + } + int i, count = argc - 3; + struct ListNode dummy; + struct ListNode *prev = &dummy; + struct ListNode *p; + for (i = 0; i < count; i++) + { + p = malloc(sizeof(*p)); + p->val = atoi(argv[i + 3]); + p->next = NULL; + prev->next = p; + prev = p; + } + int m = atoi(argv[1]); + int n = atoi(argv[2]); + struct ListNode *head = reverseBetween(dummy.next, m, n); + for (p = head; p != NULL; p = p->next) + { + printf("%d ", p->val); + } + printf("\n"); + return 0; +} +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/66.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/66.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..2a4efd751253dd4f94bfd8d19ca111e8ec1e5131 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/66.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-c4a5876d768c49afa16411eea23221fc", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/66.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/66.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..6a11f7880d143239e1d32bdb7782d3ad88853cff --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/66.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "f4ad8e3fdc974d09ad0afb1c8775ae12" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/66.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/66.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..ba67794ae4bf01396d7bbb093839a5891e9bd4d1 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/66.exercises/solution.md" @@ -0,0 +1,90 @@ +# 最小路径和 + +

给定一个包含非负整数的 m x n 网格 grid ,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小。

说明:每次只能向下或者向右移动一步。

 

示例 1:

输入:grid = [[1,3,1],[1,5,1],[4,2,1]]
输出:
7
解释:
因为路径 1→3→1→1→1 的总和最小。

示例 2:

输入:grid = [[1,2,3],[4,5,6]]
输出:
12

 

提示:

  • m == grid.length
  • n == grid[i].length
  • 1 <= m, n <= 200
  • 0 <= grid[i][j] <= 100
+ +## template + +```cpp +#include +#include +#include +static inline int min(int a, int b) +{ + return a < b ? a : b; +} +int minPathSum(int **grid, int gridRowSize, int gridColSize) +{ + int i, j; + int **dp = malloc(gridRowSize * sizeof(int *)); + for (i = 0; i < gridRowSize; i++) + { + dp[i] = malloc(gridColSize * sizeof(int)); + } + dp[0][0] = grid[0][0]; + int sum = dp[0][0]; + for (i = 1; i < gridRowSize; i++) + { + sum += grid[i][0]; + dp[i][0] = sum; + } + sum = dp[0][0]; + for (i = 1; i < gridColSize; i++) + { + sum += grid[0][i]; + dp[0][i] = sum; + } + for (i = 1; i < gridRowSize; i++) + { + for (j = 1; j < gridColSize; j++) + { + dp[i][j] = grid[i][j] + min(dp[i - 1][j], dp[i][j - 1]); + } + } + return dp[gridRowSize - 1][gridColSize - 1]; +} +int main(int argc, char **argv) +{ + int i, j; + int row = argc - 1; + int col = strlen(argv[1]); + int **grid = malloc(row * sizeof(int *)); + for (i = 0; i < row; i++) + { + grid[i] = malloc(col * sizeof(int)); + for (j = 0; j < col; j++) + { + grid[i][j] = argv[i + 1][j] - '0'; + printf("%d ", grid[i][j]); + } + printf("\n"); + } + printf("%d\n", minPathSum(grid, row, col)); + return 0; +} +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/67.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/67.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..394c29bd51818ebad96c17020b3b003496934d98 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/67.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-f35d9558bb004e20afa1137186ed1dfe", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/67.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/67.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..2fe399b91794b077f6e45f3c18d9e70244fd8fe1 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/67.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "c2dcf7aedfce4c1f8cdc906c44f1af3e" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/67.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/67.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..d735d5ac0fc684cb18771393469146431823b544 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/67.exercises/solution.md" @@ -0,0 +1,61 @@ +# 删除链表的倒数第 N 个结点 + +

给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点。

进阶:你能尝试使用一趟扫描实现吗?

 

示例 1:

输入:head = [1,2,3,4,5], n = 2
输出:
[1,2,3,5]

示例 2:

输入:head = [1], n = 1
输出:
[]

示例 3:

输入:head = [1,2], n = 1
输出:
[1]

 

提示:

  • 链表中结点的数目为 sz
  • 1 <= sz <= 30
  • 0 <= Node.val <= 100
  • 1 <= n <= sz
+ +## template + +```cpp +struct ListNode +{ + int val; + ListNode *next; + ListNode() : val(0), next(nullptr) {} + ListNode(int x) : val(x), next(nullptr) {} + ListNode(int x, ListNode *next) : val(x), next(next) {} +}; +#include +class Solution +{ +public: + ListNode *removeNthFromEnd(ListNode *head, int n) + { + ListNode empty_node(0, head); + ListNode *p = &empty_node; + std::vector pv; + while (p != nullptr) + { + pv.push_back(p); + p = p->next; + } + p = pv[pv.size() - 1 - n]; + p->next = p->next->next; + return empty_node.next; + } +}; +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/68.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/68.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..59ac6bb4ebab779a797d469a6b0042bc31310032 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/68.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-afaa77de3c2349b8a4683b325bb8b53b", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/68.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/68.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..d79500a515ae4c4a1f78286cfe9c1572fc3d7332 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/68.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "503ce168784a47bea62ad753f60ff2ee" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/68.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/68.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..9af938c7a29904f0ef57bbb2ec80c86734f13d0d --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/68.exercises/solution.md" @@ -0,0 +1,87 @@ +# 组合总和 + +

给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。 +

+

candidates 中的数字可以无限制重复被选取。

+

说明:

+
    +
  • 所有数字(包括 target)都是正整数。
  • +
  • 解集不能包含重复的组合。 
  • +
+

示例 1:

+
输入:candidates = [2,3,6,7], target = 7,
输出:
[[7],[2,2,3]]
+

示例 2:

+
输入:candidates = [2,3,5], target = 8,
输出:
[[2,2,2,2],[2,3,3],[3,5]]
+

 

+

提示:

+
    +
  • 1 <= candidates.length <= 30
  • +
  • 1 <= candidates[i] <= 200
  • +
  • candidate 中的每个元素都是独一无二的。
  • +
  • 1 <= target <= 500
  • +
+ +## template + +```cpp +#include +using namespace std; +class Solution +{ +public: + vector> combinationSum(vector &candidates, int target) + { + vector> res; + dfs(candidates, 0, target, res); + return res; + } +private: + vector stack; + void dfs(vector &candidates, int start, int target, vector> &res) + { + if (target < 0) + { + return; + } + else if (target == 0) + { + res.push_back(stack); + } + else + { + for (int i = start; i < candidates.size(); i++) + { + stack.push_back(candidates[i]); + dfs(candidates, i, target - candidates[i], res); + stack.pop_back(); + } + } + } +}; +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/69.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/69.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..cd6723138f8cef7daa4d9b4728f9c9f95554c973 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/69.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-4fb12397542c48658eb7ae3481c5f498", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/69.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/69.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..96e52a650b2dcf549cdb9731c5fb0808815a37be --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/69.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "8b79337a58c845938a7db985119f132b" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/69.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/69.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..0f66468a177ba1f5411de6901db187dfa8163237 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/69.exercises/solution.md" @@ -0,0 +1,78 @@ +# 最长回文子串 + +

给你一个字符串 s,找到 s 中最长的回文子串。

 

示例 1:

输入:s = "babad"
输出:
"bab"
解释:
"aba" 同样是符合题意的答案。

示例 2:

输入:s = "cbbd"
输出:
"bb"

示例 3:

输入:s = "a"
输出:
"a"

示例 4:

输入:s = "ac"
输出:
"a"

 

提示:

  • 1 <= s.length <= 1000
  • s 仅由数字和英文字母(大写和/或小写)组成
+ +## template + +```cpp +class Solution +{ +public: + string longestPalindrome(string s) + { + int ti = 0, maxlen = 0, i, t; + for (i = 0; s[i]; i++) + { + t = 1; + while (t <= i && s[i + t]) + { + if (s[i + t] == s[i - t]) + t++; + else + break; + } + t--; + if (2 * t + 1 > maxlen) + { + ti = i - t; + maxlen = 2 * t + 1; + } + } + for (i = 0; s[i]; i++) + { + t = 1; + while (t <= i + 1 && s[i + t]) + { + if (s[i - t + 1] == s[i + t]) + t++; + else + break; + } + t--; + if (2 * t > maxlen) + { + ti = i - t + 1; + maxlen = 2 * t; + } + } + s[ti + maxlen] = 0; + return s.c_str() + ti; + } +}; +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/70.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/70.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..c953ef94f37e33b8c93de80ae92969b5e8824fe7 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/70.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-f2434aa992324fc5a9d0a7d6ed419462", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/70.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/70.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..b3df99b4f3964ceae729c78f406b0ca6811717ab --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/70.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "3d244e91c00246a481591cfe5ff62646" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/70.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/70.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..2592b5f1a7d0ca7305d8e8ed0977d197288752b6 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/70.exercises/solution.md" @@ -0,0 +1,120 @@ +# 搜索旋转排序数组 II + +

已知存在一个按非降序排列的整数数组 nums ,数组中的值不必互不相同。

+

在传递给函数之前,nums 在预先未知的某个下标 k0 <= k < nums.length)上进行了 旋转 + ,使数组变为 [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]](下标 从 0 + 开始 计数)。例如, [0,1,2,4,4,4,5,6,6,7] 在下标 5 处经旋转后可能变为 + [4,5,6,6,7,0,1,2,4,4] 。 +

+

给你 旋转后 的数组 nums 和一个整数 target ,请你编写一个函数来判断给定的目标值是否存在于数组中。如果 + nums 中存在这个目标值 target ,则返回 true ,否则返回 false 。 +

+

 

+

示例 1:

+
输入:nums = [2,5,6,0,0,1,2], target = 0
输出:
true
+

示例 2:

+
输入:nums = [2,5,6,0,0,1,2], target = 3
输出:
false
+

 

+

提示:

+
    +
  • 1 <= nums.length <= 5000
  • +
  • -104 <= nums[i] <= 104
  • +
  • 题目数据保证 nums 在预先未知的某个下标上进行了旋转
  • +
  • -104 <= target <= 104
  • +
+

 

+

进阶:

+
    +
  • 这是 搜索旋转排序数组 的延伸题目,本题中的 nums  + 可能包含重复元素。
  • +
  • 这会影响到程序的时间复杂度吗?会有怎样的影响,为什么?
  • +
+ +## template + +```cpp +#include +#include +#include +static bool search(int *nums, int numsSize, int target) +{ + int lo = 0; + int hi = numsSize - 1; + while (lo <= hi) + { + int mid = lo + (hi - lo) / 2; + if (nums[mid] == target) + { + return true; + } + if (nums[lo] == nums[mid] && nums[mid] == nums[hi]) + { + lo++; + hi--; + } + else if (nums[lo] <= nums[mid]) + { + if (nums[lo] <= target && target < nums[mid]) + { + hi = mid - 1; + } + else + { + lo = mid + 1; + } + } + else + { + if (nums[mid] < target && target <= nums[hi]) + { + lo = mid + 1; + } + else + { + hi = mid - 1; + } + } + } + return false; +} +int main(int argc, char **argv) +{ + int i; + int target = atoi(argv[1]); + int size = argc - 2; + int *nums = malloc(size * sizeof(int)); + for (i = 0; i < argc - 2; i++) + { + nums[i] = atoi(argv[i + 2]); + } + printf("%d\n", search(nums, size, target)); + return 0; +} +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/71.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/71.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..d57e7fdc10c67793eb450f75c855b648d094ee66 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/71.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-2d257036886e4620b688bea9851574e6", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/71.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/71.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..66fb51c20ead3f7242aa78a87f48cf2ae11a0042 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/71.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "315486ff73684c8db585ad3723fd34fe" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/71.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/71.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..15f9add11857f2f783e5195024566a86ab58987e --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/71.exercises/solution.md" @@ -0,0 +1,105 @@ +# 删除有序数组中的重复项 II + +

给你一个有序数组 nums ,请你 原地 删除重复出现的元素,使每个元素 最多出现两次 ,返回删除后数组的新长度。

+

不要使用额外的数组空间,你必须在 原地 修改输入数组 并在使用 O(1) 额外空间的条件下完成。

+

 

+

说明:

+

为什么返回数值是整数,但输出的答案是数组呢?

+

请注意,输入数组是以「引用」方式传递的,这意味着在函数里修改输入数组对于调用者是可见的。

+

你可以想象内部操作如下:

+
+    // nums 是以“引用”方式传递的。也就是说,不对实参做任何拷贝
+    int len = removeDuplicates(nums);// 在函数里修改输入数组对于调用者是可见的。
+    // 根据你的函数返回的长度, 它会打印出数组中 该长度范围内 的所有元素。
+    for (int i = 0; i < len; i++) {
+            print(nums[i]);
+    }
+

 

+

示例 1:

+
输入:nums = [1,1,1,2,2,3]
输出:
5, nums = [1,1,2,2,3]
解释:
函数应返回新长度 length = 5, 并且原数组的前五个元素被修改为 1, 1, 2, 2, 3 。 不需要考虑数组中超出新长度后面的元素。
+

示例 2:

+
输入:nums = [0,0,1,1,1,1,2,3,3]
输出:
7, nums = [0,0,1,1,2,3,3]
解释:
函数应返回新长度 length = 7, 并且原数组的前五个元素被修改为 0, 0, 1, 1, 2, 3, 3 。 不需要考虑数组中超出新长度后面的元素。
+

 

+

提示:

+
    +
  • 1 <= nums.length <= 3 * 104
  • +
  • -104 <= nums[i] <= 104
  • +
  • nums 已按升序排列
  • +
+ +## template + +```cpp +#include +#include +static int removeDuplicates(int *nums, int numsSize) +{ + if (numsSize == 0) + { + return 0; + } + int i; + int len = 0; + int count = 1; + for (i = 1; i < numsSize; i++) + { + if (nums[len] == nums[i]) + { + if (count < 2) + { + count++; + nums[++len] = nums[i]; + } + } + else + { + count = 1; + nums[++len] = nums[i]; + } + } + return len + 1; +} +int main(int argc, char **argv) +{ + int i, count = argc - 1; + int *nums = malloc(count * sizeof(int)); + for (i = 0; i < count; i++) + { + nums[i] = atoi(argv[i + 1]); + } + count = removeDuplicates(nums, count); + for (i = 0; i < count; i++) + { + printf("%d ", nums[i]); + } + printf("\n"); +} +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/72.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/72.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..6ba2edec06d658d36857053f9340f1502bd196c0 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/72.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-fb130081c8504ef2ac255643f4127920", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/72.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/72.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..07c0ec6fbe3e53f9d6a5ebbe68309b0079688ecf --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/72.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "459e74d4759348c99a03fef12c35a25d" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/72.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/72.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..4f3bfced528763e8b14e685730b52aea9ead3940 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/72.exercises/solution.md" @@ -0,0 +1,84 @@ +# 交错字符串 + +

给定三个字符串 s1s2s3,请你帮忙验证 s3 是否是由 s1 和 s2 交错 组成的。

两个字符串 st 交错 的定义与过程如下,其中每个字符串都会被分割成若干 非空 子字符串:

  • s = s1 + s2 + ... + sn
  • t = t1 + t2 + ... + tm
  • |n - m| <= 1
  • 交错s1 + t1 + s2 + t2 + s3 + t3 + ... 或者 t1 + s1 + t2 + s2 + t3 + s3 + ...

提示:a + b 意味着字符串 ab 连接。

 

示例 1:

输入:s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac"
输出:
true

示例 2:

输入:s1 = "aabcc", s2 = "dbbca", s3 = "aadbbbaccc"
输出:
false

示例 3:

输入:s1 = "", s2 = "", s3 = ""
输出:
true

 

提示:

  • 0 <= s1.length, s2.length <= 100
  • 0 <= s3.length <= 200
  • s1s2、和 s3 都由小写英文字母组成
+ +## template + +```cpp +#include +#include +#include +#include +static bool isInterleave(char *s1, char *s2, char *s3) +{ + int i, j; + int len1 = strlen(s1); + int len2 = strlen(s2); + int len3 = strlen(s3); + if (len1 + len2 != len3) + { + return false; + } + bool *table = malloc((len1 + 1) * (len2 + 1) * sizeof(bool)); + bool **dp = malloc((len1 + 1) * sizeof(bool *)); + for (i = 0; i < len1 + 1; i++) + { + dp[i] = &table[i * (len2 + 1)]; + } + dp[0][0] = true; + for (i = 1; i < len1 + 1; i++) + { + dp[i][0] = dp[i - 1][0] && s1[i - 1] == s3[i - 1]; + } + for (i = 1; i < len2 + 1; i++) + { + dp[0][i] = dp[0][i - 1] && s2[i - 1] == s3[i - 1]; + } + for (i = 1; i < len1 + 1; i++) + { + for (j = 1; j < len2 + 1; j++) + { + bool up = dp[i - 1][j] && s1[i - 1] == s3[i + j - 1]; + bool left = dp[i][j - 1] && s2[j - 1] == s3[i + j - 1]; + dp[i][j] = up || left; + } + } + return dp[len1][len2]; +} +int main(int argc, char **argv) +{ + if (argc != 4) + { + fprintf(stderr, "Usage: ./test s1 s2 s3\n"); + exit(-1); + } + printf("%s\n", isInterleave(argv[1], argv[2], argv[3]) ? "true" : "false"); + return 0; +} +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/73.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/73.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..95527f45d6f54832f4c4b1f388d127663b342918 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/73.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-a51ea8b65bae411ea5d0f4083610cc96", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/73.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/73.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..91a4b7bf07c948e41488d388e29ba8860593d06d --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/73.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "964658ce1dee48748e3ea84f1bfa1414" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/73.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/73.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..929a028712fbe47bbca812aef44a75cd48e2cc37 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/73.exercises/solution.md" @@ -0,0 +1,105 @@ +# 合并区间 + +

以数组 intervals 表示若干个区间的集合,其中单个区间为 intervals[i] = [starti, endi] 。请你合并所有重叠的区间,并返回一个不重叠的区间数组,该数组需恰好覆盖输入中的所有区间。

 

示例 1:

输入:intervals = [[1,3],[2,6],[8,10],[15,18]]
输出:
[[1,6],[8,10],[15,18]]
解释:
区间 [1,3] 和 [2,6] 重叠, 将它们合并为 [1,6].

示例 2:

输入:intervals = [[1,4],[4,5]]
输出:
[[1,5]]
解释:
区间 [1,4] 和 [4,5] 可被视为重叠区间。

 

提示:

  • 1 <= intervals.length <= 104
  • intervals[i].length == 2
  • 0 <= starti <= endi <= 104
+ +## template + +```cpp +#include +#include +#include +static int compare(const void *a, const void *b) +{ + return ((int *)a)[0] - ((int *)b)[0]; +} +int **merge(int **intervals, int intervalsSize, int *intervalsColSize, int *returnSize, int **returnColumnSizes) +{ + if (intervalsSize == 0) + { + *returnSize = 0; + return intervals; + } + int i, len = 0; + int *tmp = malloc(intervalsSize * 2 * sizeof(int)); + for (i = 0; i < intervalsSize; i++) + { + tmp[i * 2] = intervals[i][0]; + tmp[i * 2 + 1] = intervals[i][1]; + } + qsort(tmp, intervalsSize, 2 * sizeof(int), compare); + intervals[0][0] = tmp[0]; + intervals[0][1] = tmp[1]; + for (i = 1; i < intervalsSize; i++) + { + if (tmp[i * 2] > intervals[len][1]) + { + len++; + intervals[len][0] = tmp[i * 2]; + intervals[len][1] = tmp[i * 2 + 1]; + } + else if (tmp[i * 2 + 1] > intervals[len][1]) + { + intervals[len][1] = tmp[i * 2 + 1]; + } + } + len += 1; + *returnSize = len; + *returnColumnSizes = malloc(len * sizeof(int)); + for (i = 0; i < len; i++) + { + (*returnColumnSizes)[i] = 2; + } + return intervals; +} +int main(int argc, char **argv) +{ + if (argc < 1 || argc % 2 == 0) + { + fprintf(stderr, "Usage: ./test s0 e0 s1 e1..."); + exit(-1); + } + int i, count = 0; + int *sizes = malloc((argc - 1) / 2 * sizeof(int)); + int **intervals = malloc((argc - 1) / 2 * sizeof(int *)); + for (i = 0; i < (argc - 1) / 2; i++) + { + sizes[i] = 2; + intervals[i] = malloc(2 * sizeof(int)); + intervals[i][0] = atoi(argv[i * 2 + 1]); + intervals[i][1] = atoi(argv[i * 2 + 2]); + } + int *col_sizes; + int **results = merge(intervals, (argc - 1) / 2, sizes, &count, &col_sizes); + for (i = 0; i < count; i++) + { + printf("[%d,%d]\n", results[i][0], results[i][1]); + } + return 0; +} +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/74.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/74.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..c8f17734103d75402ef2fbe0dffa01cde8858c98 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/74.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-f0b3392045414af9802c10e6fadeb185", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/74.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/74.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..5269ee4fa50a706d7b912bac4372a8ce9125c427 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/74.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "a0be7844bb474524b9b2f6c8dead32a4" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/74.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/74.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..f4541f605de58510e3e86edb4b0fd5112480b6f5 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/74.exercises/solution.md" @@ -0,0 +1,91 @@ +# 三数之和 + +

给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有和为 0 且不重复的三元组。

注意:答案中不可以包含重复的三元组。

 

示例 1:

输入:nums = [-1,0,1,2,-1,-4]
输出:
[[-1,-1,2],[-1,0,1]]

示例 2:

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

示例 3:

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

 

提示:

  • 0 <= nums.length <= 3000
  • -105 <= nums[i] <= 105
+ +## template + +```cpp +#include +#include +class Solution +{ +public: + vector> threeSum(vector &nums) + { + vector> r; + if (nums.size() == 0) + return r; + sort(nums.begin(), nums.end()); + int cur, left, right; + cur = 0; + while (cur < nums.size()) + { + if (nums[cur] > 0) + break; + left = cur + 1; + right = nums.size() - 1; + while (left < right) + { + int n = nums[cur] + nums[left] + nums[right]; + if (n == 0) + { + r.emplace_back(vector({nums[cur], nums[left], nums[right]})); + int t = left + 1; + while (t < right && nums[t] == nums[left]) + t++; + left = t; + t = right - 1; + while (t > left && nums[t] == nums[right]) + t--; + right = t; + } + else if (n > 0) + { + int t = right - 1; + while (t > left && nums[t] == nums[right]) + t--; + right = t; + } + else + { + int t = left + 1; + while (t < right && nums[t] == nums[left]) + t++; + left = t; + } + } + int t = cur + 1; + while (t < nums.size() && nums[t] == nums[cur]) + t++; + cur = t; + } + return r; + } +}; +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/75.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/75.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..40955c082b60c841515601699498f46ad645b814 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/75.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-15e8eacd0cf64134895c9e304650f07f", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/75.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/75.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..cefd609d7b8447ae442ddad858741cf5d8ca4e89 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/75.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "b8e1d751af744d3ca85218cc8f0e45e6" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/75.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/75.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..75928db77eb0ccce00af83f44bbbcd82865db1ab --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/75.exercises/solution.md" @@ -0,0 +1,64 @@ +# 字符串相乘 + +

给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式。

示例 1:

输入: num1 = "2", num2 = "3"
输出:
"6"

示例 2:

输入: num1 = "123", num2 = "456"
输出:
"56088"

说明:

  1. num1 和 num2 的长度小于110。
  2. num1 和 num2 只包含数字 0-9
  3. num1 和 num2 均不以零开头,除非是数字 0 本身。
  4. 不能使用任何标准库的大数类型(比如 BigInteger)直接将输入转换为整数来处理
+ +## template + +```cpp +#include +using namespace std; +class Solution +{ +public: + string multiply(string num1, string num2) + { + string res(num1.length() + num2.length(), '0'); + for (int i = num2.length() - 1; i >= 0; i--) + { + int j, carry = 0; + for (j = num1.length() - 1; j >= 0; j--) + { + carry += (num1[j] - '0') * (num2[i] - '0') + (res[i + j + 1] - '0'); + res[i + j + 1] = carry % 10 + '0'; + carry /= 10; + } + res[i + j + 1] = carry + '0'; + } + int i; + for (i = 0; i < res.length() - 1; i++) + { + if (res[i] != '0') + { + break; + } + } + return res.substr(i); + } +}; +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/76.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/76.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..b66897a8fd9a70448d847f6c53230bd1ec2125bc --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/76.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-fa325972af1746b4a703e0ee3bb55329", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/76.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/76.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..6f52e76b2f82a9aa7081389e39c547a1e9916c21 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/76.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "d36efbcaa793497eb9cef338ca029367" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/76.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/76.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..1318e8d0662b06627bd0990ad4e7460ddfce612d --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/76.exercises/solution.md" @@ -0,0 +1,83 @@ +# 最接近的三数之和 + +

给定一个包括 n 个整数的数组 nums 和 一个目标值 target。找出 nums 中的三个整数,使得它们的和与 target 最接近。返回这三个数的和。假定每组输入只存在唯一答案。

 

示例:

输入:nums = [-1,2,1,-4], target = 1
输出:
2
解释:
与 target 最接近的和是 2 (-1 + 2 + 1 = 2) 。

 

提示:

  • 3 <= nums.length <= 10^3
  • -10^3 <= nums[i] <= 10^3
  • -10^4 <= target <= 10^4
+ +## template + +```cpp +#include +class Solution +{ +public: + int threeSumClosest(vector &nums, int target) + { + sort(nums.begin(), nums.end()); + int cur, left, right; + cur = 0; + int closest = nums[0] + nums[1] + nums[2]; + while (cur < nums.size() - 2) + { + left = cur + 1; + right = nums.size() - 1; + int n; + while (left < right) + { + n = nums[cur] + nums[left] + nums[right]; + if (abs(target - n) < abs(target - closest)) + { + closest = n; + } + if (n == target) + { + break; + } + else if (n > target) + { + int t = right - 1; + while (t > left && nums[t] == nums[right]) + t--; + right = t; + } + else + { + int t = left + 1; + while (t < right && nums[t] == nums[left]) + t++; + left = t; + } + } + int t = cur + 1; + while (t < nums.size() && nums[t] == nums[cur]) + t++; + cur = t; + } + return closest; + } +}; +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/77.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/77.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..04d5b35c8086dcc542ec2d1460dd993e373ce64b --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/77.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-f6fc759a81fe4b0abc4892c6c6b5e345", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/77.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/77.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..3212360f104860f521cbfb7bda357e7d96e0fc43 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/77.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "5ad6f96648d0498d89d8d596e51d60cb" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/77.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/77.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..55bdbb4e687efa6f05c0a48aec58bb7545552f73 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/77.exercises/solution.md" @@ -0,0 +1,65 @@ +# 跳跃游戏 + +

给定一个非负整数数组 nums ,你最初位于数组的 第一个下标

数组中的每个元素代表你在该位置可以跳跃的最大长度。

判断你是否能够到达最后一个下标。

 

示例 1:

输入:nums = [2,3,1,1,4]
输出:
true
解释:
可以先跳 1 步,从下标 0 到达下标 1, 然后再从下标 1 跳 3 步到达最后一个下标。

示例 2:

输入:nums = [3,2,1,0,4]
输出:
false
解释:
无论怎样,总会到达下标为 3 的位置。但该下标的最大跳跃长度是 0 , 所以永远不可能到达最后一个下标。

 

提示:

  • 1 <= nums.length <= 3 * 104
  • 0 <= nums[i] <= 105
+ +## template + +```cpp +#include +#include +#include +static inline int max(int a, int b) +{ + return a > b ? a : b; +} +static bool canJump(int *nums, int numsSize) +{ + int i, pos = 0; + for (i = 0; i < numsSize - 1; i++) + { + if (pos < i || pos >= numsSize - 1) + { + break; + } + pos = max(i + nums[i], pos); + } + return pos >= numsSize - 1; +} +int main(int argc, char **argv) +{ + int i, count = argc - 1; + int *nums = malloc(count * sizeof(int)); + for (i = 0; i < count; i++) + { + nums[i] = atoi(argv[i + 1]); + } + printf("%s\n", canJump(nums, count) ? "true" : "false"); + return 0; +} +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/78.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/78.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..d7eb04d2e36f020f3d32e02af89cd969c16e4def --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/78.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-54ba584225b140f083bfc38f750bff12", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/78.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/78.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..d3197a35d7b9479aa66947b95d112e9beb2d3d5b --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/78.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "c8233a1315554943a1914fc8de46baf1" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/78.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/78.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..21e6e71d91bd73428cf5aa96113a2b32067970a9 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/78.exercises/solution.md" @@ -0,0 +1,102 @@ +# 单词搜索 + +

给定一个 m x n 二维字符网格 board 和一个字符串单词 word 。如果 word 存在于网格中,返回 true ;否则,返回 false

单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。

 

示例 1:

输入:board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"
输出:
true

示例 2:

输入:board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "SEE"
输出:
true

示例 3:

输入:board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCB"
输出:
false

 

提示:

  • m == board.length
  • n = board[i].length
  • 1 <= m, n <= 6
  • 1 <= word.length <= 15
  • boardword 仅由大小写英文字母组成

 

进阶:你可以使用搜索剪枝的技术来优化解决方案,使其在 board 更大的情况下可以更快解决问题?

+ +## template + +```cpp +#include +#include +#include +#include +static bool dfs(char *word, char **board, bool *used, + int row, int col, int row_size, int col_size) +{ + if (board[row][col] != *word) + { + return false; + } + used[row * col_size + col] = true; + if (*(word + 1) == '\0') + { + return true; + } + bool result = false; + if (row > 0 && !used[(row - 1) * col_size + col]) + { + result = dfs(word + 1, board, used, row - 1, col, row_size, col_size); + } + if (!result && row < row_size - 1 && !used[(row + 1) * col_size + col]) + { + result = dfs(word + 1, board, used, row + 1, col, row_size, col_size); + } + if (!result && col > 0 && !used[row * col_size + col - 1]) + { + result = dfs(word + 1, board, used, row, col - 1, row_size, col_size); + } + if (!result && col < col_size - 1 && !used[row * col_size + col + 1]) + { + result = dfs(word + 1, board, used, row, col + 1, row_size, col_size); + } + used[row * col_size + col] = false; + return result; +} +static bool exist(char **board, int boardRowSize, int boardColSize, char *word) +{ + int i, j; + int len = strlen(word); + if (len > boardRowSize * boardColSize) + { + return false; + } + bool *used = malloc(boardRowSize * boardColSize); + for (i = 0; i < boardRowSize; i++) + { + for (j = 0; j < boardColSize; j++) + { + memset(used, false, boardRowSize * boardColSize); + if (dfs(word, board, used, i, j, boardRowSize, boardColSize)) + { + return true; + } + } + } + return false; +} +int main(int argc, char **argv) +{ + if (argc < 3) + { + fprintf(stderr, "Usage: ./test word row1 row2...\n"); + exit(-1); + } + printf("%s\n", exist(argv + 2, argc - 2, strlen(argv[2]), argv[1]) ? "true" : "false"); + return 0; +} +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/79.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/79.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..a503b136e764f8ad4325019762b2ea8acb397bc1 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/79.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-c1db08ac05da4c63bf72f736426191b9", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/79.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/79.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..a9a33ce32c13eb36b969eccfa0af26b7424e60de --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/79.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "bab18cbedf4149a391c23d4a8c90bf9b" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/79.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/79.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..9633687c6b3912afec1c7c833f91967db95c6fda --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/79.exercises/solution.md" @@ -0,0 +1,110 @@ +# 验证二叉搜索树 + +
+

给你一个二叉树的根节点 root ,判断其是否是一个有效的二叉搜索树。

+ +

有效 二叉搜索树定义如下:

+ +
    +
  • 节点的左子树只包含 小于 当前节点的数。
  • +
  • 节点的右子树只包含 大于 当前节点的数。
  • +
  • 所有左子树和右子树自身必须也是二叉搜索树。
  • +
+ +

 

+ +

示例 1:

+ +
输入:root = [2,1,3]
+输出:true
+
+ +

示例 2:

+ +
输入:root = [5,1,4,null,null,3,6]
+输出:false
+解释:根节点的值是 5 ,但是右子节点的值是 4 。
+
+ +

 

+ +

提示:

+ +
    +
  • 树中节点数目范围在[1, 104]
  • +
  • -231 <= Node.val <= 231 - 1
  • +
+
+ +## template + +```cpp +#include +using namespace std; +struct TreeNode +{ + int val; + TreeNode *left; + TreeNode *right; + TreeNode() : val(0), left(nullptr), right(nullptr) {} + TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} +}; +class Solution +{ +public: + bool isValidBST(TreeNode *root) + { + stack stk; + int prev = INT_MIN; + bool first = true; + while (!stk.empty() || root != nullptr) + { + if (root != nullptr) + { + stk.push(root); + root = root->left; + } + else + { + root = stk.top(); + stk.pop(); + if (!first && prev >= root->val) + { + return false; + } + first = false; + prev = root->val; + root = root->right; + } + } + return true; + } +}; +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/80.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/80.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..b500e91bdc09a61294a832473746da9238827391 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/80.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-71001855c5de4a1c8e24a3674d0c7cd1", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/80.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/80.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..8fbbbbab8b0de72bb843f64b537c8ee119031762 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/80.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "c103b9a08fd04892a705a2dacb0291ad" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/80.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/80.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..83ca23ad8eea82eee2b2d8c9c30d8c6d0af563c4 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/80.exercises/solution.md" @@ -0,0 +1,97 @@ +# 删除排序链表中的重复元素 II + +

存在一个按升序排列的链表,给你这个链表的头节点 head ,请你删除链表中所有存在数字重复情况的节点,只保留原始链表中 没有重复出现 的数字。

返回同样按升序排列的结果链表。

 

示例 1:

输入:head = [1,2,3,3,4,4,5]
输出:
[1,2,5]

示例 2:

输入:head = [1,1,1,2,3]
输出:
[2,3]

 

提示:

  • 链表中节点数目在范围 [0, 300]
  • -100 <= Node.val <= 100
  • 题目数据保证链表已经按升序排列
+ +## template + +```cpp +#include +#include +struct ListNode +{ + int val; + struct ListNode *next; +}; +struct ListNode *deleteDuplicates(struct ListNode *head) +{ + struct ListNode dummy; + struct ListNode *p, *q, *prev; + prev = &dummy; + dummy.next = head; + p = q = head; + while (p != NULL) + { + while (q != NULL && q->val == p->val) + { + q = q->next; + } + if (p->next == q) + { + prev = p; + } + else + { + prev->next = q; + } + p = q; + } + return dummy.next; +} +int main(int argc, char **argv) +{ + int i; + struct ListNode *head = NULL; + struct ListNode *prev = NULL; + struct ListNode *p; + for (i = 0; i < argc - 1; i++) + { + p = malloc(sizeof(*p)); + p->val = atoi(argv[i + 1]); + p->next = NULL; + if (head == NULL) + { + head = p; + prev = head; + } + else + { + prev->next = p; + prev = p; + } + } + p = deleteDuplicates(head); + while (p != NULL) + { + printf("%d ", p->val); + p = p->next; + } + printf("\n"); + return 0; +} +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/81.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/81.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..302e4bdaa2711c6a4c437730f224a51083ee3088 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/81.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-a985201eb15543b2963a17e2d14a7f3d", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/81.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/81.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..ad69c873885541019c1bf98dac8688e57995d69a --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/81.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "940fcda088b8488b999d47f7dfbe097f" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/81.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/81.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..107f12bf9d03cc4261a569f41052c6bcb7575288 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/81.exercises/solution.md" @@ -0,0 +1,127 @@ +# Z 字形变换 + +
+

将一个给定字符串 s 根据给定的行数 numRows ,以从上往下、从左到右进行 Z 字形排列。

+ +

比如输入字符串为 "PAYPALISHIRING" 行数为 3 时,排列如下:

+ +
+    P   A   H   N
+    A P L S I I G
+    Y   I   R
+ +

之后,你的输出需要从左往右逐行读取,产生出一个新的字符串,比如:"PAHNAPLSIIGYIR"

+ +

请你实现这个将字符串进行指定行数变换的函数:

+ +
string convert(string s, int numRows);
+ +

 

+ +

示例 1:

+ +
输入:s = "PAYPALISHIRING", numRows = 3
+输出:"PAHNAPLSIIGYIR"
+
+ 示例 2: + +
输入:s = "PAYPALISHIRING", numRows = 4
+输出:"PINALSIGYAHRPI"
+解释:
+P     I    N
+A   L S  I G
+Y A   H R
+P     I
+
+ +

示例 3:

+ +
输入:s = "A", numRows = 1
+输出:"A"
+
+ +

 

+ +

提示:

+ +
    +
  • 1 <= s.length <= 1000
  • +
  • s 由英文字母(小写和大写)、',''.' 组成
  • +
  • 1 <= numRows <= 1000
  • +
+
+ +## template + +```cpp +class Solution +{ +public: + string convert(string s, int numRows) + { + if (numRows == 1) + return s; + int len = s.size(); + if (len <= numRows) + return s; + int cycle_len = 2 * numRows - 2; + int full_cycles = len / cycle_len; + int left = len % cycle_len; + string r; + int i; + for (i = 0; i < full_cycles; ++i) + { + r += s[i * cycle_len]; + } + if (left) + r += s[i * cycle_len]; + for (i = 0; i < numRows - 2; ++i) + { + int j; + for (j = 0; j < full_cycles; ++j) + { + r += s[j * cycle_len + i + 1]; + r += s[j * cycle_len + i + 1 + cycle_len - 2 * (i + 1)]; + } + if (left) + { + if (j * cycle_len + i + 1 < len) + r += s[j * cycle_len + i + 1]; + if (j * cycle_len + i + 1 + cycle_len - 2 * (i + 1) < len) + r += s[j * cycle_len + i + 1 + cycle_len - 2 * (i + 1)]; + } + } + for (i = 0; i < full_cycles; ++i) + r += s[i * cycle_len + numRows - 1]; + if (left >= numRows) + r += s[i * cycle_len + numRows - 1]; + return r; + } +}; +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/82.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/82.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..7c2f54c914cbb3901ec3e8a56f5a5a374c498a8e --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/82.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-04d2324956b84c9199352f1bac579a26", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/82.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/82.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..082fe4b9785ee3c842ecaa6c5c12587a7db1035e --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/82.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "d2fcf1efd994448ebe75680222e0a5e2" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/82.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/82.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..8bdc4fbe7c0bc56fdc98861356995c2ae242cc92 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/82.exercises/solution.md" @@ -0,0 +1,85 @@ +# 组合总和 II + +

给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。 +

+

candidates 中的每个数字在每个组合中只能使用一次。

+

说明:

+
    +
  • 所有数字(包括目标数)都是正整数。
  • +
  • 解集不能包含重复的组合。 
  • +
+

示例 1:

+
输入: candidates = [10,1,2,7,6,1,5], target = 8,
所求解集为:
[[1, 7],[1, 2, 5],[2, 6],[1, 1, 6]]
+

示例 2:

+
输入: candidates = [2,5,2,1,2], target = 5,
所求解集为:
[[1,2,2],[5]]
+ +## template + +```cpp +#include +using namespace std; +class Solution +{ +public: + vector> combinationSum2(vector &candidates, int target) + { + vector> res; + sort(candidates.begin(), candidates.end()); + dfs(candidates, 0, target, res); + return res; + } +private: + vector stack; + void dfs(vector &candidates, int start, int target, vector> &res) + { + if (target < 0) + { + return; + } + else if (target == 0) + { + res.push_back(stack); + } + else + { + int last = INT_MIN; + for (int i = start; i < candidates.size(); i++) + { + if (last != candidates[i]) + { + stack.push_back(candidates[i]); + dfs(candidates, i + 1, target - candidates[i], res); + stack.pop_back(); + } + last = candidates[i]; + } + } + } +}; +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/83.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/83.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..d5081b6e678483213833c07bf962b15bee304d3b --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/83.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-cf85f408907543ee806a3d2332a07e72", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/83.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/83.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..20e49f51527447522105c784cff80f683e2f1a20 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/83.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "2cea3fbdd79e47f3a2696f1be97e1414" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/83.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/83.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..a0531011cf8ed6fb759311d0b24f8a0cafa3160c --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/83.exercises/solution.md" @@ -0,0 +1,87 @@ +# 螺旋矩阵 II + +

给你一个正整数 n ,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的 n x n 正方形矩阵 matrix

 

示例 1:

输入:n = 3
输出:
[[1,2,3],[8,9,4],[7,6,5]]

示例 2:

输入:n = 1
输出:
[[1]]

 

提示:

  • 1 <= n <= 20
+ +## template + +```cpp +#include +using namespace std; +class Solution +{ +public: + vector> generateMatrix(int n) + { + vector> matrix(n, vector(n)); + int direction = 0; + int hor_top = 0; + int hor_bottom = n - 1; + int ver_left = 0; + int ver_right = n - 1; + int num = 0; + while (num < n * n) + { + switch (direction) + { + case 0: + for (int i = ver_left; i <= ver_right; i++) + { + matrix[hor_top][i] = ++num; + } + hor_top++; + break; + case 1: + for (int i = hor_top; i <= hor_bottom; i++) + { + matrix[i][ver_right] = ++num; + } + ver_right--; + break; + case 2: + for (int i = ver_right; i >= ver_left; i--) + { + matrix[hor_bottom][i] = ++num; + } + hor_bottom--; + break; + case 3: + for (int i = hor_bottom; i >= hor_top; i--) + { + matrix[i][ver_left] = ++num; + } + ver_left++; + break; + } + direction++; + direction %= 4; + } + return matrix; + } +}; +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/84.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/84.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..824ae17a02ee95eb2bfa8d1f2f0c043ffafb9e3d --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/84.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-f0add5b941524853ba8bce1d170cdd3a", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/84.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/84.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..3d725971ce04adf8d16188c892446336dccde0cc --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/84.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "82543cb9e99d4953a0ea8531316a2f27" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/84.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/84.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..3d6fb609817654ef9efd982ad15e1904d4a7c36a --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/84.exercises/solution.md" @@ -0,0 +1,63 @@ +# 两两交换链表中的节点 + +

给定一个链表,两两交换其中相邻的节点,并返回交换后的链表。

你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。

 

示例 1:

输入:head = [1,2,3,4]
输出:
[2,1,4,3]

示例 2:

输入:head = []
输出:
[]

示例 3:

输入:head = [1]
输出:
[1]

 

提示:

  • 链表中节点的数目在范围 [0, 100]
  • 0 <= Node.val <= 100

 

进阶:你能在不修改链表节点值的情况下解决这个问题吗?(也就是说,仅修改节点本身。)

+ +## template + +```cpp +#include +using namespace std; +struct ListNode +{ + int val; + ListNode *next; + ListNode() : val(0), next(nullptr) {} + ListNode(int x) : val(x), next(nullptr) {} + ListNode(int x, ListNode *next) : val(x), next(next) {} +}; +class Solution +{ +public: + ListNode *swapPairs(ListNode *head) + { + struct ListNode dummy, *prev = &dummy, *p = head; + dummy.next = head; + while (p != nullptr && p->next != nullptr) + { + struct ListNode *q = p->next; + p->next = q->next; + q->next = prev->next; + prev->next = q; + prev = p; + p = p->next; + } + return dummy.next; + } +}; +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/85.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/85.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..11c8f56e14d4c6b8b890d6881ee3baac9bd4801a --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/85.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-1a32ddc1ddca400c8b063f6d19fb8907", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/85.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/85.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..a1c73683425ee715b81a91affa5a39cf174bff3b --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/85.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "296fd8f82ac4470d8c077f261b73e0bf" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/85.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/85.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..973dd513499f60d742f66e32d6e1dcbc9587d8d8 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/85.exercises/solution.md" @@ -0,0 +1,73 @@ +# 颜色分类 + +

给定一个包含红色、白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色、白色、蓝色顺序排列。

此题中,我们使用整数 0、 12 分别表示红色、白色和蓝色。

     

    示例 1:

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

    示例 2:

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

    示例 3:

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

    示例 4:

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

     

    提示:

    • n == nums.length
    • 1 <= n <= 300
    • nums[i]012

     

    进阶:

    • 你可以不使用代码库中的排序函数来解决这道题吗?
    • 你能想出一个仅使用常数空间的一趟扫描算法吗?
    + +## template + +```cpp +#include +using namespace std; +class Solution +{ +public: + void sortColors(vector &nums) + { + int i = 0, j = nums.size() - 1; + while (i < j) + { + if (nums[i] == 0) + { + i++; + continue; + } + if (nums[j] != 0) + { + j--; + continue; + } + swap(nums[i], nums[j]); + } + j = nums.size() - 1; + while (i < j) + { + if (nums[i] == 1) + { + i++; + continue; + } + if (nums[j] != 1) + { + j--; + continue; + } + swap(nums[i], nums[j]); + } + } +}; +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/86.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/86.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..74c44ac62f64693d76d833ca908938f712c1b136 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/86.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-9b3d74b35c564dfaab8603a7b2110f65", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/86.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/86.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..849d1810408a28b8f49755ba93fdbb6da9b86953 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/86.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "e8c5ac6556174e368ff9752403716ada" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/86.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/86.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..f8c5b94b788f57cb162673b75c192f8bdc80f498 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/86.exercises/solution.md" @@ -0,0 +1,138 @@ +# 不同路径 II + +

    一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” )。

    +

    机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为“Finish”)。

    +

    现在考虑网格中有障碍物。那么从左上角到右下角将会有多少条不同的路径?

    +

    +

    网格中的障碍物和空位置分别用 10 来表示。

    +

     

    +

    示例 1:

    +
    输入:obstacleGrid = [[0,0,0],[0,1,0],[0,0,0]]
    输出:
    2
    解释:
    3x3 网格的正中间有一个障碍物。从左上角到右下角一共有 2 条不同的路径:
    1. 向右 -> 向右 -> 向下 -> 向下
    2. 向下 -> 向下 -> 向右 -> 向右
    +

    示例 2:

    +
    输入:obstacleGrid = [[0,1],[0,0]]
    输出:
    1
    +

     

    +

    提示:

    +
      +
    • m == obstacleGrid.length
    • +
    • n == obstacleGrid[i].length
    • +
    • 1 <= m, n <= 100
    • +
    • obstacleGrid[i][j]01
    • +
    + +## template + +```cpp +#include +#include +static int uniquePathsWithObstacles(int **obstacleGrid, int obstacleGridRowSize, int obstacleGridColSize) +{ + int row, col; + int reset = 0; + for (row = 0; row < obstacleGridRowSize; row++) + { + if (reset) + { + obstacleGrid[row][0] = 1; + } + else + { + if (obstacleGrid[row][0] == 1) + { + reset = 1; + } + } + } + reset = 0; + for (col = 0; col < obstacleGridColSize; col++) + { + if (reset) + { + obstacleGrid[0][col] = 1; + } + else + { + if (obstacleGrid[0][col] == 1) + { + reset = 1; + } + } + } + for (row = 0; row < obstacleGridRowSize; row++) + { + int *line = obstacleGrid[row]; + for (col = 0; col < obstacleGridColSize; col++) + { + line[col] ^= 1; + } + } + for (row = 1; row < obstacleGridRowSize; row++) + { + int *last_line = obstacleGrid[row - 1]; + int *line = obstacleGrid[row]; + for (col = 1; col < obstacleGridColSize; col++) + { + if (line[col] != 0) + { + line[col] = line[col - 1] + last_line[col]; + } + } + } + return obstacleGrid[obstacleGridRowSize - 1][obstacleGridColSize - 1]; +} +int main(int argc, char **argv) +{ + if (argc < 3) + { + fprintf(stderr, "Usage: ./test m n\n"); + exit(-1); + } + int i, j, k = 3; + int row_size = atoi(argv[1]); + int col_size = atoi(argv[2]); + int **grids = malloc(row_size * sizeof(int *)); + for (i = 0; i < row_size; i++) + { + grids[i] = malloc(col_size * sizeof(int)); + int *line = grids[i]; + for (j = 0; j < col_size; j++) + { + line[j] = atoi(argv[k++]); + printf("%d ", line[j]); + } + printf("\n"); + } + printf("%d\n", uniquePathsWithObstacles(grids, row_size, col_size)); + return 0; +} +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/87.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/87.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..53ce93e09f626b901809e4d1ff5a53d27a68927c --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/87.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-2241bbc6d900433d918667440b3f2dbf", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/87.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/87.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..0476a9143e7643fdbe04a8fe7ef710996e7c50ad --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/87.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "0d64ba7398c344fca20c0b90624f3680" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/1.cpp/87.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/87.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..8a05b893d62f821801d527d0cc4d731709e6732f --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/1.cpp/87.exercises/solution.md" @@ -0,0 +1,146 @@ +# 有效的数独 + +

    请你判断一个 9x9 的数独是否有效。只需要 根据以下规则 ,验证已经填入的数字是否有效即可。

    +
      +
    1. 数字 1-9 在每一行只能出现一次。
    2. +
    3. 数字 1-9 在每一列只能出现一次。
    4. +
    5. 数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。(请参考示例图)
    6. +
    +

    数独部分空格内已填入了数字,空白格用 '.' 表示。

    +

    注意:

    +
      +
    • 一个有效的数独(部分已被填充)不一定是可解的。
    • +
    • 只需要根据以上规则,验证已经填入的数字是否有效即可。
    • +
    +

     

    +

    示例 1:

    +
    输入:board = 
    +    [["5","3",".",".","7",".",".",".","."]
    +    ,["6",".",".","1","9","5",".",".","."]
    +    ,[".","9","8",".",".",".",".","6","."]
    +    ,["8",".",".",".","6",".",".",".","3"]
    +    ,["4",".",".","8",".","3",".",".","1"]
    +    ,["7",".",".",".","2",".",".",".","6"]
    +    ,[".","6",".",".",".",".","2","8","."]
    +    ,[".",".",".","4","1","9",".",".","5"]
    +    ,[".",".",".",".","8",".",".","7","9"]]
    +输出:true
    +
    +

    示例 2:

    +
    输入:board = 
    +    [["8","3",".",".","7",".",".",".","."]
    +    ,["6",".",".","1","9","5",".",".","."]
    +    ,[".","9","8",".",".",".",".","6","."]
    +    ,["8",".",".",".","6",".",".",".","3"]
    +    ,["4",".",".","8",".","3",".",".","1"]
    +    ,["7",".",".",".","2",".",".",".","6"]
    +    ,[".","6",".",".",".",".","2","8","."]
    +    ,[".",".",".","4","1","9",".",".","5"]
    +    ,[".",".",".",".","8",".",".","7","9"]]
    +输出:false
    +解释:除了第一行的第一个数字从 5 改为 8 以外,空格内其他数字均与 示例1 相同。 但由于位于左上角的 3x3 宫内有两个 8 存在, 因此这个数独是无效的。
    +

     

    +

    提示:

    +
      +
    • board.length == 9
    • +
    • board[i].length == 9
    • +
    • board[i][j] 是一位数字或者 '.'
    • +
    + +## template + +```cpp +#include +using namespace std; +class Solution +{ +public: + bool isValidSudoku(vector> &board) + { + for (int i = 0; i < board.size(); i++) + { + vector mark(10); + for (int j = 0; j < board.size(); j++) + { + if (!valid(board, mark, i, j)) + { + return false; + } + } + } + for (int j = 0; j < board.size(); j++) + { + vector mark(10); + for (int i = 0; i < board.size(); i++) + { + if (!valid(board, mark, i, j)) + { + return false; + } + } + } + for (int k = 0; k < board.size(); k++) + { + int sr = k / 3 * 3; + int sc = (k % 3) * 3; + vector mark(10); + for (int i = sr; i < sr + 3; i++) + { + for (int j = sc; j < sc + 3; j++) + { + if (!valid(board, mark, i, j)) + { + return false; + } + } + } + } + return true; + } +private: + bool valid(vector> &board, vector &mark, int i, int j) + { + if (board[i][j] != '.') + { + int index = board[i][j] - '0'; + if (mark[index]) + { + return false; + } + else + { + mark[index] = 1; + } + } + return true; + } +}; +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/10.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/10.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..b0a30bdb1f28f49552406bca639bfc0974e955ae --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/10.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-8c77fcb047c0495bafecdfcd1de6ff22", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/10.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/10.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..3fd9f7763dd13332fd3cb1e52f3a9959661abd43 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/10.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "fcf347d163044712ade2667f4edca699" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/10.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/10.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..9b117f71f3b16d30a97fa2f0b36e15bc57cffbf2 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/10.exercises/solution.md" @@ -0,0 +1,108 @@ +# 不同的二叉搜索树 II + +
    +

    给你一个整数 n ,请你生成并返回所有由 n 个节点组成且节点值从 1n 互不相同的不同 + 二叉搜索树 。可以按 任意顺序 返回答案。 +

    + +

     

    + +
    +
    +

    示例 1:

    + +
    输入:n = 3
    +输出:[[1,null,2,null,3],[1,null,3,2],[2,1,3],[3,1,null,null,2],[3,2,null,1]]
    +    
    + +

    示例 2:

    + +
    输入:n = 1
    +输出:[[1]]
    +    
    + +

     

    + +

    提示:

    + +
      +
    • 1 <= n <= 8
    • +
    +
    +
    +
    + +## template + +```java +class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode(int x) { + val = x; + } +} + +class Solution { + public List generateTrees(int n) { + + if (n == 0) + return new LinkedList(); + + return generate_trees(1, n); + } + + private LinkedList generate_trees(int start, int end) { + LinkedList all_trees = new LinkedList(); + + if (start > end) { + all_trees.add(null); + return all_trees; + } + + for (int i = start; i <= end; i++) { + LinkedList left_trees = generate_trees(start, i - 1); + LinkedList right_trees = generate_trees(i + 1, end); + + for (TreeNode l : left_trees) + for (TreeNode r : right_trees) { + TreeNode current_tree = new TreeNode(i); + current_tree.left = l; + current_tree.right = r; + all_trees.add(current_tree); + } + } + + return all_trees; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/11.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/11.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..c3c773f81ceae5311bc6377940fb5a990eed602e --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/11.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-0482619df2074139be316afe4f0ed9ca", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/11.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/11.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..7f37699476e924c51c59d18e22da3a513c0fd830 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/11.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "311aad527c97415ba8d40a8745e15e4e" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/11.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/11.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..0beb9679773ebd66ff6cc0a094f394504130b4bb --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/11.exercises/solution.md" @@ -0,0 +1,180 @@ +# 字符串转换整数 (atoi) + +
    +

    请你来实现一个 myAtoi(string s) 函数,使其能将字符串转换成一个 32 位有符号整数(类似 C/C++ 中的 atoi 函数)。

    + +

    函数 myAtoi(string s) 的算法如下:

    + +
      +
    • 读入字符串并丢弃无用的前导空格
    • +
    • 检查下一个字符(假设还未到字符末尾)为正还是负号,读取该字符(如果有)。 确定最终结果是负数还是正数。 如果两者都不存在,则假定结果为正。
    • +
    • 读入下一个字符,直到到达下一个非数字字符或到达输入的结尾。字符串的其余部分将被忽略。
    • +
    • 将前面步骤读入的这些数字转换为整数(即,"123" -> 123, "0032" -> 32)。如果没有读入数字,则整数为 0 。必要时更改符号(从步骤 2 开始)。
    • +
    • 如果整数数超过 32 位有符号整数范围 [−231,  231 − 1] + ,需要截断这个整数,使其保持在这个范围内。具体来说,小于 −231 的整数应该被固定为 −231 ,大于 + 231 − 1 的整数应该被固定为 231 − 1 。 +
    • +
    • 返回整数作为最终结果。
    • +
    + +

    注意:

    + +
      +
    • 本题中的空白字符只包括空格字符 ' '
    • +
    • 除前导空格或数字后的其余字符串外,请勿忽略 任何其他字符。
    • +
    + +

     

    + +

    示例 1:

    + +
    输入:s = "42"
    +输出:42
    +解释:加粗的字符串为已经读入的字符,插入符号是当前读取的字符。
    +第 1 步:"42"(当前没有读入字符,因为没有前导空格)
    +		 ^
    +第 2 步:"42"(当前没有读入字符,因为这里不存在 '-' 或者 '+')
    +		 ^
    +第 3 步:"42"(读入 "42")
    +		   ^
    +解析得到整数 42 。
    +由于 "42" 在范围 [-231, 231 - 1] 内,最终结果为 42 。
    + +

    示例 2:

    + +
    输入:s = "   -42"
    +输出:-42
    +解释:
    +第 1 步:"   -42"(读入前导空格,但忽视掉)
    +			^
    +第 2 步:"   -42"(读入 '-' 字符,所以结果应该是负数)
    +			 ^
    +第 3 步:"   -42"(读入 "42")
    +			   ^
    +解析得到整数 -42 。
    +由于 "-42" 在范围 [-231, 231 - 1] 内,最终结果为 -42 。
    +
    + +

    示例 3:

    + +
    输入:s = "4193 with words"
    +输出:4193
    +解释:
    +第 1 步:"4193 with words"(当前没有读入字符,因为没有前导空格)
    +		 ^
    +第 2 步:"4193 with words"(当前没有读入字符,因为这里不存在 '-' 或者 '+')
    +		 ^
    +第 3 步:"4193 with words"(读入 "4193";由于下一个字符不是一个数字,所以读入停止)
    +		     ^
    +解析得到整数 4193 。
    +由于 "4193" 在范围 [-231, 231 - 1] 内,最终结果为 4193 。
    +
    + +

    示例 4:

    + +
    输入:s = "words and 987"
    +输出:0
    +解释:
    +第 1 步:"words and 987"(当前没有读入字符,因为没有前导空格)
    +		 ^
    +第 2 步:"words and 987"(当前没有读入字符,因为这里不存在 '-' 或者 '+')
    +		 ^
    +第 3 步:"words and 987"(由于当前字符 'w' 不是一个数字,所以读入停止)
    +		 ^
    +解析得到整数 0 ,因为没有读入任何数字。
    +由于 0 在范围 [-231, 231 - 1] 内,最终结果为 0 。
    + +

    示例 5:

    + +
    输入:s = "-91283472332"
    +输出:-2147483648
    +解释:
    +第 1 步:"-91283472332"(当前没有读入字符,因为没有前导空格)
    +		 ^
    +第 2 步:"-91283472332"(读入 '-' 字符,所以结果应该是负数)
    +		  ^
    +第 3 步:"-91283472332"(读入 "91283472332")
    +		            ^
    +解析得到整数 -91283472332 。
    +由于 -91283472332 小于范围 [-231, 231 - 1] 的下界,最终结果被截断为 -231 = -2147483648 。
    + +

     

    + +

    提示:

    + +
      +
    • 0 <= s.length <= 200
    • +
    • s 由英文字母(大写和小写)、数字(0-9)、' ''+''-' 和 + '.' 组成 +
    • +
    +
    + +## template + +```java +class Solution { + public int myAtoi(String s) { + long y = 0; + int i = 0; + boolean w = false; + boolean sign = false; + int offset = 0; + char[] ints = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; + while (i < s.length()) { + char c = s.charAt(i); + boolean isSign = false; + if (w == false && c != ' ') { + w = true; + if (c == '-') { + sign = true; + isSign = true; + } + if (c == '+') { + isSign = true; + } + } + if (w && (!isSign)) { + int v = Arrays.binarySearch(ints, c); + if (v < 0) { + break; + } + y = y * 10 + v; + if (y > 0x7FFFFFFF) { + y = 0x7FFFFFFF; + offset = 1; + break; + } + } + i++; + } + return sign ? -(int) (y + offset) : (int) y; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/12.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/12.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..1eb72cc9a2aa354a2161595db8249907a792239d --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/12.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-c1616f4f6de24ca19506f39da751eb79", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/12.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/12.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..0174b03deabce5894029bc2455996039465edfde --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/12.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "56335d89d6794087ad0d7b30027eb874" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/12.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/12.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..474f9ab1e0781bfc3b8749d70ec93d9008656127 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/12.exercises/solution.md" @@ -0,0 +1,81 @@ +# 不同的二叉搜索树 + +
    +

    给你一个整数 n ,求恰由 n 个节点组成且节点值从 1n 互不相同的 二叉搜索树 + 有多少种?返回满足题意的二叉搜索树的种数。

    + +

     

    + +

    示例 1:

    + +
    输入:n = 3
    +输出:5
    +    
    + +

    示例 2:

    + +
    输入:n = 1
    +输出:1
    +    
    + +

     

    + +

    提示:

    + +
      +
    • 1 <= n <= 19
    • +
    +
    + +## template + +```java +class Solution { + public int numTrees(int n) { + if (n < 2) { + return 1; + } + + int[] count = new int[n + 1]; + count[0] = 1; + count[1] = 1; + for (int i = 2; i <= n; i++) { + int sum = 0; + for (int root = 1; root <= i; root++) { + sum = sum + count[root - 1] * count[i - root]; + } + count[i] = sum; + } + + return count[n]; + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/13.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/13.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..a6e4a2cb3cd71907c6884e81971d6dcd2c68c039 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/13.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-f47ab734296f40818eee8ab6a7115d17", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/13.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/13.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..2461e66fd91066447103e0ef478d43a91b96cbed --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/13.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "3c532b4fd52548d986773be462a7b38d" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/13.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/13.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..160f26cf88ad07fc360235c2019a8ffb6aa45f2d --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/13.exercises/solution.md" @@ -0,0 +1,98 @@ +# 插入区间 + +

    给你一个 无重叠的按照区间起始端点排序的区间列表。

    +

    在列表中插入一个新的区间,你需要确保列表中的区间仍然有序且不重叠(如果有必要的话,可以合并区间)。

    +

     

    +

    示例 1:

    +
    输入:intervals = [[1,3],[6,9]], newInterval = [2,5]
    输出:
    [[1,5],[6,9]]
    +

    示例 2:

    +
    输入:intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8]
    输出:
    [[1,2],[3,10],[12,16]]
    解释:
    这是因为新的区间 [4,8] 与 [3,5],[6,7],[8,10] 重叠。
    +

    示例 3:

    +
    输入:intervals = [], newInterval = [5,7]
    输出:
    [[5,7]]
    +

    示例 4:

    +
    输入:intervals = [[1,5]], newInterval = [2,3]
    输出:
    [[1,5]]
    +

    示例 5:

    +
    输入:intervals = [[1,5]], newInterval = [2,7]
    输出:
    [[1,7]]
    +

     

    +

    提示:

    +
      +
    • 0 <= intervals.length <= 104
    • +
    • intervals[i].length == 2
    • +
    • 0 <= intervals[i][0] <= intervals[i][1] <= 105
    • +
    • intervals 根据 intervals[i][0]升序 排列
    • +
    • newInterval.length == 2
    • +
    • 0 <= newInterval[0] <= newInterval[1] <= 105
    • +
    + +## template + +```java + +public class Interval { + int start; + int end; + + Interval() { + start = 0; + end = 0; + } + + Interval(int s, int e) { + start = s; + end = e; + } +} + +class Solution { + public int[][] insert(int[][] intervals, int[] newInterval) { + int[][] newIntervals = new int[intervals.length + 1][]; + System.arraycopy(intervals, 0, newIntervals, 0, intervals.length); + newIntervals[intervals.length] = newInterval; + + Arrays.sort(newIntervals, (a, b) -> a[0] - b[0]); + Stack stack = new Stack<>(); + for (int[] num : newIntervals) { + if (stack.isEmpty()) { + stack.push(num); + continue; + } + int[] arr = stack.peek(); + if (arr[1] >= num[0]) { + int[] combine = { arr[0], Math.max(arr[1], num[1]) }; + stack.pop(); + stack.push(combine); + } else { + stack.push(num); + } + } + return stack.toArray(new int[0][]); + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/14.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/14.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..225ff84db716babdac0af355580d757ab167b966 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/14.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-19da8c9496ca40f6adc9b8657a95f7f3", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/14.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/14.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..8e967ade16ebb1193d938ff828ffa2ce1ed9794c --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/14.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "6b126d016c65401599231bcb942ff2bd" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/14.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/14.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..29a7321339dbbd8a62ad16536e2e2a43e4b2d779 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/14.exercises/solution.md" @@ -0,0 +1,69 @@ +# 四数之和 + +

    给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满足条件且不重复的四元组。

    注意:答案中不可以包含重复的四元组。

     

    示例 1:

    输入:nums = [1,0,-1,0,-2,2], target = 0
    输出:
    [[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]

    示例 2:

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

     

    提示:

    • 0 <= nums.length <= 200
    • -109 <= nums[i] <= 109
    • -109 <= target <= 109
    + +## template + +```java +class Solution { + public List> fourSum(int[] nums, int target) { + long l_target = target; + Arrays.sort(nums); + List> results = new ArrayList<>(); + int N = nums.length; + for (int i = 0; i < N - 3; i++) { + if (i > 0 && nums[i] == nums[i - 1]) + continue; + for (int j = i + 1; j < N - 2; j++) { + if (j > i + 1 && nums[j] == nums[j - 1]) + continue; + for (int k = j + 1, l = N - 1; k < l; k++) { + if (k > j + 1 && nums[k] == nums[k - 1]) + continue; + while (k < l && (l_target - nums[i] - nums[j] - nums[k] - nums[l]) < 0) { + l--; + } + if (k >= l) { + break; + } + if ((target - nums[i] - nums[j] - nums[k] - nums[l]) == 0) { + List item = new ArrayList<>(); + item.add(nums[i]); + item.add(nums[j]); + item.add(nums[k]); + item.add(nums[l]); + results.add(item); + } + } + } + } + return results; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/15.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/15.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..4b9380f2eec652927a84b4b1d8ab2813a79d21b2 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/15.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-47e9de3a5ef1478883bf32125ea2bdf3", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/15.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/15.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..63ef68d84d886a81fb997f90e51f57032b60e709 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/15.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "72a749865e8f4cf8aeb9b9cf7e3d8ed3" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/15.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/15.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..4bb94f2ad1a08f64f5a46d5b44a5316928d3ae9e --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/15.exercises/solution.md" @@ -0,0 +1,121 @@ +# 外观数列 + +
    +

    给定一个正整数 n ,输出外观数列的第 n 项。

    + +

    「外观数列」是一个整数序列,从数字 1 开始,序列中的每一项都是对前一项的描述。

    + +

    你可以将其视作是由递归公式定义的数字字符串序列:

    + +
      +
    • countAndSay(1) = "1"
    • +
    • countAndSay(n) 是对 countAndSay(n-1) 的描述,然后转换成另一个数字字符串。
    • +
    + +

    前五项如下:

    + +
    +    1.     1
    +    2.     11
    +    3.     21
    +    4.     1211
    +    5.     111221
    +    第一项是数字 1 
    +    描述前一项,这个数是 1 即 “ 一 个 1 ”,记作 "11"
    +    描述前一项,这个数是 11 即 “ 二 个 1 ” ,记作 "21"
    +    描述前一项,这个数是 21 即 “ 一 个 2 + 一 个 1 ” ,记作 "1211"
    +    描述前一项,这个数是 1211 即 “ 一 个 1 + 一 个 2 + 二 个 1 ” ,记作 "111221"
    +    
    + +

    描述 一个数字字符串,首先要将字符串分割为 最小 数量的组,每个组都由连续的最多 相同字符 + 组成。然后对于每个组,先描述字符的数量,然后描述字符,形成一个描述组。要将描述转换为数字字符串,先将每组中的字符数量用数字替换,再将所有描述组连接起来。

    + +

    例如,数字字符串 "3322251" 的描述如下图:

    + +
      +
    + +

     

    + +

    示例 1:

    + +
    输入:n = 1
    +输出:"1"
    +解释:这是一个基本样例。
    +
    + +

    示例 2:

    + +
    输入:n = 4
    +输出:"1211"
    +解释:
    +countAndSay(1) = "1"
    +countAndSay(2) = 读 "1" = 一 个 1 = "11"
    +countAndSay(3) = 读 "11" = 二 个 1 = "21"
    +countAndSay(4) = 读 "21" = 一 个 2 + 一 个 1 = "12" + "11" = "1211"
    +
    + +

     

    + +

    提示:

    + +
      +
    • 1 <= n <= 30
    • +
    +
    + +## template + +```java +class Solution { + public String countAndSay(int n) { + String pre = "1"; + for (int i = 1; i < n; i++) { + StringBuilder temp = new StringBuilder(); + char c = pre.charAt(0); + int cnt = 1; + for (int j = 1; j < pre.length(); j++) { + char cc = pre.charAt(j); + if (c == cc) { + cnt++; + } else { + temp.append(cnt).append(c); + cnt = 1; + c = cc; + } + } + temp.append(cnt).append(c); + pre = temp.toString(); + } + return pre; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/16.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/16.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..fb4b176a8e7f47177cc9b9e7d8febfbd81918222 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/16.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-8ac83269dfdf4952a2f0b572020aeb07", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/16.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/16.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..7302ab06e86e7de992baf3924a3a8108a0e0df6d --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/16.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "c29252e6b1c047289cd15d9010638a41" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/16.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/16.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..3556c4f6a33305e8697f957d57e06e71c486534c --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/16.exercises/solution.md" @@ -0,0 +1,65 @@ +# 旋转链表 + +

    给你一个链表的头节点 head ,旋转链表,将链表每个节点向右移动 k 个位置。

     

    示例 1:

    输入:head = [1,2,3,4,5], k = 2
    输出:
    [4,5,1,2,3]

    示例 2:

    输入:head = [0,1,2], k = 4
    输出:
    [2,0,1]

     

    提示:

    • 链表中节点的数目在范围 [0, 500]
    • -100 <= Node.val <= 100
    • 0 <= k <= 2 * 109
    + +## template + +```java +public class ListNode { + int val; + ListNode next; + ListNode(int x) { + val = x; + } +} +class Solution { + public ListNode rotateRight(ListNode head, int k) { + if (head == null || k == 0) { + return head; + } + ListNode cursor = head; + ListNode tail = null; + int length = 1; + while (cursor.next != null) { + cursor = cursor.next; + length++; + } + int loop = length - (k % length); + tail = cursor; + cursor.next = head; + cursor = head; + for (int i = 0; i < loop; i++) { + cursor = cursor.next; + tail = tail.next; + } + tail.next = null; + return cursor; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/17.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/17.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..32b072506df81b0d5495f4834730fc9139c5a1ed --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/17.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-bd43cf6d844c442b8d5c09969b905166", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/17.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/17.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..69fe65989bf7144f5652af5258e2c13068ca72da --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/17.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "43bd22aebe9a43fb848b0e9ad2ee4777" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/17.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/17.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..2b13a7175af5e3e24588a2626e73457ff5923107 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/17.exercises/solution.md" @@ -0,0 +1,69 @@ +# 组合 + +

    给定两个整数 nk,返回 1 ... n 中所有可能的 k 个数的组合。

    +

    示例:

    +
    输入: n = 4, k = 2
    输出:
    [[2,4],[3,4],[2,3],[1,2],[1,3],[1,4],]
    + +## template + +```java + +import java.util.*; + +public class Solution77 { + List> output = new LinkedList<>(); + int n; + int k; + + public void traceback(int first, LinkedList current) { + if (current.size() == k) { + + output.add(new LinkedList(current)); + System.out.println(output); + return; + } + + for (int i = first; i <= n; i++) { + + current.add(i); + traceback(i + 1, current); + + current.removeLast(); + } + } + + public List> combine(int n, int k) { + this.n = n; + this.k = k; + traceback(1, new LinkedList<>()); + return output; + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/18.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/18.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..b3aaba8b5ccb32010ffae6402f1a48ea17dc32ce --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/18.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-923abd0becbb4abd97ebfff249082f46", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/18.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/18.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..d30500de4597b4607e319d6523c158030b648e24 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/18.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "7671e2aadf874141a65e29516c86dd1b" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/18.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/18.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..480444bd46984a1d1bdb7dbfc797fa7c62bf9241 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/18.exercises/solution.md" @@ -0,0 +1,66 @@ +# 括号生成 + +

    数字 n 代表生成括号的对数,请你设计一个函数,用于能够生成所有可能的并且 有效的 括号组合。

     

    示例 1:

    输入:n = 3
    输出:
    ["((()))","(()())","(())()","()(())","()()()"]

    示例 2:

    输入:n = 1
    输出:
    ["()"]

     

    提示:

    • 1 <= n <= 8
    + +## template + +```java +class Solution { + void gen(Stack p, int lc, int rc, List r, int n) { + if (lc > n) { + return; + } + if (lc == n && rc == n) { + StringBuilder sb = new StringBuilder(); + for (String l : p) { + sb.append(l); + } + r.add(sb.toString()); + } + p.push("("); + lc++; + gen(p, lc, rc, r, n); + p.pop(); + lc--; + if (lc > rc) { + p.push(")"); + rc++; + gen(p, lc, rc, r, n); + p.pop(); + rc--; + } + } + public List generateParenthesis(int n) { + List results = new ArrayList(); + Stack p = new Stack(); + gen(p, 0, 0, results, n); + return results; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/19.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/19.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..a18200df569d0cb99f8a98658a9909efb990af29 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/19.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-07bdd9acd4664d37b9308222744b0529", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/19.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/19.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..1b0e1719e529254122ba0dff575cb0f065a2e937 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/19.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "9a2194cdf66748fca6df95005cca6ea4" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/19.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/19.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..ede72f62101237f9cb976254a8560b71ae213641 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/19.exercises/solution.md" @@ -0,0 +1,51 @@ +# 旋转图像 + +

    给定一个 × n 的二维矩阵 matrix 表示一个图像。请你将图像顺时针旋转 90 度。

    你必须在 原地 旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要 使用另一个矩阵来旋转图像。

     

    示例 1:

    输入:matrix = [[1,2,3],[4,5,6],[7,8,9]]
    输出:
    [[7,4,1],[8,5,2],[9,6,3]]

    示例 2:

    输入:matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]
    输出:
    [[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]

    示例 3:

    输入:matrix = [[1]]
    输出:
    [[1]]

    示例 4:

    输入:matrix = [[1,2],[3,4]]
    输出:
    [[3,1],[4,2]]

     

    提示:

    • matrix.length == n
    • matrix[i].length == n
    • 1 <= n <= 20
    • -1000 <= matrix[i][j] <= 1000
    + +## template + +```java +class Solution { + + public void rotate(int[][] matrix) { + int len = matrix.length; + int le = len - 1; + for (int i = 0; i < len / 2; i++) { + for (int j = 0; j < (len + 1) / 2; j++) { + int temp = matrix[i][j]; + matrix[i][j] = matrix[le - j][i]; + matrix[le - j][i] = matrix[le - i][le - j]; + matrix[le - i][le - j] = matrix[j][le - i]; + matrix[j][le - i] = temp; + } + } + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/20.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/20.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..b70a69cda8185e63fa0e6572047147acc9732380 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/20.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-3987208af25c4ac0af4842650ea1e5e7", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/20.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/20.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..abe972cba19c5c4084e8e953e19ccfcb78c46fae --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/20.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "a050eb4a7ec1488ea0ba1fb19243df41" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/20.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/20.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..4cdb67df84c2a5dbc01ad48062547ddf119786ab --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/20.exercises/solution.md" @@ -0,0 +1,54 @@ +# 盛最多水的容器 + +

    给你 n 个非负整数 a1,a2,...,an每个数代表坐标中的一个点 (i, ai) 。在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai)(i, 0) 。找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。

    说明:你不能倾斜容器。

     

    示例 1:

    输入:[1,8,6,2,5,4,8,3,7]
    输出:
    49
    解释:
    图中垂直线代表输入数组 [1,8,6,2,5,4,8,3,7]。在此情况下,容器能够容纳水(表示为蓝色部分)的最大值为 49。

    示例 2:

    输入:height = [1,1]
    输出:
    1

    示例 3:

    输入:height = [4,3,2,1,4]
    输出:
    16

    示例 4:

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

     

    提示:

    • n = height.length
    • 2 <= n <= 3 * 104
    • 0 <= height[i] <= 3 * 104
    + +## template + +```java +class Solution { + public int maxArea(int[] height) { + int N = height.length; + int i = 0; + int j = N - 1; + int max = 0; + while (i < j) { + int c = (j - i) * Math.min(height[i], height[j]); + if (c > max) { + max = c; + } + if (height[i] > height[j]) { + j--; + } else { + i++; + } + } + return max; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/21.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/21.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..f5fdf28b20abb7636c33c07fa97d6b4ba5d1035a --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/21.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-ed76415d470d4929adc103d1aba326e9", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/21.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/21.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..6e5e38cff776239d162c19d407bda9cd54042c7d --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/21.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "51ec34e954684cf297c80c42be58e629" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/21.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/21.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..b4930739628686711034a6bf5ea1e6e486cbadf4 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/21.exercises/solution.md" @@ -0,0 +1,63 @@ +# 复原 IP 地址 + +

    给定一个只包含数字的字符串,用以表示一个 IP 地址,返回所有可能从 s 获得的 有效 IP 地址 。你可以按任何顺序返回答案。

    有效 IP 地址 正好由四个整数(每个整数位于 0 到 255 之间组成,且不能含有前导 0),整数之间用 '.' 分隔。

    例如:"0.1.2.201" 和 "192.168.1.1" 是 有效 IP 地址,但是 "0.011.255.245"、"192.168.1.312" 和 "192.168@1.1" 是 无效 IP 地址。

     

    示例 1:

    输入:s = "25525511135"
    输出:
    ["255.255.11.135","255.255.111.35"]

    示例 2:

    输入:s = "0000"
    输出:
    ["0.0.0.0"]

    示例 3:

    输入:s = "1111"
    输出:
    ["1.1.1.1"]

    示例 4:

    输入:s = "010010"
    输出:
    ["0.10.0.10","0.100.1.0"]

    示例 5:

    输入:s = "101023"
    输出:
    ["1.0.10.23","1.0.102.3","10.1.0.23","10.10.2.3","101.0.2.3"]

     

    提示:

    • 0 <= s.length <= 3000
    • s 仅由数字组成
    + +## template + +```java +class Solution { + private List res = new ArrayList<>(); + + public List restoreIpAddresses(String s) { + if (s.length() < 4) + return res; + backtrack(s, 0, new StringBuilder(), 0); + return res; + } + + private void backtrack(String s, int start, StringBuilder sb, int pointNumOfSb) { + if (pointNumOfSb > 4) + return; + if (start == s.length() && pointNumOfSb == 4) { + res.add(sb.toString().substring(1)); + return; + } + for (int i = start; i < s.length() && i - start < 3; i++) { + String x = s.substring(start, i + 1); + if (x.charAt(0) == '0' && x.length() > 1) + return; + if (Integer.parseInt(x) <= 255) { + sb.append("." + x); + backtrack(s, i + 1, sb, pointNumOfSb + 1); + sb.delete(sb.lastIndexOf("."), sb.length()); + } + } + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/22.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/22.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..883d0a1cc03dbd6a74278f6e930adc8c704e6cad --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/22.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-2f6152d9101e450dae9edf9c3fac7b05", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/22.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/22.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..79665f23a09b54b18ae09480d2eac8473562313b --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/22.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "264f3f94da4340078e5526e6eb073c43" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/22.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/22.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..472fa532a0be1d83735ec4ee4354d09a7e190acb --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/22.exercises/solution.md" @@ -0,0 +1,57 @@ +# 格雷编码 + +

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

    +

    给定一个代表编码总位数的非负整数 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]。
    + +## template + +```java +class Solution { + public List grayCode(int n) { + List res = new ArrayList<>(); + res.add(0); + int cur; + for (int i = 0; i < n; i++) { + int change = 1 << i; + cur = res.size() - 1; + while (cur >= 0) { + res.add(res.get(cur) ^ change); + cur--; + } + } + return res; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/23.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/23.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..818860c594217c92be05d41f273a9f1ec9dfd92a --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/23.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-82deb808258e4d5c9b6c6c6a04f0e0bd", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/23.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/23.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..31b3ab846a553966f0ac337495cd59119f7de065 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/23.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "a9f4b8b841324c9898703af2b3c9bd02" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/23.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/23.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..6874e85db140386bddf6f7b5f258ce0152cebc35 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/23.exercises/solution.md" @@ -0,0 +1,63 @@ +# 下一个排列 + +

    实现获取 下一个排列 的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列。

    如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列)。

    必须 原地 修改,只允许使用额外常数空间。

     

    示例 1:

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

    示例 2:

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

    示例 3:

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

    示例 4:

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

     

    提示:

    • 1 <= nums.length <= 100
    • 0 <= nums[i] <= 100
    + +## template + +```java +class Solution { + public void nextPermutation(int[] nums) { + int i = nums.length - 1; + while (i > 0 && nums[i] <= nums[i - 1]) { + i--; + } + if (i > 0) { + int j = nums.length - 1; + while (j >= 0 && nums[j] <= nums[i - 1]) { + j--; + } + swap(nums, i - 1, j); + } + reverse(nums, i); + } + private void reverse(int[] nums, int i) { + int j = nums.length - 1; + while (i < j) { + swap(nums, i, j); + i++; + j--; + } + } + private void swap(int[] nums, int i, int j) { + int temp = nums[i]; + nums[i] = nums[j]; + nums[j] = temp; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/24.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/24.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..3aaa2219b0bd7d337932cd2b4f089e54e2985c11 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/24.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-74b59e14dd384e97a60aa9a6adfee078", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/24.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/24.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..4db2d8a7662f79cea117301e883edd153ee1da9e --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/24.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "b3d45c955e6a403f9bcf0d10eeabef41" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/24.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/24.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..2c21e07af7fac9c23a839297f615f284a6fe7cd9 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/24.exercises/solution.md" @@ -0,0 +1,61 @@ +# 子集 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
    + +## template + +```java +class Solution { + public List> subsetsWithDup(int[] nums) { + List> retList = new ArrayList<>(); + retList.add(new ArrayList<>()); + if (nums == null || nums.length == 0) + return retList; + Arrays.sort(nums); + List tmp = new ArrayList<>(); + tmp.add(nums[0]); + retList.add(tmp); + if (nums.length == 1) + return retList; + int lastLen = 1; + for (int i = 1; i < nums.length; i++) { + int size = retList.size(); + if (nums[i] != nums[i - 1]) { + lastLen = size; + } + for (int j = size - lastLen; j < size; j++) { + List inner = new ArrayList(retList.get(j)); + inner.add(nums[i]); + retList.add(inner); + } + } + return retList; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/25.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/25.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..c444e20ebc5c0e04a4319dc07f040069dbac9e19 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/25.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-bae992c0de9a4fbca46142cd7b5ff9dd", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/25.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/25.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..6159a9c6f1fd15b7cde7ea22d09ed2c752424ad2 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/25.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "48a445f27544450cbd4383a1745bc5de" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/25.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/25.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..5a1dc5d8e6731990d71037ff05fdf570fabbfff2 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/25.exercises/solution.md" @@ -0,0 +1,72 @@ +# 简化路径 + +

    给你一个字符串 path ,表示指向某一文件或目录的 Unix 风格 绝对路径 (以 '/' 开头),请你将其转化为更加简洁的规范路径。

    在 Unix 风格的文件系统中,一个点(.)表示当前目录本身;此外,两个点 (..) 表示将目录切换到上一级(指向父目录);两者都可以是复杂相对路径的组成部分。任意多个连续的斜杠(即,'//')都被视为单个斜杠 '/' 。 对于此问题,任何其他格式的点(例如,'...')均被视为文件/目录名称。

    请注意,返回的 规范路径 必须遵循下述格式:

    • 始终以斜杠 '/' 开头。
    • 两个目录名之间必须只有一个斜杠 '/'
    • 最后一个目录名(如果存在)不能 '/' 结尾。
    • 此外,路径仅包含从根目录到目标文件或目录的路径上的目录(即,不含 '.''..')。

    返回简化后得到的 规范路径

     

    示例 1:

    输入:path = "/home/"
    输出:
    "/home"
    解释:
    注意,最后一个目录名后面没有斜杠。

    示例 2:

    输入:path = "/../"
    输出:
    "/"
    解释:
    从根目录向上一级是不可行的,因为根目录是你可以到达的最高级。

    示例 3:

    输入:path = "/home//foo/"
    输出:
    "/home/foo"
    解释:
    在规范路径中,多个连续斜杠需要用一个斜杠替换。

    示例 4:

    输入:path = "/a/./b/../../c/"
    输出:
    "/c"

     

    提示:

    • 1 <= path.length <= 3000
    • path 由英文字母,数字,'.''/''_' 组成。
    • path 是一个有效的 Unix 风格绝对路径。
    + +## template + +```java +class Solution { + public String simplifyPath(String path) { + + Deque stack = new ArrayDeque<>(); + + for (String str : path.split("/")) { + + if ("".equals(str) || ".".equals(str)) + continue; + stack.push(str); + } + + StringBuilder sb = new StringBuilder(); + int count = 0; + while (!stack.isEmpty()) { + String str = stack.pop(); + + if ("..".equals(str)) + count++; + else { + + if (count > 0) { + count--; + continue; + } + + sb.insert(0, str); + sb.insert(0, "/"); + } + } + + if (sb.length() == 0) + sb.append("/"); + + return sb.toString(); + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/26.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/26.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..d2b44b92fb076145ca4898955e9eec8ac05375af --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/26.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-03b22f16ca8a4355901f6f0a99757f3e", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/26.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/26.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..7e378ada574ce0a50b4f4645132c8f0074b0b263 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/26.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "1ef4881f992d439190409c691eb791ea" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/26.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/26.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..37d5cfd587e3a94935fab301881978bed7f43041 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/26.exercises/solution.md" @@ -0,0 +1,71 @@ +# 两数相加 + +

    给你两个 非空 的链表,表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的,并且每个节点只能存储 一位 数字。

    请你将两个数相加,并以相同形式返回一个表示和的链表。

    你可以假设除了数字 0 之外,这两个数都不会以 0 开头。

     

    示例 1:

    输入:l1 = [2,4,3], l2 = [5,6,4]
    输出:
    [7,0,8]
    解释:
    342 + 465 = 807.

    示例 2:

    输入:l1 = [0], l2 = [0]
    输出:
    [0]

    示例 3:

    输入:l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
    输出:
    [8,9,9,9,0,0,0,1]

     

    提示:

    • 每个链表中的节点数在范围 [1, 100]
    • 0 <= Node.val <= 9
    • 题目数据保证列表表示的数字不含前导零
    + +## template + +```java +class ListNode { + int val; + ListNode next; + ListNode() { + } + ListNode(int val) { + this.val = val; + } + ListNode(int val, ListNode next) { + this.val = val; + this.next = next; + } +} +class Solution { + public ListNode addTwoNumbers(ListNode l1, ListNode l2) { + ListNode lrr = l1; + while (true) { + l1.val = l1.val + l2.val; + if (l1.next == null && l2.next == null && l1.val < 10) { + break; + } + if (l1.next == null) { + l1.next = new ListNode(0); + } + if (l2.next == null) { + l2.next = new ListNode(0); + } + if (l1.val >= 10) { + l1.val = l1.val - 10; + l1.next.val += 1; + } + l1 = l1.next; + l2 = l2.next; + } + return lrr; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/27.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/27.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..4e78f246f264f9a7fbef0c8737d7602e985fd1c4 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/27.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-ddb3174b00024240be3f15a2a74733a1", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/27.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/27.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..2afa45c09273da9a7e85b32bdcf0a268aebc1653 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/27.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "55ed56f613e44a8985bcb73f7063d971" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/27.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/27.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..05aca400d073afaa38674f5e6b1730d4b6cbda0a --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/27.exercises/solution.md" @@ -0,0 +1,64 @@ +# 分隔链表 + +

    给你一个链表的头节点 head 和一个特定值 x ,请你对链表进行分隔,使得所有 小于 x 的节点都出现在 大于或等于 x 的节点之前。

    你应当 保留 两个分区中每个节点的初始相对位置。

     

    示例 1:

    输入:head = [1,4,3,2,5,2], x = 3
    输出
    :[1,2,2,4,3,5]

    示例 2:

    输入:head = [2,1], x = 2
    输出
    :[1,2]

     

    提示:

    • 链表中节点的数目在范围 [0, 200]
    • -100 <= Node.val <= 100
    • -200 <= x <= 200
    + +## template + +```java +public class ListNode { + int val; + ListNode next; + ListNode(int x) { + val = x; + } +} +class Solution { + public ListNode partition(ListNode head, int x) { + ListNode dummyHead1 = new ListNode(0); + ListNode dummyHead2 = new ListNode(0); + ListNode node1 = dummyHead1; + ListNode node2 = dummyHead2; + while (head != null) { + if (head.val < x) { + node1.next = head; + head = head.next; + node1 = node1.next; + node1.next = null; + } else { + node2.next = head; + head = head.next; + node2 = node2.next; + node2.next = null; + } + } + node1.next = dummyHead2.next; + return dummyHead1.next; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/28.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/28.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..45ee6a1b633b7de7a4233a9c2ceb4c2a2c2a73ed --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/28.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-a42019ddbfad4f19b29539cbdb1d558d", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/28.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/28.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..8c709dcb5ce30e5cc24d86b5a2e243373b81c557 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/28.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "3e16432078fd4d6d88a1f056943e56ea" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/28.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/28.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..a9eeebf0877923520bd13b5bd774539769783293 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/28.exercises/solution.md" @@ -0,0 +1,145 @@ +# 整数转罗马数字 + +
    +

    罗马数字包含以下七种字符: I, V, X, LCD 和 M。 +

    + +
    字符          数值
    +I             1
    +V             5
    +X             10
    +L             50
    +C             100
    +D             500
    +M             1000
    + +

    例如, 罗马数字 2 写做 II ,即为两个并列的 1。12 + 写做 XII ,即为 X + II 。 27 + 写做  XXVII, + 即为 XX + V + II 。

    + +

    通常情况下,罗马数字中小的数字在大的数字的右边。但也存在特例,例如 4 不写做 IIII,而是 IV。数字 1 在数字 5 的左边,所表示的数等于大数 5 + 减小数 1 得到的数值 4 。同样地,数字 9 表示为 IX。这个特殊的规则只适用于以下六种情况:

    + +
      +
    • I 可以放在 V (5) 和 X (10) 的左边,来表示 4 和 9。
    • +
    • X 可以放在 L (50) 和 C (100) 的左边,来表示 40 + 和 90。 
    • +
    • C 可以放在 D (500) 和 M (1000) 的左边,来表示 400 + 和 900。
    • +
    + +

    给你一个整数,将其转为罗马数字。

    + +

     

    + +

    示例 1:

    + +
    输入: num = 3
    +
    输出:
    "III"
    + +

    示例 2:

    + +
    输入: num = 4
    +
    输出:
    "IV"
    + +

    示例 3:

    + +
    输入: num = 9
    +
    输出:
    "IX"
    + +

    示例 4:

    + +
    输入: num = 58
    +
    输出:
    "LVIII" +
    解释:
    L = 50, V = 5, III = 3. +
    + +

    示例 5:

    + +
    输入: num = 1994
    +
    输出:
    "MCMXCIV" +
    解释:
    M = 1000, CM = 900, XC = 90, IV = 4.
    + +

     

    + +

    提示:

    + +
      +
    • 1 <= num <= 3999
    • +
    +
    + +## template + +```java +class Solution { + public String intToRoman(int num) { + int f = 1000; + int f2 = 1000; + char[] sym = new char[] { 'M', 'D', 'C', 'L', 'X', 'V', 'I' }; + int fsi = 0; + int[] s = new int[] { 2, 5 }; + int si = 0; + int[] s2 = new int[] { 10, 1 }; + int si2 = 0; + StringBuilder roman = new StringBuilder(); + while (num > 0) { + int d = (int) Math.floor(num / f); + int r = num % f; + int d2 = (int) Math.floor(num / f2); + int r2 = num % f2; + if (d > 0) { + if (d == 4) { + roman.append(sym[fsi]); + roman.append(sym[fsi - 1]); + num = r; + } else if (d2 == 9) { + roman.append(sym[fsi + 1]); + roman.append(sym[fsi - 1]); + num = r2; + } else { + for (int i = 0; i < d; i++) { + roman.append(sym[fsi]); + } + num = r; + } + } + f = f / s[si]; + si++; + si %= 2; + f2 = f2 / s2[si2]; + si2++; + si2 %= 2; + fsi++; + } + return roman.toString(); + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/29.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/29.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..cb57b17afd9147eab10a1cdf9bd00f443a23bc1a --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/29.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-a2234e60f4e241cdaf7118f108a4e7b9", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/29.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/29.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..17fc48e244ce7673806083916567bd79315913c0 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/29.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "d7b9ab0329004d7fa727d65dd13e5867" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/29.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/29.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..d3e5925e321390114060d408b1eb57028b0d9d62 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/29.exercises/solution.md" @@ -0,0 +1,76 @@ +# 全排列 II + +

    给定一个可包含重复数字的序列 nums按任意顺序 返回所有不重复的全排列。

     

    示例 1:

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

    示例 2:

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

     

    提示:

    • 1 <= nums.length <= 8
    • -10 <= nums[i] <= 10
    + +## template + +```java +class Solution { + List> ans = new ArrayList<>(); + + public List> permuteUnique(int[] nums) { + dfs(nums, 0); + return ans; + } + + private void dfs(int[] nums, int cur) { + if (cur == nums.length) { + List line = new ArrayList<>(); + for (int i : nums) { + line.add(i); + } + ans.add(line); + } else { + for (int i = cur; i < nums.length; i++) { + if (canSwap(nums, cur, i)) { + swap(nums, cur, i); + dfs(nums, cur + 1); + swap(nums, cur, i); + } + } + } + } + + private boolean canSwap(int nums[], int begin, int end) { + for (int i = begin; i < end; i++) { + if (nums[i] == nums[end]) { + return false; + } + } + + return true; + } + + private void swap(int nums[], int i, int j) { + int temp = nums[i]; + nums[i] = nums[j]; + nums[j] = temp; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/30.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/30.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..65f44b1ebec9178208293a4400e51a25c36184fe --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/30.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-1ce31ee774cc479ba539ca08f957da5a", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/30.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/30.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..3d1425dab7a57f36a9002a2f7ef83c996bebb528 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/30.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "1f04d4cdb5d148f6a74282b702683f53" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/30.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/30.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..acb00fa8dc564e19aaa6e71e024b0898a086f686 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/30.exercises/solution.md" @@ -0,0 +1,53 @@ +# 无重复字符的最长子串 + +

    给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。

     

    示例 1:

    输入: s = "abcabcbb"
    输出:
    3
    解释:
    因为无重复字符的最长子串是 "abc",所以其长度为 3。

    示例 2:

    输入: s = "bbbbb"
    输出:
    1
    解释:
    因为无重复字符的最长子串是 "b",所以其长度为 1。

    示例 3:

    输入: s = "pwwkew"
    输出:
    3
    解释:
    因为无重复字符的最长子串是 "wke",所以其长度为 3。  +请注意,你的答案必须是 子串 的长度,"pwke" 是一个子序列,不是子串。

    示例 4:

    输入: s = ""
    输出:
    0

     

    提示:

    • 0 <= s.length <= 5 * 104
    • s 由英文字母、数字、符号和空格组成
    + +## template + +```java +class Solution { + public int lengthOfLongestSubstring(String s) { + int i = 0; + int j = 0; + int m = 0; + Map hset = new HashMap<>(); + for (; j < s.length(); j++) { + int code = s.codePointAt(j); + Integer o = hset.get(code); + if (o != null && o > i) { + i = o; + } + m = m > j - i + 1 ? m : j - i + 1; + hset.put(code, j + 1); + } + return m; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/31.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/31.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..18158ae66b0a596e60fc42fafe380d38703a472b --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/31.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-0fec02446f8046d9bb5588f6dfad8f99", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/31.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/31.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..4cf09960dd94081a516a310c7826d59e857b6482 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/31.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "760bca1d15cf44fa93a4510c6648f4f0" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/31.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/31.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..d7e8f6214b12983bca7de0b6e7122fd1a68d92ca --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/31.exercises/solution.md" @@ -0,0 +1,80 @@ +# 解码方法 + +

    一条包含字母 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 只包含数字,并且可能包含前导零。
    • +
    + +## template + +```java +class Solution { + public int numDecodings(String s) { + if (s == null || s.length() == 0) { + return 0; + } + int n = s.length(); + int[] dp = new int[n + 1]; + dp[0] = 1; + dp[1] = (s.charAt(0) == '0' ? 0 : 1); + for (int i = 1; i < n; i++) { + char c = s.charAt(i); + char pre = s.charAt(i - 1); + dp[i + 1] = c == '0' ? 0 : dp[i]; + if (pre == '1' || (pre == '2' && c <= '6')) { + dp[i + 1] += dp[i - 1]; + } + } + return dp[n]; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/32.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/32.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..1ecc6caa4c6068930c6ecd7f4aea293c4663d391 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/32.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-dbf1215ef2ad4f189c646a82bb3015df", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/32.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/32.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..f9ff5b6ee6fb5a7001b3d0123362e5b846322fe7 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/32.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "8478d047fa1b4802badbcbaf24d9d493" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/32.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/32.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..f40de1d011bacb06447b805b45c3bfc984de01be --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/32.exercises/solution.md" @@ -0,0 +1,56 @@ +# 两数相除 + +

    给定两个整数,被除数 dividend 和除数 divisor。将两数相除,要求不使用乘法、除法和 mod 运算符。

    返回被除数 dividend 除以除数 divisor 得到的商。

    整数除法的结果应当截去(truncate)其小数部分,例如:truncate(8.345) = 8 以及 truncate(-2.7335) = -2

     

    示例 1:

    输入: dividend = 10, divisor = 3
    输出:
    3
    解释:
    10/3 = truncate(3.33333..) = truncate(3) = 3

    示例 2:

    输入: dividend = 7, divisor = -3
    输出:
    -2
    解释:
    7/-3 = truncate(-2.33333..) = -2

     

    提示:

    • 被除数和除数均为 32 位有符号整数。
    • 除数不为 0。
    • 假设我们的环境只能存储 32 位有符号整数,其数值范围是 [−231,  231 − 1]。本题中,如果除法结果溢出,则返回 231 − 1。
    + +## template + +```java +class Solution { + public int divide(int dividend, int divisor) { + if (dividend == 0) { + return 0; + } + if (dividend == Integer.MIN_VALUE && divisor == -1) { + return Integer.MAX_VALUE; + } + boolean negative; + negative = (dividend ^ divisor) < 0; + long t = Math.abs((long) dividend); + long d = Math.abs((long) divisor); + int result = 0; + for (int i = 31; i >= 0; i--) { + if ((t >> i) >= d) { + result += 1 << i; + t -= d << i; + } + } + return negative ? -result : result; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/33.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/33.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..f57b06faeeeebf637976305b63702eaaeb67933c --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/33.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-7787f94d6cd24b4687aa04bad490dcdb", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/33.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/33.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..9921900857021f24c005f8c6370e4fcea93c6e16 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/33.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "b0273841fc20454c919506f8347f10aa" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/33.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/33.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..2d33088319bad2c4c38428b47b002b56697e3987 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/33.exercises/solution.md" @@ -0,0 +1,77 @@ +# 螺旋矩阵 + +

    给你一个 mn 列的矩阵 matrix ,请按照 顺时针螺旋顺序 ,返回矩阵中的所有元素。

     

    示例 1:

    输入:matrix = [[1,2,3],[4,5,6],[7,8,9]]
    输出:
    [1,2,3,6,9,8,7,4,5]

    示例 2:

    输入:matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
    输出:
    [1,2,3,4,8,12,11,10,9,5,6,7]

     

    提示:

    • m == matrix.length
    • n == matrix[i].length
    • 1 <= m, n <= 10
    • -100 <= matrix[i][j] <= 100
    + +## template + +```java +class Solution { + public List spiralOrder(int[][] matrix) { + List res = new ArrayList(); + if (matrix.length == 0 || (matrix.length == 1 && matrix[0].length == 0)) + return res; + int left = 0; + int right = matrix[0].length - 1; + int top = 0; + int bottom = matrix.length - 1; + int num = (right + 1) * (bottom + 1); + while (num > 0) { + for (int j = left; j <= right; j++) { + res.add(matrix[top][j]); + num--; + } + if (num <= 0) + break; + top++; + for (int i = top; i <= bottom; i++) { + res.add(matrix[i][right]); + num--; + } + if (num <= 0) + break; + right--; + for (int j = right; j >= left; j--) { + res.add(matrix[bottom][j]); + num--; + } + if (num <= 0) + break; + bottom--; + for (int i = bottom; i >= top; i--) { + res.add(matrix[i][left]); + num--; + } + if (num <= 0) + break; + left++; + } + return res; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/34.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/34.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..406cfb54c55fd5f91a8fb92d3084d6eb2e196a70 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/34.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-64da55094c5b46ba92e7deac23aeb465", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/34.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/34.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..59cfeb12476806649f183d79c1461a8b38a33efb --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/34.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "ce2277c91ff64313afb5212277095237" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/34.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/34.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..eed31e5600dab9de70678a959b93d8f5bb2afcfb --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/34.exercises/solution.md" @@ -0,0 +1,102 @@ +# 全排列 + +

    给定一个 没有重复 数字的序列,返回其所有可能的全排列。

    示例:

    输入: [1,2,3]
    输出:
    [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]]
    + +## template + +```java +class Solution { + public List> permute(int[] nums) { + + List> result = new ArrayList>(); + Arrays.sort(nums); + List first = new ArrayList(); + for (int r = 0; r < nums.length; r++) { + first.add(nums[r]); + } + result.add(first); + int i = nums.length - 2; + while (i >= 0) { + if (nums[i] < nums[i + 1]) { + int temp = nums[i]; + for (int j = nums.length - 1; j > i; j--) { + if (nums[j] > temp) { + nums[i] = nums[j]; + nums[j] = temp; + break; + } + } + nums = quick_sort(nums, i + 1, nums.length - 1); + List sub = new ArrayList(); + for (int t = 0; t < nums.length; t++) { + sub.add(nums[t]); + } + result.add(sub); + i = nums.length - 2; + + } else { + i--; + } + } + + return result; + + } + + public int[] quick_sort(int[] a, int left, int right) { + if (left < right) { + int l = left; + int r = right; + int temp = a[l]; + while (l != r) { + while (l < r && a[r] > temp) { + r--; + } + if (l < r) { + a[l] = a[r]; + l++; + } + + while (l < r && a[l] < temp) { + l++; + } + if (l < r) { + a[r] = a[l]; + r--; + } + + } + a[l] = temp; + quick_sort(a, left, l - 1); + quick_sort(a, l + 1, right); + } + return a; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/35.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/35.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..e72ad814d62decff50bf5c55f87560799effa671 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/35.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-e7e2eb66c1794485bc437486af62e66a", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/35.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/35.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..bba988f8544d58f0882aa9c3867bf3c5240c6aaa --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/35.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "9b8e380a20e146949e874c5b03c6f369" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/35.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/35.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..f8a178fa9a02fdadb7555c5cf0094fa49505065c --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/35.exercises/solution.md" @@ -0,0 +1,90 @@ +# 搜索旋转排序数组 + +

    整数数组 nums 按升序排列,数组中的值 互不相同

    +

    在传递给函数之前,nums 在预先未知的某个下标 k0 <= k < nums.length)上进行了 旋转,使数组变为 + [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]](下标 从 0 开始 + 计数)。例如, [0,1,2,4,5,6,7] 在下标 3 处经旋转后可能变为 [4,5,6,7,0,1,2] 。 +

    +

    给你 旋转后 的数组 nums 和一个整数 target ,如果 nums 中存在这个目标值 + target ,则返回它的下标,否则返回 -1 。 +

    +

     

    +

    示例 1:

    +
    输入:nums = [4,5,6,7,0,1,2], target = 0
    输出:
    4
    +

    示例 2:

    +
    输入:nums = [4,5,6,7,0,1,2], target = 3
    输出:
    -1
    +

    示例 3:

    +
    输入:nums = [1], target = 0
    输出:
    -1
    +

     

    +

    提示:

    +
      +
    • 1 <= nums.length <= 5000
    • +
    • -10^4 <= nums[i] <= 10^4
    • +
    • nums 中的每个值都 独一无二
    • +
    • 题目数据保证 nums 在预先未知的某个下标上进行了旋转
    • +
    • -10^4 <= target <= 10^4
    • +
    +

     

    +

    进阶:你可以设计一个时间复杂度为 O(log n) 的解决方案吗?

    + +## template + +```java +class Solution { + public int search(int[] nums, int target) { + int start = 0; + int end = nums.length - 1; + + while (start <= end) { + int mid = start + (end - start) / 2; + if (nums[mid] == target) { + return mid; + } + + if (nums[start] <= nums[mid]) { + if (target >= nums[start] && target <= nums[mid]) { + end = mid - 1; + } else { + start = start + 1; + } + } + + if (nums[mid] <= nums[end]) { + if (target >= nums[mid] && target <= nums[end]) { + start = mid + 1; + } else { + end = end - 1; + } + } + } + return -1; + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/36.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/36.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..5c0735807eb1cf160c5639fd482a6ee0140830f1 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/36.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-04b312e7a54d49d1a77253fb3de12147", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/36.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/36.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..202938694f81bac2f87a15784b90a23480bfacb9 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/36.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "c10a7a4f66ec47a1955f199759083c35" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/36.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/36.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..f7994d804dca088e5be78732652f61c96f6a2b74 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/36.exercises/solution.md" @@ -0,0 +1,52 @@ +# 搜索二维矩阵 + +

    编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值。该矩阵具有如下特性:

    • 每行中的整数从左到右按升序排列。
    • 每行的第一个整数大于前一行的最后一个整数。

     

    示例 1:

    输入:matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3
    输出:
    true

    示例 2:

    输入:matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13
    输出:
    false

     

    提示:

    • m == matrix.length
    • n == matrix[i].length
    • 1 <= m, n <= 100
    • -104 <= matrix[i][j], target <= 104
    + +## template + +```java +class Solution { + public boolean searchMatrix(int[][] matrix, int target) { + if (matrix.length == 0 || matrix[0].length == 0) + return false; + int begin, mid, end; + begin = mid = 0; + int len1 = matrix.length, len2 = matrix[0].length; + end = len1 * len2 - 1; + while (begin < end) { + mid = (begin + end) / 2; + if (matrix[mid / len2][mid % len2] < target) + begin = mid + 1; + else + end = mid; + } + return matrix[begin / len2][begin % len2] == target; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/37.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/37.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..339ef035642ee34482946b09207c72ef538d3696 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/37.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-fd8d484e8e814825af166059585f378c", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/37.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/37.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..5b09e1a3d0f3ae3ac139406b10f79679ade7cf3f --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/37.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "37a1728a76a441b598788d428e6f92dc" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/37.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/37.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..6f4dfb14319bc9b3aa2257ff4ae5e99f6eb96367 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/37.exercises/solution.md" @@ -0,0 +1,63 @@ +# Pow(x, n) + +

    实现 pow(x, n) ,即计算 x 的 n 次幂函数(即,xn)。

     

    示例 1:

    输入:x = 2.00000, n = 10
    输出:
    1024.00000

    示例 2:

    输入:x = 2.10000, n = 3
    输出:
    9.26100

    示例 3:

    输入:x = 2.00000, n = -2
    输出:
    0.25000
    解释:
    2-2 = 1/22 = 1/4 = 0.25

     

    提示:

    • -100.0 < x < 100.0
    • -231 <= n <= 231-1
    • -104 <= xn <= 104
    + +## template + +```java +public class Solution { + public double myPow(double x, int n) { + if (n < 0) { + + return 1 / pow(x, -n); + } else { + + return pow(x, n); + } + } + + private double pow(double x, int n) { + + if (n == 0) { + return 1.0; + } + if (n == 1) { + return x; + } + double val = pow(x, n / 2); + + if (n % 2 == 0) { + return val * val; + } else { + return val * val * x; + } + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/38.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/38.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..263fff0f6aeb98bc52992db454f7555b2d0ccf86 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/38.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-5b36f419614a4d0ebc413a4dfdcf390f", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/38.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/38.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..f682239674b12f1de999ea207954f3805ce43b5d --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/38.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "f07b670cc53b42a79d011b837578cb42" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/38.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/38.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..b494df343aeb395db6467dd88f28edd1174746b0 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/38.exercises/solution.md" @@ -0,0 +1,88 @@ +# 字母异位词分组 + +

    给定一个字符串数组,将字母异位词组合在一起。字母异位词指字母相同,但排列不同的字符串。

    +

    示例:

    +
    输入:[eat", "tea", "tan", "ate", "nat", "bat"]
    输出:
    [[ate","eat","tea"],["nat","tan"],["bat"]]
    +

    说明:

    +
      +
    • 所有输入均为小写字母。
    • +
    • 不考虑答案输出的顺序。
    • +
    + +## template + +```java + +import java.util.*; + +public class GroupAnagrams { + + public List> groupAnagrams(String[] strs) { + HashMap> map = new HashMap<>(); + for (String str : strs) { + char[] cs = str.toCharArray(); + Arrays.sort(cs); + + String key = String.valueOf(cs); + if (!map.containsKey(key)) { + map.put(key, new ArrayList<>()); + } + map.get(key).add(str); + } + return new ArrayList(map.values()); + } + + public List> groupAnagrams2(String[] strs) { + if (strs.length <= 0) { + return new ArrayList<>(); + } + HashMap> map = new HashMap<>(); + for (String str : strs) { + char[] cs = str.toCharArray(); + int[] count = new int[26]; + for (char c : cs) { + ++count[c - 'a']; + } + StringBuilder s = new StringBuilder(""); + for (int num : count) { + s.append(num); + } + + String key = String.valueOf(s); + + if (!map.containsKey(key)) { + map.put(key, new ArrayList<>()); + } + map.get(key).add(str); + } + return new ArrayList(map.values()); + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/39.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/39.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..538f4f79b9e39280e882685b3bc3a8570c1eb310 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/39.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-47f60d66943b4038b2a296bfa0375921", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/39.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/39.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..499b5d39086561b291ab7d494e3c7bb20281664b --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/39.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "8309de6325b747ef9b6df5662d93706c" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/39.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/39.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..5782ce2cbadce6f114dea17cf232d02d728a48e6 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/39.exercises/solution.md" @@ -0,0 +1,55 @@ +# 矩阵置零 + +

    给定一个 m x n 的矩阵,如果一个元素为 0 ,则将其所在行和列的所有元素都设为 0 。请使用 原地 算法

    进阶:

    • 一个直观的解决方案是使用  O(mn) 的额外空间,但这并不是一个好的解决方案。
    • 一个简单的改进方案是使用 O(m + n) 的额外空间,但这仍然不是最好的解决方案。
    • 你能想出一个仅使用常量空间的解决方案吗?

     

    示例 1:

    输入:matrix = [[1,1,1],[1,0,1],[1,1,1]]
    输出:
    [[1,0,1],[0,0,0],[1,0,1]]

    示例 2:

    输入:matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]]
    输出:
    [[0,0,0,0],[0,4,5,0],[0,3,1,0]]

     

    提示:

    • m == matrix.length
    • n == matrix[0].length
    • 1 <= m, n <= 200
    • -231 <= matrix[i][j] <= 231 - 1
    + +## template + +```java +class Solution { + public void setZeroes(int[][] matrix) { + int[] xNum = new int[matrix[0].length]; + int[] yNum = new int[matrix.length]; + for (int i = 0; i < matrix.length; i++) { + for (int j = 0; j < matrix[i].length; j++) { + if (matrix[i][j] == 0) { + xNum[j] = 1; + yNum[i] = 1; + } + } + } + for (int i = 0; i < matrix.length; i++) { + for (int j = 0; j < matrix[i].length; j++) { + if (xNum[j] == 1 || yNum[i] == 1) { + matrix[i][j] = 0; + } + } + } + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/40.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/40.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..583477d51f61d33936606090e2974be94b557f35 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/40.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-422a519de5334474931339182df135d0", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/40.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/40.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..bf5cca0487ea28cc9374737cbdf8ee976250ee02 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/40.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "148b3429a9ab4632867ec7996e6940ea" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/40.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/40.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..1b4353788340f38b88354f3c1a221ca33dc76a54 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/40.exercises/solution.md" @@ -0,0 +1,99 @@ +# 在排序数组中查找元素的第一个和最后一个位置 + +

    给定一个按照升序排列的整数数组 nums,和一个目标值 target。找出给定目标值在数组中的开始位置和结束位置。

    +

    如果数组中不存在目标值 target,返回 [-1, -1]

    +

    进阶:

    +
      +
    • 你可以设计并实现时间复杂度为 O(log n) 的算法解决此问题吗?
    • +
    +

     

    +

    示例 1:

    +
    输入:nums = [5,7,7,8,8,10], target = 8
    输出:
    [3,4]
    +

    示例 2:

    +
    输入:nums = [5,7,7,8,8,10], target = 6
    输出:
    [-1,-1]
    +

    示例 3:

    +
    输入:nums = [], target = 0
    输出:
    [-1,-1]
    +

     

    +

    提示:

    +
      +
    • 0 <= nums.length <= 105
    • +
    • -109 <= nums[i] <= 109
    • +
    • nums 是一个非递减数组
    • +
    • -109 <= target <= 109
    • +
    + +## template + +```java +public class Solution { + + public int[] searchRange(int[] nums, int target) { + int[] result = new int[2]; + result[0] = floor(nums, target); + result[1] = ceil(nums, target); + return result; + } + + private int floor(int[] nums, int target) { + int left = -1; + int right = nums.length - 1; + while (left < right) { + int mid = left + (right - left + 1) / 2; + if (target <= nums[mid]) { + right = mid - 1; + } else { + left = mid; + } + } + if (left + 1 < nums.length && nums[left + 1] == target) { + return left + 1; + } else { + return -1; + } + } + + private int ceil(int[] nums, int target) { + int left = 0; + int right = nums.length; + while (left < right) { + int mid = left + (right - left) / 2; + if (target >= nums[mid]) { + left = mid + 1; + } else { + right = mid; + } + } + if (right - 1 >= 0 && nums[right - 1] == target) { + return right - 1; + } else { + return -1; + } + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/41.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/41.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..1ee0aef300a4886f4dc3a0ddd7b3dfb2a90ffc14 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/41.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-4139b9f6d3b14ab991cc31ce9ea1f9b7", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/41.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/41.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..2e49362c9169a5a2053faae0bef0f1041c534ea8 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/41.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "00fe811789944c828971dd8c7c091086" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/41.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/41.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..eaaa27f579f61797d37f7255502a84d2db8828e8 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/41.exercises/solution.md" @@ -0,0 +1,58 @@ +# 跳跃游戏 II + +

    给定一个非负整数数组,你最初位于数组的第一个位置。

    +

    数组中的每个元素代表你在该位置可以跳跃的最大长度。

    +

    你的目标是使用最少的跳跃次数到达数组的最后一个位置。

    +

    示例:

    +
    输入: [2,3,1,1,4]
    输出:
    2
    解释:
    跳到最后一个位置的最小跳跃数是 2。从下标为 0 跳到下标为 1 的位置,跳 1 步,然后跳 3 步到达数组的最后一个位置。
    +

    说明:

    +

    假设你总是可以到达数组的最后一个位置。

    + +## template + +```java +class Solution { + public int jump(int[] nums) { + int end = 0; + int steps = 0; + int maxPosition = 0; + for (int i = 0; i < nums.length - 1; i++) { + + maxPosition = Math.max(maxPosition, i + nums[i]); + + if (i == end) { + end = maxPosition; + steps++; + } + } + return steps; + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/42.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/42.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..8f28be5b1e55438b448706fd46ca2e0510dfd18d --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/42.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-9646692dbf1a413c84b0e00893518fed", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/42.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/42.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..54a9df14b2e43bfb601c8a2ad5193d742f7427de --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/42.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "342fe214c0514c5188b925933a888bf1" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/42.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/42.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..f29396a8c370ff2b02552f4a0ebc245a0d9edd47 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/42.exercises/solution.md" @@ -0,0 +1,52 @@ +# 反转链表 II + +给你单链表的头指针 head 和两个整数 leftright ,其中 left <= right 。请你反转从位置 left 到位置 right 的链表节点,返回 反转后的链表

     

    示例 1:

    输入:head = [1,2,3,4,5], left = 2, right = 4
    输出:
    [1,4,3,2,5]

    示例 2:

    输入:head = [5], left = 1, right = 1
    输出:
    [5]

     

    提示:

    • 链表中节点数目为 n
    • 1 <= n <= 500
    • -500 <= Node.val <= 500
    • 1 <= left <= right <= n

     

    进阶: 你可以使用一趟扫描完成反转吗?

    + +## template + +```java +class Solution { + public ListNode reverseBetween(ListNode head, int m, int n) { + ListNode dummy = new ListNode(0); + dummy.next = head; + ListNode pre = dummy; + for (int i = 1; i < m; i++) { + pre = pre.next; + } + head = pre.next; + for (int i = m; i < n; i++) { + ListNode nex = head.next; + head.next = nex.next; + nex.next = pre.next; + pre.next = nex; + } + return dummy.next; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/43.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/43.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..0903ee0fbcb905eaa0d0461bb262ff349e6d1739 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/43.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-db524e3e613b42918a34ecb098852676", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/43.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/43.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..fa50b9a3448ed21ee96a3c787df05e7c1a4dce33 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/43.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "7a9170d9073f41adba5980ab2408b618" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/43.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/43.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..524f0965a6851bcb238331e8fa66647df3ca0abb --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/43.exercises/solution.md" @@ -0,0 +1,76 @@ +# 最小路径和 + +

    给定一个包含非负整数的 m x n 网格 grid ,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小。

    说明:每次只能向下或者向右移动一步。

     

    示例 1:

    输入:grid = [[1,3,1],[1,5,1],[4,2,1]]
    输出:
    7
    解释:
    因为路径 1→3→1→1→1 的总和最小。

    示例 2:

    输入:grid = [[1,2,3],[4,5,6]]
    输出:
    12

     

    提示:

    • m == grid.length
    • n == grid[i].length
    • 1 <= m, n <= 200
    • 0 <= grid[i][j] <= 100
    + +## template + +```java +class Solution { + public int minPathSum(int[][] grid) { + int m = grid.length; + int n = grid[0].length; + + int sum = 0; + + if (m < 1 || n < 1) + return 0; + if (m == 1) { + for (int i = 0; i < n; i++) { + sum = sum + grid[0][i]; + } + return sum; + } + if (n == 1) { + for (int i = 0; i < m; i++) { + sum = sum + grid[i][0]; + } + return sum; + } + + int[][] dp = new int[m][n]; + dp[0][0] = grid[0][0]; + + for (int k = 1; k < m; k++) { + dp[k][0] = grid[k][0] + dp[k - 1][0]; + } + + for (int l = 1; l < n; l++) { + dp[0][l] = grid[0][l] + dp[0][l - 1]; + } + + for (int k = 1; k < m; k++) { + for (int l = 1; l < n; l++) { + dp[k][l] = grid[k][l] + Math.min(dp[k - 1][l], dp[k][l - 1]); + } + } + return dp[m - 1][n - 1]; + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/44.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/44.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..f4c6c0826c2119b32a94c0cfaadbb9d41433a575 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/44.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-30087098d11a44a18d83493d3cb35a1f", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/44.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/44.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..2902736d8b0681c8047895288a07e1e1adbe2332 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/44.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "0640b7f2f9a445db86c6e2ed6b4823c1" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/44.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/44.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..28563a1a1b3c578854018818d36440d98ac4ae54 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/44.exercises/solution.md" @@ -0,0 +1,62 @@ +# 删除链表的倒数第 N 个结点 + +

    给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点。

    进阶:你能尝试使用一趟扫描实现吗?

     

    示例 1:

    输入:head = [1,2,3,4,5], n = 2
    输出:
    [1,2,3,5]

    示例 2:

    输入:head = [1], n = 1
    输出:
    []

    示例 3:

    输入:head = [1,2], n = 1
    输出:
    [1]

     

    提示:

    • 链表中结点的数目为 sz
    • 1 <= sz <= 30
    • 0 <= Node.val <= 100
    • 1 <= n <= sz
    + +## template + +```java +public class ListNode { + int val; + ListNode next; + ListNode() { + } + ListNode(int val) { + this.val = val; + } + ListNode(int val, ListNode next) { + this.val = val; + this.next = next; + } +} +class Solution { + public ListNode removeNthFromEnd(ListNode head, int n) { + ListNode v = new ListNode(0, head); + ListNode handle = v; + List index = new ArrayList<>(); + while (v != null) { + index.add(v); + v = v.next; + } + int pre = index.size() - n - 1; + int next = index.size() - n + 1; + index.get(pre).next = next >= 0 && next < index.size() ? index.get(next) : null; + return handle.next; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/45.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/45.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..6d874171e81b854f265459444f6db80540afe239 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/45.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-55b12ac398bc400b97847513b588e14f", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/45.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/45.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..dbfbab7ac38de384ea99fc99511c9b641a826e1c --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/45.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "9721247fd6a44b4c91cc63d6ac0be993" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/45.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/45.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..8738110ee64af902262d66adccba7099d80c60ca --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/45.exercises/solution.md" @@ -0,0 +1,88 @@ +# 组合总和 + +

    给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。 +

    +

    candidates 中的数字可以无限制重复被选取。

    +

    说明:

    +
      +
    • 所有数字(包括 target)都是正整数。
    • +
    • 解集不能包含重复的组合。 
    • +
    +

    示例 1:

    +
    输入:candidates = [2,3,6,7], target = 7,
    输出:
    [[7],[2,2,3]]
    +

    示例 2:

    +
    输入:candidates = [2,3,5], target = 8,
    输出:
    [[2,2,2,2],[2,3,3],[3,5]]
    +

     

    +

    提示:

    +
      +
    • 1 <= candidates.length <= 30
    • +
    • 1 <= candidates[i] <= 200
    • +
    • candidate 中的每个元素都是独一无二的。
    • +
    • 1 <= target <= 500
    • +
    + +## template + +```java +class Solution { + public List> combinationSum(int[] candiates, int target) { + + List> resultList = new ArrayList<>(); + + List result = new ArrayList<>(); + + Arrays.sort(candiates); + + dfs(candiates, resultList, result, 0, target); + return resultList; + + } + + private void dfs(int[] candiates, List> resultList, List result, int start, int target) { + + if (target < 0) { + return; + } + + else if (target == 0) { + + resultList.add(new ArrayList<>(result)); + } else { + for (int i = start; i < candiates.length; i++) { + result.add(candiates[i]); + + dfs(candiates, resultList, result, i, target - candiates[i]); + + result.remove(result.size() - 1); + } + } + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/46.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/46.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..217e777636fbbea4036e1f9909d228265cfb6771 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/46.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-15c4db7be0a743e08df53431886cb58e", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/46.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/46.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..1cae4225016bd10b1b61e11d8d559ed6e6016d73 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/46.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "64b5adb956c74448b5c1d4b6077ecdd1" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/46.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/46.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..71a1b37740dad39799c22400565f483c76ac4b3c --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/46.exercises/solution.md" @@ -0,0 +1,68 @@ +# 最长回文子串 + +

    给你一个字符串 s,找到 s 中最长的回文子串。

     

    示例 1:

    输入:s = "babad"
    输出:
    "bab"
    解释:
    "aba" 同样是符合题意的答案。

    示例 2:

    输入:s = "cbbd"
    输出:
    "bb"

    示例 3:

    输入:s = "a"
    输出:
    "a"

    示例 4:

    输入:s = "ac"
    输出:
    "a"

     

    提示:

    • 1 <= s.length <= 1000
    • s 仅由数字和英文字母(大写和/或小写)组成
    + +## template + +```java +class Solution { + public String longestPalindrome(String s) { + int ti = 0, maxlen = 0, i, t; + for (i = 0; i < s.length(); i++) { + t = 1; + while (t <= i && i + t < s.length()) { + if (s.charAt(i + t) == s.charAt(i - t)) + t++; + else + break; + } + t--; + if (2 * t + 1 > maxlen) { + ti = i - t; + maxlen = 2 * t + 1; + } + } + for (i = 0; i < s.length(); i++) { + t = 1; + while (t <= i + 1 && i + t < s.length()) { + if (s.charAt(i - t + 1) == s.charAt(i + t)) + t++; + else + break; + } + t--; + if (2 * t > maxlen) { + ti = i - t + 1; + maxlen = 2 * t; + } + } + return s.substring(ti, ti + maxlen); + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/47.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/47.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..05577be10c4acddea560fca3e36847322090016b --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/47.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-c630196f3c834970824b27bbdf3240d4", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/47.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/47.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..9bdfd637e1d76156fecc9dff3417adef7f602834 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/47.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "23dc75eca6fb49d3bac3605f9bbef9bf" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/47.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/47.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..e71e9cfeb5c89bc846d319c0bd3a2afbca22365a --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/47.exercises/solution.md" @@ -0,0 +1,89 @@ +# 搜索旋转排序数组 II + +

    已知存在一个按非降序排列的整数数组 nums ,数组中的值不必互不相同。

    +

    在传递给函数之前,nums 在预先未知的某个下标 k0 <= k < nums.length)上进行了 旋转 + ,使数组变为 [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]](下标 从 0 + 开始 计数)。例如, [0,1,2,4,4,4,5,6,6,7] 在下标 5 处经旋转后可能变为 + [4,5,6,6,7,0,1,2,4,4] 。 +

    +

    给你 旋转后 的数组 nums 和一个整数 target ,请你编写一个函数来判断给定的目标值是否存在于数组中。如果 + nums 中存在这个目标值 target ,则返回 true ,否则返回 false 。 +

    +

     

    +

    示例 1:

    +
    输入:nums = [2,5,6,0,0,1,2], target = 0
    输出:
    true
    +

    示例 2:

    +
    输入:nums = [2,5,6,0,0,1,2], target = 3
    输出:
    false
    +

     

    +

    提示:

    +
      +
    • 1 <= nums.length <= 5000
    • +
    • -104 <= nums[i] <= 104
    • +
    • 题目数据保证 nums 在预先未知的某个下标上进行了旋转
    • +
    • -104 <= target <= 104
    • +
    +

     

    +

    进阶:

    +
      +
    • 这是 搜索旋转排序数组 的延伸题目,本题中的 nums  + 可能包含重复元素。
    • +
    • 这会影响到程序的时间复杂度吗?会有怎样的影响,为什么?
    • +
    + +## template + +```java +class Solution { + public boolean search(int[] nums, int target) { + int low = 0; + int high = nums.length - 1; + while (low <= high) { + while (low < high && nums[low] == nums[low + 1]) { + low++; + } + while (low < high && nums[high] == nums[high - 1]) { + high--; + } + int mid = (low + high) / 2; + if (nums[mid] == target) { + return true; + } + if (nums[mid] >= nums[0] && (target > nums[mid] || target < nums[0])) { + low = mid + 1; + } else if (nums[mid] < nums[0] && target > nums[mid] && target < nums[0]) { + low = mid + 1; + } else { + high = mid - 1; + } + } + return false; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/48.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/48.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..d11d6ba22a84b1f3ba03de3bf14f816840346bf4 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/48.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-9df24233d3aa4a8aa96e636bf16935a2", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/48.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/48.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..4821cf3e9a9af6f3cee87485df1ce0c7710ff7dd --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/48.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "e2d5c60bfd0840b39345fcea3bc158a3" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/48.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/48.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..a8404bf2f9f1bbcba05cc8f51a65689372e64f80 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/48.exercises/solution.md" @@ -0,0 +1,70 @@ +# 删除有序数组中的重复项 II + +

    给你一个有序数组 nums ,请你 原地 删除重复出现的元素,使每个元素 最多出现两次 ,返回删除后数组的新长度。

    +

    不要使用额外的数组空间,你必须在 原地 修改输入数组 并在使用 O(1) 额外空间的条件下完成。

    +

     

    +

    说明:

    +

    为什么返回数值是整数,但输出的答案是数组呢?

    +

    请注意,输入数组是以「引用」方式传递的,这意味着在函数里修改输入数组对于调用者是可见的。

    +

    你可以想象内部操作如下:

    +
    +    // nums 是以“引用”方式传递的。也就是说,不对实参做任何拷贝
    +    int len = removeDuplicates(nums);// 在函数里修改输入数组对于调用者是可见的。
    +    // 根据你的函数返回的长度, 它会打印出数组中 该长度范围内 的所有元素。
    +    for (int i = 0; i < len; i++) {
    +            print(nums[i]);
    +    }
    +

     

    +

    示例 1:

    +
    输入:nums = [1,1,1,2,2,3]
    输出:
    5, nums = [1,1,2,2,3]
    解释:
    函数应返回新长度 length = 5, 并且原数组的前五个元素被修改为 1, 1, 2, 2, 3 。 不需要考虑数组中超出新长度后面的元素。
    +

    示例 2:

    +
    输入:nums = [0,0,1,1,1,1,2,3,3]
    输出:
    7, nums = [0,0,1,1,2,3,3]
    解释:
    函数应返回新长度 length = 7, 并且原数组的前五个元素被修改为 0, 0, 1, 1, 2, 3, 3 。 不需要考虑数组中超出新长度后面的元素。
    +

     

    +

    提示:

    +
      +
    • 1 <= nums.length <= 3 * 104
    • +
    • -104 <= nums[i] <= 104
    • +
    • nums 已按升序排列
    • +
    + +## template + +```java +class Solution { + public int removeDuplicates(int[] nums) { + int i = 0; + for (int n : nums) + if (i < 2 || n > nums[i - 2]) + nums[i++] = n; + return i; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/49.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/49.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..b833386587bcef001d667e72344784d2c389d4ee --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/49.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-8f7f1dee77f4486397fe49d39c40a7b6", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/49.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/49.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..1306bcbd929b70096c59feaeb042b300531882a8 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/49.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "4dceb07c017b4310ab5e15b16b15572e" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/49.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/49.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..5b10137a9cef987f6f738acfa144395cb9c1bc57 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/49.exercises/solution.md" @@ -0,0 +1,55 @@ +# 交错字符串 + +

    给定三个字符串 s1s2s3,请你帮忙验证 s3 是否是由 s1 和 s2 交错 组成的。

    两个字符串 st 交错 的定义与过程如下,其中每个字符串都会被分割成若干 非空 子字符串:

    • s = s1 + s2 + ... + sn
    • t = t1 + t2 + ... + tm
    • |n - m| <= 1
    • 交错s1 + t1 + s2 + t2 + s3 + t3 + ... 或者 t1 + s1 + t2 + s2 + t3 + s3 + ...

    提示:a + b 意味着字符串 ab 连接。

     

    示例 1:

    输入:s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac"
    输出:
    true

    示例 2:

    输入:s1 = "aabcc", s2 = "dbbca", s3 = "aadbbbaccc"
    输出:
    false

    示例 3:

    输入:s1 = "", s2 = "", s3 = ""
    输出:
    true

     

    提示:

    • 0 <= s1.length, s2.length <= 100
    • 0 <= s3.length <= 200
    • s1s2、和 s3 都由小写英文字母组成
    + +## template + +```java +class Solution { + public boolean isInterleave(String s1, String s2, String s3) { + if ((s1.length() + s2.length()) != s3.length()) + return false; + boolean[][] dp = new boolean[s2.length() + 1][s1.length() + 1]; + dp[0][0] = true; + for (int i = 1; i <= s1.length(); i++) { + dp[0][i] = dp[0][i - 1] && s1.charAt(i - 1) == s3.charAt(i - 1) ? true : false; + } + for (int i = 1; i <= s2.length(); i++) { + dp[i][0] = dp[i - 1][0] && s2.charAt(i - 1) == s3.charAt(i - 1) ? true : false; + } + for (int i = 1; i < dp.length; i++) { + for (int j = 1; j < dp[0].length; j++) { + dp[i][j] = (dp[i][j - 1] && s1.charAt(j - 1) == s3.charAt(i + j - 1)) + || (dp[i - 1][j] && s2.charAt(i - 1) == s3.charAt(i + j - 1)); + } + } + return dp[s2.length()][s1.length()]; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/50.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/50.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..7a0515209a9776c94c769264db5d5575cf6e3fe9 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/50.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-b3a89daf270b4516a54c7f96273b1f82", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/50.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/50.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..00994b2af94501fab9a1ea07414b01b330076b10 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/50.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "531741de3f9d46c9a2cf010dcb8585da" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/50.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/50.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..72e49eecade964c16306dfcb32c1d3b9f67bc31e --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/50.exercises/solution.md" @@ -0,0 +1,58 @@ +# 合并区间 + +

    以数组 intervals 表示若干个区间的集合,其中单个区间为 intervals[i] = [starti, endi] 。请你合并所有重叠的区间,并返回一个不重叠的区间数组,该数组需恰好覆盖输入中的所有区间。

     

    示例 1:

    输入:intervals = [[1,3],[2,6],[8,10],[15,18]]
    输出:
    [[1,6],[8,10],[15,18]]
    解释:
    区间 [1,3] 和 [2,6] 重叠, 将它们合并为 [1,6].

    示例 2:

    输入:intervals = [[1,4],[4,5]]
    输出:
    [[1,5]]
    解释:
    区间 [1,4] 和 [4,5] 可被视为重叠区间。

     

    提示:

    • 1 <= intervals.length <= 104
    • intervals[i].length == 2
    • 0 <= starti <= endi <= 104
    + +## template + +```java +class Solution { + public int[][] merge(int[][] intervals) { + List res = new ArrayList<>(); + if (intervals == null) { + return res.toArray(new int[0][]); + } + Arrays.sort(intervals, (a, b) -> a[0] - b[0]); + int i = 0; + int left = 0; + int right = 0; + while (i < intervals.length) { + left = intervals[i][0]; + right = intervals[i][1]; + while (i < intervals.length - 1 && right >= intervals[i + 1][0]) { + i++; + right = Math.max(right, intervals[i][1]); + } + res.add(new int[] { left, right }); + i++; + } + return res.toArray(new int[0][]); + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/51.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/51.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..c0ec0886b727d3e1d3071e5300a4ca391f18f386 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/51.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-4bd007ac2e064eb5aef42420481688da", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/51.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/51.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..7fd1a3affa95f2de371c2b4ff825e7c540fad4cf --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/51.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "31588c8b09f7447dacebad4659b96ef5" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/51.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/51.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..56fae876e39f9f18222b37293582fcb2a382a26c --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/51.exercises/solution.md" @@ -0,0 +1,81 @@ +# 三数之和 + +

    给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有和为 0 且不重复的三元组。

    注意:答案中不可以包含重复的三元组。

     

    示例 1:

    输入:nums = [-1,0,1,2,-1,-4]
    输出:
    [[-1,-1,2],[-1,0,1]]

    示例 2:

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

    示例 3:

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

     

    提示:

    • 0 <= nums.length <= 3000
    • -105 <= nums[i] <= 105
    + +## template + +```java +class Solution { + public List> threeSum(int[] nums) { + Arrays.sort(nums); + List> result = new ArrayList<>(); + int cur, left, right; + cur = 0; + while (cur < nums.length) { + if (nums[cur] > 0) + break; + left = cur + 1; + right = nums.length - 1; + while (left < right) { + int n = nums[cur] + nums[left] + nums[right]; + if (n == 0) { + List r = new ArrayList(); + r.add(nums[cur]); + r.add(nums[left]); + r.add(nums[right]); + result.add(r); + int t = left + 1; + while (t < right && nums[t] == nums[left]) + t++; + left = t; + t = right - 1; + while (t > left && nums[t] == nums[right]) + t--; + right = t; + } else if (n > 0) { + int t = right - 1; + while (t > left && nums[t] == nums[right]) + t--; + right = t; + } else { + int t = left + 1; + while (t < right && nums[t] == nums[left]) + t++; + left = t; + } + } + int t = cur + 1; + while (t < nums.length && nums[t] == nums[cur]) + t++; + cur = t; + } + return result; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/52.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/52.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..5ab1c4881ce84d8eb4f72e9eb7dbf1cb53419163 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/52.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-4325e7eaef78410e987609e4cb9668f3", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/52.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/52.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..dabf295b6508a63da3f3f4c73e706d239350af59 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/52.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "5f90248041484830a49a17daf97b30ee" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/52.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/52.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..cad93074c3c0ac67e70bed73c16140233a07fc58 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/52.exercises/solution.md" @@ -0,0 +1,65 @@ +# 字符串相乘 + +

    给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式。

    示例 1:

    输入: num1 = "2", num2 = "3"
    输出:
    "6"

    示例 2:

    输入: num1 = "123", num2 = "456"
    输出:
    "56088"

    说明:

    1. num1 和 num2 的长度小于110。
    2. num1 和 num2 只包含数字 0-9
    3. num1 和 num2 均不以零开头,除非是数字 0 本身。
    4. 不能使用任何标准库的大数类型(比如 BigInteger)直接将输入转换为整数来处理
    + +## template + +```java +class Solution { + public String multiply(String num1, String num2) { + if (num1.equals("0") || num2.equals("0")) + return "0"; + + int m = num1.length(); + int n = num2.length(); + + int[] intRes = new int[m + n - 1]; + + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + intRes[i + j] += (num1.charAt(i) - 48) * (num2.charAt(j) - 48); + } + } + + for (int i = intRes.length - 1; i > 0; i--) { + if (intRes[i] >= 10) { + intRes[i - 1] += intRes[i] / 10; + intRes[i] %= 10; + } + } + + String res = ""; + for (int i = 0; i < intRes.length; i++) { + res += String.valueOf(intRes[i]); + } + return res; + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/53.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/53.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..5fa3f66b007197aec067eb72127e9e2e24d6ab2b --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/53.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-b39a91a210b145af9dd80190d97e3aad", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/53.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/53.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..4a16855d2afec7976c1028c012fa9e3d68f2e792 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/53.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "514591aa8d344620a18c19b7626b99b3" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/53.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/53.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..d52cb196b62e35f0364cdbc167ab06165746aeba --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/53.exercises/solution.md" @@ -0,0 +1,84 @@ +# 最接近的三数之和 + +

    给定一个包括 n 个整数的数组 nums 和 一个目标值 target。找出 nums 中的三个整数,使得它们的和与 target 最接近。返回这三个数的和。假定每组输入只存在唯一答案。

     

    示例:

    输入:nums = [-1,2,1,-4], target = 1
    输出:
    2
    解释:
    与 target 最接近的和是 2 (-1 + 2 + 1 = 2) 。

     

    提示:

    • 3 <= nums.length <= 10^3
    • -10^3 <= nums[i] <= 10^3
    • -10^4 <= target <= 10^4
    + +## template + +```java +class Solution { + int oneSumCloset(int[] nums, int i, int j, int start, int end, int target) { + if (start == i || start == j) + start = start + 1; + if (end == i || end == j) + end = end - 1; + if (start == end) { + return nums[start]; + } else if (end == start + 1 || end == start - 1) { + if (Math.abs(nums[end] - target) > Math.abs(nums[start] - target)) { + return nums[start]; + } else { + return nums[end]; + } + } else { + int middle = (int) Math.floor((start + end) / 2); + if (nums[middle] > target) { + end = middle; + } else { + start = middle; + } + return oneSumCloset(nums, i, j, start, end, target); + } + } + public int threeSumClosest(int[] nums, int target) { + Arrays.sort(nums); + int minValue = 0; + boolean hasMin = false; + for (int i = 0; i < nums.length - 2; i++) { + for (int j = i + 1; j < nums.length - 1; j++) { + int twoSum = nums[i] + nums[j]; + int rest = target - twoSum; + int restClost = oneSumCloset(nums, i, j, j + 1, nums.length - 1, rest); + int newValue = restClost + twoSum; + ; + if (!hasMin) { + minValue = newValue; + hasMin = true; + } else { + int d1 = Math.abs(minValue - target); + int d2 = Math.abs(newValue - target); + if (d1 > d2) { + minValue = newValue; + } + } + } + } + return minValue; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/54.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/54.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..9ae43e1a2b74f5a3fdc3d8d5d0fb19c4ba25dbe0 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/54.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-a9d16f984f094eadbae6706274b14dea", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/54.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/54.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..7b46f572da62e53353c0903a094b2ada13fbec16 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/54.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "64742d9f25eb4d769741b596fd1451c8" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/54.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/54.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..81462423b692ce3b01df1cbdd08032182f388865 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/54.exercises/solution.md" @@ -0,0 +1,55 @@ +# 跳跃游戏 + +

    给定一个非负整数数组 nums ,你最初位于数组的 第一个下标

    数组中的每个元素代表你在该位置可以跳跃的最大长度。

    判断你是否能够到达最后一个下标。

     

    示例 1:

    输入:nums = [2,3,1,1,4]
    输出:
    true
    解释:
    可以先跳 1 步,从下标 0 到达下标 1, 然后再从下标 1 跳 3 步到达最后一个下标。

    示例 2:

    输入:nums = [3,2,1,0,4]
    输出:
    false
    解释:
    无论怎样,总会到达下标为 3 的位置。但该下标的最大跳跃长度是 0 , 所以永远不可能到达最后一个下标。

     

    提示:

    • 1 <= nums.length <= 3 * 104
    • 0 <= nums[i] <= 105
    + +## template + +```java +class Solution { + public boolean canJump(int[] nums) { + boolean can = true; + if (nums.length < 2) { + return can; + } + int n = nums.length; + int stride = 1; + for (int i = n - 2; i >= 0; i--) { + if (nums[i] < stride) { + stride++; + can = false; + } else { + can = true; + stride = 1; + } + } + return can; + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/55.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/55.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..793dcb5a4ae5c8256c8f463f161470b3eb251ac7 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/55.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-05053a74a39444869d60fc6e317e52cb", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/55.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/55.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..ce78cc2509ad875868506a50f4e38a2bf29f1a63 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/55.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "9d86ef9fca2f4b299aabbc92918e96ea" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/55.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/55.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..a20d0041a18e905117edf1531ea8e003cd2d3c60 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/55.exercises/solution.md" @@ -0,0 +1,67 @@ +# 单词搜索 + +

    给定一个 m x n 二维字符网格 board 和一个字符串单词 word 。如果 word 存在于网格中,返回 true ;否则,返回 false

    单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。

     

    示例 1:

    输入:board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"
    输出:
    true

    示例 2:

    输入:board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "SEE"
    输出:
    true

    示例 3:

    输入:board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCB"
    输出:
    false

     

    提示:

    • m == board.length
    • n = board[i].length
    • 1 <= m, n <= 6
    • 1 <= word.length <= 15
    • boardword 仅由大小写英文字母组成

     

    进阶:你可以使用搜索剪枝的技术来优化解决方案,使其在 board 更大的情况下可以更快解决问题?

    + +## template + +```java +class Solution { + public boolean exist(char[][] board, String word) { + int cl = board.length; + int rl = board[0].length; + boolean[][] flag = new boolean[cl][rl]; + for (int i = 0; i < cl; i++) { + for (int j = 0; j < rl; j++) { + if (find(board, word, flag, i, j, 0)) + return true; + } + } + + return false; + } + + public boolean find(char[][] board, String word, boolean[][] flag, int i, int j, int index) { + int cl = board.length; + int rl = board[0].length; + if (word.length() == index) + return true; + if (i < 0 || i >= cl || j >= rl || j < 0) + return false; + if (flag[i][j] || word.charAt(index) != board[i][j]) + return false; + flag[i][j] = true; + boolean judge = find(board, word, flag, i - 1, j, index + 1) || find(board, word, flag, i + 1, j, index + 1) + || find(board, word, flag, i, j - 1, index + 1) || find(board, word, flag, i, j + 1, index + 1); + flag[i][j] = false; + return judge; + + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/56.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/56.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..2767399935fc4c41184758404a1641542a300751 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/56.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-a6e95cfa9c8549e6ad3e487f7f7dcc6a", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/56.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/56.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..ea3cdddf0ca6cfe66b9d801ae5381ba5ca57c1bd --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/56.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "ac3b780342e942989c756c82951443b0" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/56.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/56.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..fbed407046292ae4de12a796ca3331c3e6514d3e --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/56.exercises/solution.md" @@ -0,0 +1,112 @@ +# 验证二叉搜索树 + +
    +

    给你一个二叉树的根节点 root ,判断其是否是一个有效的二叉搜索树。

    + +

    有效 二叉搜索树定义如下:

    + +
      +
    • 节点的左子树只包含 小于 当前节点的数。
    • +
    • 节点的右子树只包含 大于 当前节点的数。
    • +
    • 所有左子树和右子树自身必须也是二叉搜索树。
    • +
    + +

     

    + +

    示例 1:

    + +
    输入:root = [2,1,3]
    +输出:true
    +
    + +

    示例 2:

    + +
    输入:root = [5,1,4,null,null,3,6]
    +输出:false
    +解释:根节点的值是 5 ,但是右子节点的值是 4 。
    +
    + +

     

    + +

    提示:

    + +
      +
    • 树中节点数目范围在[1, 104]
    • +
    • -231 <= Node.val <= 231 - 1
    • +
    +
    + +## template + +```java + +public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode(int x) { + val = x; + } +} + +class Solution { + public boolean isValidBST(TreeNode root) { + + if (root == null) + return true; + if (root.left == null && root.right == null) { + return true; + } + if (root.left != null) { + TreeNode cur = root.left; + while (cur.right != null) { + cur = cur.right; + } + if (cur.val >= root.val) { + return false; + } + } + if (root.right != null) { + TreeNode cur = root.right; + while (cur.left != null) { + cur = cur.left; + } + if (cur.val <= root.val) { + return false; + } + } + + boolean left = isValidBST(root.left); + boolean right = isValidBST(root.right); + + return left && right; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/57.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/57.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..ab4e72bca79ca7faaad6757ef5dbc9036586b403 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/57.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-3deeea16066945fa9f1876b4e2e0c4c2", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/57.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/57.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..bf3120337ef8ae81fc16f5081e8542c48d95bcd0 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/57.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "1141e850f19c483f848c80b72273ce8e" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/57.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/57.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..85d2e3daff22d05c6451e2842bdb8d6bf7ff153d --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/57.exercises/solution.md" @@ -0,0 +1,56 @@ +# 删除排序链表中的重复元素 II + +

    存在一个按升序排列的链表,给你这个链表的头节点 head ,请你删除链表中所有存在数字重复情况的节点,只保留原始链表中 没有重复出现 的数字。

    返回同样按升序排列的结果链表。

     

    示例 1:

    输入:head = [1,2,3,3,4,4,5]
    输出:
    [1,2,5]

    示例 2:

    输入:head = [1,1,1,2,3]
    输出:
    [2,3]

     

    提示:

    • 链表中节点数目在范围 [0, 300]
    • -100 <= Node.val <= 100
    • 题目数据保证链表已经按升序排列
    + +## template + +```java +public class ListNode { + int val; + ListNode next; + ListNode(int x) { val = x; } +} +class Solution { + public ListNode deleteDuplicates(ListNode head) { + if (head == null || head.next == null) { + return head; + } + ListNode next = head.next; + if (head.val == next.val) { + while (next != null && head.val == next.val) { + next = next.next; + } + head = deleteDuplicates(next); + } else { + head.next = deleteDuplicates(next); + } + return head; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/58.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/58.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..25d2c7b8709a5c401f3f8af6554d7181df4741e0 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/58.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-e3f77fd94e2844bba738c3b35436610f", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/58.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/58.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..6634033be603c07e9829d516c4290a157499c368 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/58.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "de4cd48a21f9425aa760e8d6786bc991" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/58.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/58.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..3ad16b8dc30cd8bd70019666c85de793a1e28a52 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/58.exercises/solution.md" @@ -0,0 +1,121 @@ +# Z 字形变换 + +
    +

    将一个给定字符串 s 根据给定的行数 numRows ,以从上往下、从左到右进行 Z 字形排列。

    + +

    比如输入字符串为 "PAYPALISHIRING" 行数为 3 时,排列如下:

    + +
    +    P   A   H   N
    +    A P L S I I G
    +    Y   I   R
    + +

    之后,你的输出需要从左往右逐行读取,产生出一个新的字符串,比如:"PAHNAPLSIIGYIR"

    + +

    请你实现这个将字符串进行指定行数变换的函数:

    + +
    string convert(string s, int numRows);
    + +

     

    + +

    示例 1:

    + +
    输入:s = "PAYPALISHIRING", numRows = 3
    +输出:"PAHNAPLSIIGYIR"
    +
    + 示例 2: + +
    输入:s = "PAYPALISHIRING", numRows = 4
    +输出:"PINALSIGYAHRPI"
    +解释:
    +P     I    N
    +A   L S  I G
    +Y A   H R
    +P     I
    +
    + +

    示例 3:

    + +
    输入:s = "A", numRows = 1
    +输出:"A"
    +
    + +

     

    + +

    提示:

    + +
      +
    • 1 <= s.length <= 1000
    • +
    • s 由英文字母(小写和大写)、',''.' 组成
    • +
    • 1 <= numRows <= 1000
    • +
    +
    + +## template + +```java +class Solution { + public String convert(String s, int numRows) { + if (numRows == 1) + return s; + int len = s.length(); + if (len <= numRows) + return s; + int cycle_len = 2 * numRows - 2; + int full_cycles = len / cycle_len; + int left = len % cycle_len; + StringBuilder r = new StringBuilder(); + int i; + for (i = 0; i < full_cycles; ++i) { + r.append(s.charAt(i * cycle_len)); + } + if (left > 0) { + r.append(s.charAt(i * cycle_len)); + } + for (i = 0; i < numRows - 2; ++i) { + int j; + for (j = 0; j < full_cycles; ++j) { + r.append(s.charAt(j * cycle_len + i + 1)); + r.append(s.charAt(j * cycle_len + i + 1 + cycle_len - 2 * (i + 1))); + } + if (left > 0) { + if (j * cycle_len + i + 1 < len) + r.append(s.charAt(j * cycle_len + i + 1)); + if (j * cycle_len + i + 1 + cycle_len - 2 * (i + 1) < len) + r.append(s.charAt(j * cycle_len + i + 1 + cycle_len - 2 * (i + 1))); + } + } + for (i = 0; i < full_cycles; ++i) + r.append(s.charAt(i * cycle_len + numRows - 1)); + if (left >= numRows) + r.append(s.charAt(i * cycle_len + numRows - 1)); + return r.toString(); + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/59.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/59.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..dd7807907e4f400c72e6d7da2d67549ac7a5e4ec --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/59.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-b6c537dc542c44ccb82de451ae8e053a", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/59.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/59.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..f5a267e8f9f393cd0420a6e0185e4847a78c664c --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/59.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "0259726a90354ee7be6c88bfd2790735" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/59.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/59.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..947a24c4432df70d49fdb99d25ab943ec5d8bfb0 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/59.exercises/solution.md" @@ -0,0 +1,75 @@ +# 组合总和 II + +

    给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。 +

    +

    candidates 中的每个数字在每个组合中只能使用一次。

    +

    说明:

    +
      +
    • 所有数字(包括目标数)都是正整数。
    • +
    • 解集不能包含重复的组合。 
    • +
    +

    示例 1:

    +
    输入: candidates = [10,1,2,7,6,1,5], target = 8,
    所求解集为:
    [[1, 7],[1, 2, 5],[2, 6],[1, 1, 6]]
    +

    示例 2:

    +
    输入: candidates = [2,5,2,1,2], target = 5,
    所求解集为:
    [[1,2,2],[5]]
    + +## template + +```java +class Solution { + public List> combinationSum2(int[] candidates, int target) { + Arrays.sort(candidates); + List> res = new ArrayList>(); + if (candidates.length == 0 || target < candidates[0]) + return res; + List tmp = new ArrayList(); + helper(candidates, target, 0, tmp, res); + return res; + } + + public void helper(int[] a, int target, int start, List tmp, List> res) { + if (target < 0) + return; + if (target == 0) { + res.add(new ArrayList(tmp)); + return; + } + for (int i = start; i < a.length; i++) { + tmp.add(a[i]); + int newtarget = target - a[i]; + helper(a, newtarget, i + 1, tmp, res); + tmp.remove(tmp.size() - 1); + if (newtarget <= 0) + break; + while (i + 1 < a.length && a[i] == a[i + 1])// 组合中有重复元素,不要重复开头 + i++; + } + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/6.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/6.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..221e9082ce5054d44af9a51218d1d5a228677e27 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/6.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-b1ec97a0ab0b46d2b20e6f1f9ad49cde", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/6.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/6.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..826e09d1e7de43c656f3bf88cb6a3cc27670a4af --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/6.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "32cd10aeb1d648f7a901e51b50b4a183" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/6.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/6.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..6fda3b8f2f64e8f8984f531ba61fa3e9c3b870f0 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/6.exercises/solution.md" @@ -0,0 +1,76 @@ +# 恢复二叉搜索树 + +

    给你二叉搜索树的根节点 root ,该树中的两个节点被错误地交换。请在不改变其结构的情况下,恢复这棵树。

    进阶:使用 O(n) 空间复杂度的解法很容易实现。你能想出一个只使用常数空间的解决方案吗?

     

    示例 1:

    输入:root = [1,3,null,null,2]
    输出:
    [3,1,null,null,2]
    解释:
    3 不能是 1 左孩子,因为 3 > 1 。交换 1 和 3 使二叉搜索树有效。

    示例 2:

    输入:root = [3,1,4,null,null,2]
    输出:
    [2,1,4,null,null,3]
    解释:
    2 不能在 3 的右子树中,因为 2 < 3 。交换 2 和 3 使二叉搜索树有效。

     

    提示:

    • 树上节点的数目在范围 [2, 1000]
    • -231 <= Node.val <= 231 - 1
    + +## template + +```java +public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode() { + } + + TreeNode(int val) { + this.val = val; + } + + TreeNode(int val, TreeNode left, TreeNode right) { + this.val = val; + this.left = left; + this.right = right; + } +} + +class Solution { + TreeNode t1, t2, pre; + + public void recoverTree(TreeNode root) { + inorder(root); + int temp = t1.val; + t1.val = t2.val; + t2.val = temp; + } + + public void inorder(TreeNode root) { + if (root == null) + return; + inorder(root.left); + if (pre != null && pre.val > root.val) { + if (t1 == null) + t1 = pre; + t2 = root; + } + pre = root; + inorder(root.right); + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/60.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/60.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..91e2a1d588f15ad1cd31f0462ed28f2422a66bad --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/60.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-754b296026254e769ddfa665d92eb725", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/60.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/60.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..2ba5130840f01cfc6cd15169b1784055aab6e5b8 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/60.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "63ebe12aca2a4b13853c42fd66b3cf98" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/60.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/60.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..6a41243329986efc80fc680df3225625adef411e --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/60.exercises/solution.md" @@ -0,0 +1,85 @@ +# 螺旋矩阵 II + +

    给你一个正整数 n ,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的 n x n 正方形矩阵 matrix

     

    示例 1:

    输入:n = 3
    输出:
    [[1,2,3],[8,9,4],[7,6,5]]

    示例 2:

    输入:n = 1
    输出:
    [[1]]

     

    提示:

    • 1 <= n <= 20
    + +## template + +```java +package LeetCode; + +public class GenerateMatrix { + public int[][] generateMatrix(int n) { + int[][] res = new int[n][n]; + if (n == 0) { + return res; + } + int left = 0; + int right = n - 1; + int up = 0; + int down = n - 1; + int i = 1; + while (i <= n * n) { + + for (int col = left; col <= right; col++) { + res[up][col] = i; + i++; + } + + up++; + if (i <= n * n) { + for (int j = up; j <= down; j++) { + res[j][right] = i; + i++; + } + right--; + } + if (i <= n * n) { + for (int j = right; j >= left; j--) { + res[down][j] = i; + i++; + } + down--; + } + if (i <= n * n) { + for (int j = down; j >= up; j--) { + res[j][left] = i; + i++; + } + left++; + } + } + return res; + } + + public static void main(String[] args) { + GenerateMatrix a = new GenerateMatrix(); + a.generateMatrix(3); + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/61.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/61.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..d8cc34a2c4a747dd9bc378f008082885d17bf1f1 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/61.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-6b75d0f0158045a89d048742ea85f67a", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/61.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/61.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..fe9a0ebf3d9a010636952eced327eba5913e00e8 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/61.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "29a8e04eaa4c4fba9ef373313f4af42d" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/61.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/61.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..7d092db76b33261565af78904a2348d3b8d1451b --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/61.exercises/solution.md" @@ -0,0 +1,56 @@ +# 两两交换链表中的节点 + +

    给定一个链表,两两交换其中相邻的节点,并返回交换后的链表。

    你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。

     

    示例 1:

    输入:head = [1,2,3,4]
    输出:
    [2,1,4,3]

    示例 2:

    输入:head = []
    输出:
    []

    示例 3:

    输入:head = [1]
    输出:
    [1]

     

    提示:

    • 链表中节点的数目在范围 [0, 100]
    • 0 <= Node.val <= 100

     

    进阶:你能在不修改链表节点值的情况下解决这个问题吗?(也就是说,仅修改节点本身。)

    + +## template + +```java +public class ListNode { + int val; + ListNode next; + ListNode(int x) { + val = x; + } +} +class Solution { + public ListNode swapPairs(ListNode head) { + ListNode list1 = new ListNode(0); + list1.next = head; + ListNode list2 = list1; + while (head != null && head.next != null) { + list2.next = head.next; + head.next = list2.next.next; + list2.next.next = head; + list2 = list2.next.next; + head = list2.next; + } + return list1.next; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/62.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/62.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..6895d914fe9af59d194168bd3bd83001befb9424 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/62.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-64748ebd5f0046ec8d27c97c7b358d2c", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/62.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/62.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..ab65951796fc69492e5767d3d7f75404967b226f --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/62.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "240f9bd549ae4f8ab9499be8b7fedf73" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/62.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/62.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..9393eee81a14aa491a4566e4eec2ea548a719b6f --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/62.exercises/solution.md" @@ -0,0 +1,56 @@ +# 颜色分类 + +

    给定一个包含红色、白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色、白色、蓝色顺序排列。

    此题中,我们使用整数 0、 12 分别表示红色、白色和蓝色。

       

      示例 1:

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

      示例 2:

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

      示例 3:

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

      示例 4:

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

       

      提示:

      • n == nums.length
      • 1 <= n <= 300
      • nums[i]012

       

      进阶:

      • 你可以不使用代码库中的排序函数来解决这道题吗?
      • 你能想出一个仅使用常数空间的一趟扫描算法吗?
      + +## template + +```java +class Solution { + public void sortColors(int[] nums) { + int low = 0, high = nums.length - 1; + int i = 0; + while (i <= high) { + if (nums[i] == 0) { + int tmp = nums[i]; + nums[i] = nums[low]; + nums[low] = tmp; + ++low; + ++i; + } else if (nums[i] == 1) { + ++i; + } else if (i <= high && nums[i] == 2) { + int tmp = nums[i]; + nums[i] = nums[high]; + nums[high] = tmp; + --high; + } + } + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/63.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/63.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..d5b439e4ba7390c1200498e0a2d3699a3383b70e --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/63.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-ef8a36976755404ba5027382502438cd", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/63.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/63.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..5e2d2c87e1627e27f808b8b0224c475127db0dfa --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/63.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "fc2d484b20204c4bb9fab81b4bb40789" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/63.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/63.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..ff9c5c408fc3733d30c9593ff5183ae8a12565fa --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/63.exercises/solution.md" @@ -0,0 +1,99 @@ +# 不同路径 II + +

      一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” )。

      +

      机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为“Finish”)。

      +

      现在考虑网格中有障碍物。那么从左上角到右下角将会有多少条不同的路径?

      +

      +

      网格中的障碍物和空位置分别用 10 来表示。

      +

       

      +

      示例 1:

      +
      输入:obstacleGrid = [[0,0,0],[0,1,0],[0,0,0]]
      输出:
      2
      解释:
      3x3 网格的正中间有一个障碍物。从左上角到右下角一共有 2 条不同的路径:
      1. 向右 -> 向右 -> 向下 -> 向下
      2. 向下 -> 向下 -> 向右 -> 向右
      +

      示例 2:

      +
      输入:obstacleGrid = [[0,1],[0,0]]
      输出:
      1
      +

       

      +

      提示:

      +
        +
      • m == obstacleGrid.length
      • +
      • n == obstacleGrid[i].length
      • +
      • 1 <= m, n <= 100
      • +
      • obstacleGrid[i][j]01
      • +
      + +## template + +```java +public class Solution { + public int uniquePathsWithObstacles(int[][] obstacleGrid) { + int m = obstacleGrid.length; + int n = obstacleGrid[0].length; + if (obstacleGrid[0][0] == 1) { + return 0; + } else if (m == 1 && n == 1) { + return 1; + } + int[][] paths = new int[m][n]; + for (int i = 0; i < m; ++i) { + if (obstacleGrid[i][0] == 1) { + while (i < m) { + paths[i][0] = 0; + ++i; + } + break; + } else { + paths[i][0] = 1; + } + } + for (int j = 1; j < n; ++j) { + if (obstacleGrid[0][j] == 1) { + while (j < n) { + paths[0][j] = 0; + ++j; + } + break; + } else { + paths[0][j] = 1; + } + } + for (int i = 1; i < m; ++i) + for (int j = 1; j < n; ++j) { + if (obstacleGrid[i][j] == 1) { + paths[i][j] = 0; + } else { + paths[i][j] = paths[i][j - 1] + paths[i - 1][j]; + } + } + return paths[m - 1][n - 1]; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/64.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/64.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..3c43da444f53af682485475afef791ad07844bb8 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/64.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-7a16ebcf4f2848a6ad48a39ae0ffa685", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/64.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/64.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..d6a152775e7a38312a5d14c8e579ecee48827737 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/64.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "4300c8ff4b9f43999c979626dd45b68c" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/64.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/64.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..9bfba34f63dad98d95b2df5d3427eecbc791f33b --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/64.exercises/solution.md" @@ -0,0 +1,104 @@ +# 有效的数独 + +

      请你判断一个 9x9 的数独是否有效。只需要 根据以下规则 ,验证已经填入的数字是否有效即可。

      +
        +
      1. 数字 1-9 在每一行只能出现一次。
      2. +
      3. 数字 1-9 在每一列只能出现一次。
      4. +
      5. 数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。(请参考示例图)
      6. +
      +

      数独部分空格内已填入了数字,空白格用 '.' 表示。

      +

      注意:

      +
        +
      • 一个有效的数独(部分已被填充)不一定是可解的。
      • +
      • 只需要根据以上规则,验证已经填入的数字是否有效即可。
      • +
      +

       

      +

      示例 1:

      +
      输入:board = 
      +    [["5","3",".",".","7",".",".",".","."]
      +    ,["6",".",".","1","9","5",".",".","."]
      +    ,[".","9","8",".",".",".",".","6","."]
      +    ,["8",".",".",".","6",".",".",".","3"]
      +    ,["4",".",".","8",".","3",".",".","1"]
      +    ,["7",".",".",".","2",".",".",".","6"]
      +    ,[".","6",".",".",".",".","2","8","."]
      +    ,[".",".",".","4","1","9",".",".","5"]
      +    ,[".",".",".",".","8",".",".","7","9"]]
      +输出:true
      +
      +

      示例 2:

      +
      输入:board = 
      +    [["8","3",".",".","7",".",".",".","."]
      +    ,["6",".",".","1","9","5",".",".","."]
      +    ,[".","9","8",".",".",".",".","6","."]
      +    ,["8",".",".",".","6",".",".",".","3"]
      +    ,["4",".",".","8",".","3",".",".","1"]
      +    ,["7",".",".",".","2",".",".",".","6"]
      +    ,[".","6",".",".",".",".","2","8","."]
      +    ,[".",".",".","4","1","9",".",".","5"]
      +    ,[".",".",".",".","8",".",".","7","9"]]
      +输出:false
      +解释:除了第一行的第一个数字从 5 改为 8 以外,空格内其他数字均与 示例1 相同。 但由于位于左上角的 3x3 宫内有两个 8 存在, 因此这个数独是无效的。
      +

       

      +

      提示:

      +
        +
      • board.length == 9
      • +
      • board[i].length == 9
      • +
      • board[i][j] 是一位数字或者 '.'
      • +
      + +## template + +```java +class Solution { + public boolean isValidSudoku(char[][] board) { + boolean[][] row = new boolean[9][9]; + boolean[][] col = new boolean[9][9]; + boolean[][] block = new boolean[9][9]; + for (int i = 0; i < 9; i++) { + for (int j = 0; j < 9; j++) { + if (board[i][j] != '.') { + int num = board[i][j] - '1'; + int blockIndex = i / 3 * 3 + j / 3; + if (row[i][num] || col[j][num] || block[blockIndex][num]) { + return false; + } else { + row[i][num] = true; + col[j][num] = true; + block[blockIndex][num] = true; + } + } + } + } + return true; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/7.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/7.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..f1113047f643eb7f5fec2beeafbdfb341f4350b2 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/7.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-a7a3d54d8f7e4d5e81b3ad6393ed66d2", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/7.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/7.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..fe1fb15d690a520af43420e74abf16fe235b72ca --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/7.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "e0a6902259b74415ab16c87e74a73e7e" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/7.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/7.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..edf3581f9c65ec1d9825c549deb36e8285ae9d40 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/7.exercises/solution.md" @@ -0,0 +1,54 @@ +# 子集 + +

      给你一个整数数组 nums ,数组中的元素 互不相同 。返回该数组所有可能的子集(幂集)。

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

       

      示例 1:

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

      示例 2:

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

       

      提示:

      • 1 <= nums.length <= 10
      • -10 <= nums[i] <= 10
      • nums 中的所有元素 互不相同
      + +## template + +```java +class Solution { + public List> subsets(int[] nums) { + List> res = new ArrayList>(); + List tmp = new ArrayList<>(); + res.add(tmp); + if (nums.length == 0) + return res; + helper(nums, 0, tmp, res); + return res; + } + + public void helper(int[] nums, int start, List tmp, List> res) { + for (int i = start; i < nums.length; i++) { + tmp.add(nums[i]); + helper(nums, i + 1, tmp, res); + res.add(new ArrayList(tmp)); + tmp.remove(tmp.size() - 1); + } + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/8.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/8.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..edbcc33ce4ea47765a36545a18c0146d1d7318b6 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/8.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-821b460a82644430b36bfe84140b1a99", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/8.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/8.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..4e565d9b239c554f6f6ef580febf8f5fff068897 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/8.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "b9bc30e44e1d4356bf483fcbd0ed9290" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/8.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/8.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..170cc8ffa22df807191e69ed83b16abf2ed8753e --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/8.exercises/solution.md" @@ -0,0 +1,70 @@ +# 不同路径 + +

      一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为 “Start” )。

      +

      机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为 “Finish” )。

      +

      问总共有多少条不同的路径?

      +

       

      +

      示例 1:

      +
      输入:m = 3, n = 7
      输出:
      28
      +

      示例 2:

      +
      输入:m = 3, n = 2
      输出:
      3
      解释:
      从左上角开始,总共有 3 条路径可以到达右下角。
      1. 向右 -> 向下 -> 向下
      2. 向下 -> 向下 -> 向右
      3. 向下 -> 向右 -> 向下
      +

      示例 3:

      +
      输入:m = 7, n = 3
      输出:
      28
      +

      示例 4:

      +
      输入:m = 3, n = 3
      输出:
      6
      +

       

      +

      提示:

      +
        +
      • 1 <= m, n <= 100
      • +
      • 题目数据保证答案小于等于 2 * 109
      • +
      + +## template + +```java +class Solution { + public int uniquePaths(int m, int n) { + int[][] route = new int[m][n]; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (i == 0 || j == 0) { + route[i][j] = 1; + } else { + route[i][j] = route[i - 1][j] + route[i][j - 1]; + + } + } + } + + return route[m - 1][n - 1]; + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/9.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/9.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..1f7735ee9539fd78689026fd4efbc9a3e50bfb21 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/9.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-9582193475ab454dbfe7d56d8903ec1b", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/9.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/9.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..87f3b73d2b1df8f78b1367b4a74eee4b7670a42a --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/9.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "758c317aa8db47d292c3ff614ed2be59" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/9.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/9.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..db087f25b9690f9d4107cf78240a15d6bdfa591e --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/9.exercises/solution.md" @@ -0,0 +1,81 @@ +# 电话号码的字母组合 + +

      给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。答案可以按 任意顺序 返回。

      给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。

       

      示例 1:

      输入:digits = "23"
      输出:
      ["ad","ae","af","bd","be","bf","cd","ce","cf"]

      示例 2:

      输入:digits = ""
      输出:
      []

      示例 3:

      输入:digits = "2"
      输出:
      ["a","b","c"]

       

      提示:

      • 0 <= digits.length <= 4
      • digits[i] 是范围 ['2', '9'] 的一个数字。
      + +## template + +```java +class Solution { + public List letterCombinations(String digits) { + Character[][] letters = { {}, {}, { 'a', 'b', 'c' }, { 'd', 'e', 'f' }, { 'g', 'h', 'i' }, { 'j', 'k', 'l' }, + { 'm', 'n', 'o' }, { 'p', 'q', 'r', 's' }, { 't', 'u', 'v' }, { 'w', 'x', 'y', 'z' }, }; + List> combinations = new ArrayList<>(); + for (int i = 0; i < digits.length(); i++) { + Character d = digits.charAt(i); + int index = Character.getNumericValue(d); + Character[] letter = letters[index]; + System.out.println(d); + if (i == 0) { + for (int j = 0; j < letter.length; j++) { + List c = new ArrayList<>(); + c.add(letter[j]); + combinations.add(c); + } + } else { + List> added = new ArrayList<>(); + for (int j = 0; j < combinations.size(); j++) { + List c = combinations.get(j); + List origin_c = new ArrayList<>(c); + for (int k = 0; k < letter.length; k++) { + Character l = letter[k]; + if (k == 0) { + c.add(l); + } else { + List new_c = new ArrayList<>(origin_c); + new_c.add(l); + added.add(new_c); + } + } + } + combinations.addAll(added); + } + } + List output = new ArrayList<>(); + for (int i = 0; i < combinations.size(); i++) { + List c = combinations.get(i); + StringBuilder sb = new StringBuilder(); + for (Character l : c) { + sb.append(l); + } + output.add(sb.toString()); + } + return output; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/10.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/10.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..9b47b6529e1bc8a1291ba0b5b82640c61d893bbd --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/10.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-714fbc491c1144fbab2cf7caf56a48cc", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/10.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/10.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..c0e0093f08e24f93b79bf889764bc79c5b27b0ba --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/10.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "548ac23c411244da8bc1c7fcd2effff2" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/10.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/3.python/10.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..e0a64873fe0e7ff2441c726827ea89f44f727ca2 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/10.exercises/solution.md" @@ -0,0 +1,174 @@ +# 字符串转换整数 (atoi) + +
      +

      请你来实现一个 myAtoi(string s) 函数,使其能将字符串转换成一个 32 位有符号整数(类似 C/C++ 中的 atoi 函数)。

      + +

      函数 myAtoi(string s) 的算法如下:

      + +
        +
      • 读入字符串并丢弃无用的前导空格
      • +
      • 检查下一个字符(假设还未到字符末尾)为正还是负号,读取该字符(如果有)。 确定最终结果是负数还是正数。 如果两者都不存在,则假定结果为正。
      • +
      • 读入下一个字符,直到到达下一个非数字字符或到达输入的结尾。字符串的其余部分将被忽略。
      • +
      • 将前面步骤读入的这些数字转换为整数(即,"123" -> 123, "0032" -> 32)。如果没有读入数字,则整数为 0 。必要时更改符号(从步骤 2 开始)。
      • +
      • 如果整数数超过 32 位有符号整数范围 [−231,  231 − 1] + ,需要截断这个整数,使其保持在这个范围内。具体来说,小于 −231 的整数应该被固定为 −231 ,大于 + 231 − 1 的整数应该被固定为 231 − 1 。 +
      • +
      • 返回整数作为最终结果。
      • +
      + +

      注意:

      + +
        +
      • 本题中的空白字符只包括空格字符 ' '
      • +
      • 除前导空格或数字后的其余字符串外,请勿忽略 任何其他字符。
      • +
      + +

       

      + +

      示例 1:

      + +
      输入:s = "42"
      +输出:42
      +解释:加粗的字符串为已经读入的字符,插入符号是当前读取的字符。
      +第 1 步:"42"(当前没有读入字符,因为没有前导空格)
      +		 ^
      +第 2 步:"42"(当前没有读入字符,因为这里不存在 '-' 或者 '+')
      +		 ^
      +第 3 步:"42"(读入 "42")
      +		   ^
      +解析得到整数 42 。
      +由于 "42" 在范围 [-231, 231 - 1] 内,最终结果为 42 。
      + +

      示例 2:

      + +
      输入:s = "   -42"
      +输出:-42
      +解释:
      +第 1 步:"   -42"(读入前导空格,但忽视掉)
      +			^
      +第 2 步:"   -42"(读入 '-' 字符,所以结果应该是负数)
      +			 ^
      +第 3 步:"   -42"(读入 "42")
      +			   ^
      +解析得到整数 -42 。
      +由于 "-42" 在范围 [-231, 231 - 1] 内,最终结果为 -42 。
      +
      + +

      示例 3:

      + +
      输入:s = "4193 with words"
      +输出:4193
      +解释:
      +第 1 步:"4193 with words"(当前没有读入字符,因为没有前导空格)
      +		 ^
      +第 2 步:"4193 with words"(当前没有读入字符,因为这里不存在 '-' 或者 '+')
      +		 ^
      +第 3 步:"4193 with words"(读入 "4193";由于下一个字符不是一个数字,所以读入停止)
      +		     ^
      +解析得到整数 4193 。
      +由于 "4193" 在范围 [-231, 231 - 1] 内,最终结果为 4193 。
      +
      + +

      示例 4:

      + +
      输入:s = "words and 987"
      +输出:0
      +解释:
      +第 1 步:"words and 987"(当前没有读入字符,因为没有前导空格)
      +		 ^
      +第 2 步:"words and 987"(当前没有读入字符,因为这里不存在 '-' 或者 '+')
      +		 ^
      +第 3 步:"words and 987"(由于当前字符 'w' 不是一个数字,所以读入停止)
      +		 ^
      +解析得到整数 0 ,因为没有读入任何数字。
      +由于 0 在范围 [-231, 231 - 1] 内,最终结果为 0 。
      + +

      示例 5:

      + +
      输入:s = "-91283472332"
      +输出:-2147483648
      +解释:
      +第 1 步:"-91283472332"(当前没有读入字符,因为没有前导空格)
      +		 ^
      +第 2 步:"-91283472332"(读入 '-' 字符,所以结果应该是负数)
      +		  ^
      +第 3 步:"-91283472332"(读入 "91283472332")
      +		            ^
      +解析得到整数 -91283472332 。
      +由于 -91283472332 小于范围 [-231, 231 - 1] 的下界,最终结果被截断为 -231 = -2147483648 。
      + +

       

      + +

      提示:

      + +
        +
      • 0 <= s.length <= 200
      • +
      • s 由英文字母(大写和小写)、数字(0-9)、' ''+''-' 和 + '.' 组成 +
      • +
      +
      + +## template + +```python +class Solution: + def myAtoi(self, s: str) -> int: + y = 0 + i = 0 + w = False + sign = False + ints = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] + while i < len(s): + c = s[i] + isSign = False + if w == False and c != ' ': + w = True + if c == '-': + sign = True + isSign = True + if c == '+': + isSign = True + if w and not isSign: + try: + v = ints.index(c) + y = y*10+v + except: + break + i += 1 + offset = 0 + if y > 2147483647: + y = 2147483647 + offset = 1 + return -(y+offset) if sign else y +# %% +s = Solution() +print(s.myAtoi(s = "42")) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/11.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/11.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..3ed9195f74938879c8d3b12531bb8ff3aa0296c5 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/11.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-3c404ea0347e435f8dbe9c32ebf245dc", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/11.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/11.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..4680d01cdddcc4e41b27a0278ad0d44114bb809e --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/11.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "a47185fd4f9e40e0a915853ac7d278c6" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/11.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/3.python/11.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..161b2eff3975b9f82e92a9f58a25e3eca667af87 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/11.exercises/solution.md" @@ -0,0 +1,76 @@ +# 不同的二叉搜索树 + +
      +

      给你一个整数 n ,求恰由 n 个节点组成且节点值从 1n 互不相同的 二叉搜索树 + 有多少种?返回满足题意的二叉搜索树的种数。

      + +

       

      + +

      示例 1:

      + +
      输入:n = 3
      +输出:5
      +    
      + +

      示例 2:

      + +
      输入:n = 1
      +输出:1
      +    
      + +

       

      + +

      提示:

      + +
        +
      • 1 <= n <= 19
      • +
      +
      + +## template + +```python +class Solution(object): + def numTrees(self, n): + """ + :type n: int + :rtype: int + """ + dp = [0] * (n + 1) + dp[0] = 1 + dp[1] = 1 + for level in range(2, n + 1): + for root in range(1, level + 1): + dp[level] += dp[level - root] * dp[root - 1] + return dp[n] +# %% +s = Solution() +print(s.numTrees(n = 3)) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/12.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/12.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..44c639a74331f4cbb9eefea8d60c9d43ec63b483 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/12.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-f35dcba4dd134a1ba1656141f1ba8f7b", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/12.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/12.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..b7cb2459db7a2bf4ad90c2f2377eb9649b0c1875 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/12.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "ffe99edc154642f8a48d998f843adbdc" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/12.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/3.python/12.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..2a09421a4d6d410d58458c5dfad871385720f979 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/12.exercises/solution.md" @@ -0,0 +1,118 @@ +# 插入区间 + +

      给你一个 无重叠的按照区间起始端点排序的区间列表。

      +

      在列表中插入一个新的区间,你需要确保列表中的区间仍然有序且不重叠(如果有必要的话,可以合并区间)。

      +

       

      +

      示例 1:

      +
      输入:intervals = [[1,3],[6,9]], newInterval = [2,5]
      输出:
      [[1,5],[6,9]]
      +

      示例 2:

      +
      输入:intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8]
      输出:
      [[1,2],[3,10],[12,16]]
      解释:
      这是因为新的区间 [4,8] 与 [3,5],[6,7],[8,10] 重叠。
      +

      示例 3:

      +
      输入:intervals = [], newInterval = [5,7]
      输出:
      [[5,7]]
      +

      示例 4:

      +
      输入:intervals = [[1,5]], newInterval = [2,3]
      输出:
      [[1,5]]
      +

      示例 5:

      +
      输入:intervals = [[1,5]], newInterval = [2,7]
      输出:
      [[1,7]]
      +

       

      +

      提示:

      +
        +
      • 0 <= intervals.length <= 104
      • +
      • intervals[i].length == 2
      • +
      • 0 <= intervals[i][0] <= intervals[i][1] <= 105
      • +
      • intervals 根据 intervals[i][0]升序 排列
      • +
      • newInterval.length == 2
      • +
      • 0 <= newInterval[0] <= newInterval[1] <= 105
      • +
      + +## template + +```python +class Interval(object): + def __init__(self, s=0, e=0): + self.start = s + self.end = e +class Solution(object): + def list2interval(self, list_interval): + ret = [] + for i in list_interval: + interval = Interval(i[0], i[1]) + ret.append(interval) + return ret + def interval2list(self, interval): + ret = [] + x = [0,0] + for i in interval: + x[0] = i.start + x[1] = i.end + ret.append(x) + x = [0,0] + return ret + def insert(self, intervals, newInterval): + """ + :type intervals: List[Interval] + :type newInterval: Interval + :rtype: List[Interval] + """ + if intervals is None or len(intervals) == 0: + return [newInterval] + intervals = self.list2interval(intervals) + newInterval = Interval(newInterval[0], newInterval[1]) + intervals.sort(key=lambda x:x.start) + pos = 0 + while pos < len(intervals): + if newInterval.end < intervals[pos].start: + intervals.insert(pos, newInterval) + intervals = self.interval2list(intervals) + return intervals + if self.check_overlap(intervals[pos], newInterval): + temp = intervals.pop(pos) + newInterval = self.merge_intervals(temp, newInterval) + else: + pos += 1 + if len(intervals) == 0 or pos == len(intervals): + intervals.append(newInterval) + intervals = self.interval2list(intervals) + return intervals + def check_overlap(self, curr_int, new_int): + if curr_int.start <= new_int.start: + if curr_int.end > new_int.start: + return True + else: + if curr_int.start <= new_int.end: + return True + return False + def merge_intervals(self, int1, int2): + temp_int = Interval() + temp_int.start = min([int1.start, int2.start]) + temp_int.end = max([int1.end, int2.end]) + return temp_int +# %% +s = Solution() +print(s.insert(intervals = [[1,3],[6,9]], newInterval = [2,5])) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/13.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/13.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..d5ac0f9d4dfdb69af156bb1805505ecde07c7bd6 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/13.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-55487be880fa4b4193069f58592788b1", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/13.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/13.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..cbc52d04bf7ece81e51b4a9aa5420041a2433790 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/13.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "a1e145b77da24f9f9676b85c4e551c1f" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/13.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/3.python/13.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..26a00e97b5b4b89c941296b8ffe614999fa542ff --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/13.exercises/solution.md" @@ -0,0 +1,78 @@ +# 四数之和 + +

      给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满足条件且不重复的四元组。

      注意:答案中不可以包含重复的四元组。

       

      示例 1:

      输入:nums = [1,0,-1,0,-2,2], target = 0
      输出:
      [[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]

      示例 2:

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

       

      提示:

      • 0 <= nums.length <= 200
      • -109 <= nums[i] <= 109
      • -109 <= target <= 109
      + +## template + +```python +class Solution(object): + def fourSum(self, nums, target): + """ + :type nums: List[int] + :type target: int + :rtype: List[List[int]] + """ + nums.sort() + results = [] + N = len(nums) + i = 0 + while i < N-3: + if i > 0 and nums[i] == nums[i-1]: + i += 1 + continue + j = i+1 + while j < N-2: + if j > i+1 and nums[j] == nums[j-1]: + j += 1 + continue + k = j+1 + l = N-1 + while k < l: + if k > j+1 and nums[k] == nums[k-1]: + k += 1 + continue + while k < l and (target - nums[i] - nums[j] - nums[k] - nums[l]) < 0: + l -= 1 + if k >= l: + break + if target == nums[i] + nums[j] + nums[k] + nums[l]: + results.append([ + nums[i], + nums[j], + nums[k], + nums[l] + ]) + k += 1 + j += 1 + i += 1 + return results +# %% +s = Solution() +print(s.fourSum(nums = [1,0,-1,0,-2,2], target = 0)) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/14.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/14.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..ac9d5a31069f707382f924d9dbc8a517144206e6 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/14.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-0af96bbfeccf4f22b45b04bb08ca41ad", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/14.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/14.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..326945bdd48d7187c0a013421405bdbd8b09e5e1 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/14.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "830147197d464e2a93437eb51808d97a" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/14.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/3.python/14.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..f947c89be671b8c6e0aea4ba276f1239070d32a4 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/14.exercises/solution.md" @@ -0,0 +1,121 @@ +# 外观数列 + +
      +

      给定一个正整数 n ,输出外观数列的第 n 项。

      + +

      「外观数列」是一个整数序列,从数字 1 开始,序列中的每一项都是对前一项的描述。

      + +

      你可以将其视作是由递归公式定义的数字字符串序列:

      + +
        +
      • countAndSay(1) = "1"
      • +
      • countAndSay(n) 是对 countAndSay(n-1) 的描述,然后转换成另一个数字字符串。
      • +
      + +

      前五项如下:

      + +
      +    1.     1
      +    2.     11
      +    3.     21
      +    4.     1211
      +    5.     111221
      +    第一项是数字 1 
      +    描述前一项,这个数是 1 即 “ 一 个 1 ”,记作 "11"
      +    描述前一项,这个数是 11 即 “ 二 个 1 ” ,记作 "21"
      +    描述前一项,这个数是 21 即 “ 一 个 2 + 一 个 1 ” ,记作 "1211"
      +    描述前一项,这个数是 1211 即 “ 一 个 1 + 一 个 2 + 二 个 1 ” ,记作 "111221"
      +    
      + +

      描述 一个数字字符串,首先要将字符串分割为 最小 数量的组,每个组都由连续的最多 相同字符 + 组成。然后对于每个组,先描述字符的数量,然后描述字符,形成一个描述组。要将描述转换为数字字符串,先将每组中的字符数量用数字替换,再将所有描述组连接起来。

      + +

      例如,数字字符串 "3322251" 的描述如下图:

      + +
        +
      + +

       

      + +

      示例 1:

      + +
      输入:n = 1
      +输出:"1"
      +解释:这是一个基本样例。
      +
      + +

      示例 2:

      + +
      输入:n = 4
      +输出:"1211"
      +解释:
      +countAndSay(1) = "1"
      +countAndSay(2) = 读 "1" = 一 个 1 = "11"
      +countAndSay(3) = 读 "11" = 二 个 1 = "21"
      +countAndSay(4) = 读 "21" = 一 个 2 + 一 个 1 = "12" + "11" = "1211"
      +
      + +

       

      + +

      提示:

      + +
        +
      • 1 <= n <= 30
      • +
      +
      + +## template + +```python +class Solution: + def countAndSay(self, n): + if n == 1: + return '1' + x = '1' + while n > 1: + x = self.count(x) + n -= 1 + return x + def count(self, x): + m = list(x) + res = [] + m.append(None) + i , j = 0 , 0 + while i < len(m) - 1: + j += 1 + if m[j] != m[i]: + res += [j - i, m[i]] + i = j + return ''.join(str(s) for s in res) +# %% +s = Solution() +print(s.countAndSay(n = 4)) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/15.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/15.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..1d07d85b5e10dbd9110b550a384631f6885000fa --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/15.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-10a61006159d4be380e71ffb67e5b38f", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/15.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/15.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..54f616d245f4af12334c7461133a712e3e0d769c --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/15.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "06bbdf33dc3f41d38966c279ce795bed" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/15.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/3.python/15.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..e21839126b975c4009c27ca525b9e792c4a2848c --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/15.exercises/solution.md" @@ -0,0 +1,94 @@ +# 旋转链表 + +

      给你一个链表的头节点 head ,旋转链表,将链表每个节点向右移动 k 个位置。

       

      示例 1:

      输入:head = [1,2,3,4,5], k = 2
      输出:
      [4,5,1,2,3]

      示例 2:

      输入:head = [0,1,2], k = 4
      输出:
      [2,0,1]

       

      提示:

      • 链表中节点的数目在范围 [0, 500]
      • -100 <= Node.val <= 100
      • 0 <= k <= 2 * 109
      + +## template + +```python +class ListNode(object): + def __init__(self, x): + self.val = x + self.next = None +class LinkList: + def __init__(self): + self.head=None + def initList(self, data): + self.head = ListNode(data[0]) + r=self.head + p = self.head + for i in data[1:]: + node = ListNode(i) + p.next = node + p = p.next + return r + def convert_list(self,head): + ret = [] + if head == None: + return + node = head + while node != None: + ret.append(node.val) + node = node.next + return ret +class Solution(object): + def rotateRight(self, head, k): + """ + :type head: ListNode + :type k: int + :rtype: ListNode + """ + if not head or k == 0: + return head + slow = fast = head + length = 1 + while k and fast.next: + fast = fast.next + length += 1 + k -= 1 + if k != 0: + k = (k + length - 1) % length + return self.rotateRight(head, k) + else: + while fast.next: + fast = fast.next + slow = slow.next + return self.rotate(head, fast, slow) + def rotate(self, head, fast, slow): + fast.next = head + head = slow.next + slow.next = None + return head +# %% +l = LinkList() +list1 = [0,1,2] +k = 4 +l1 = l.initList(list1) +s = Solution() +print(l.convert_list(s.rotateRight(l1, k))) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/16.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/16.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..c956455ea2acd70a46d5fefdcdc99c8a2c5b7ae5 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/16.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-eab307fa207b4d22ad5f58889e30f835", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/16.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/16.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..bbed258285db381ab3129c2ab9b182a9aa308fd8 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/16.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "f60804dd34624cbbb1d166c174ddca6d" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/16.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/3.python/16.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..98cb99e963964f9e5ac9cb37e0865ebc0b7d73e6 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/16.exercises/solution.md" @@ -0,0 +1,54 @@ +# 组合 + +

      给定两个整数 nk,返回 1 ... n 中所有可能的 k 个数的组合。

      +

      示例:

      +
      输入: n = 4, k = 2
      输出:
      [[2,4],[3,4],[2,3],[1,2],[1,3],[1,4],]
      + +## template + +```python +class Solution(object): + def combine(self, n, k): + res = [] + self.get_combine(res, [], n, k, 1) + return res + def get_combine(self, res, prefix, n, k, start): + if k == 0: + res.append(list(prefix)) + elif start <= n: + prefix.append(start) + self.get_combine(res, prefix, + n, k - 1, start + 1) + prefix.pop() + self.get_combine(res, prefix, + n, k, start + 1) +if __name__ == "__main__": + s = Solution() + print (s.combine(4, 2)) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/17.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/17.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..ba15b0ec4ad8c4c72cd95a0289a590fec402e3d9 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/17.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-eee5eb4d73074133a09df804ea5858b7", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/17.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/17.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..8decd3ee6d16948ffb44445cbd647faf3d1a1fe9 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/17.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "3a11758c49414d6189716a398293e3fa" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/17.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/3.python/17.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..3accfb0e698f4521f2f512cee3ab4e3893061016 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/17.exercises/solution.md" @@ -0,0 +1,59 @@ +# 括号生成 + +

      数字 n 代表生成括号的对数,请你设计一个函数,用于能够生成所有可能的并且 有效的 括号组合。

       

      示例 1:

      输入:n = 3
      输出:
      ["((()))","(()())","(())()","()(())","()()()"]

      示例 2:

      输入:n = 1
      输出:
      ["()"]

       

      提示:

      • 1 <= n <= 8
      + +## template + +```python +from typing import List +class Solution: + def generateParenthesis(self, n: int) -> List[str]: + def gen(p, lc, rc, r, n): + if lc > n: + return + if lc == n and rc == n: + r.append(''.join(p)) + p.append('(') + lc += 1 + gen(p, lc, rc, r, n) + p.pop() + lc -= 1 + if lc > rc: + p.append(')') + rc += 1 + gen(p, lc, rc, r, n) + p.pop() + rc -= 1 + results = [] + gen([], 0, 0, results, n) + return results +# %% +s = Solution() +print(s.generateParenthesis(n = 3)) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/18.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/18.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..0b4a911b7676ce8ebbc16558688b165535de18b3 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/18.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-677abb7a965b4620bcb41d968fbc67b4", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/18.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/18.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..5769f2622c7b2ca409f3a6227a70a38f44d1e7e0 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/18.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "fd951d0fa9b44f0ba2267fabfb326bce" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/18.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/3.python/18.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..744918da7a03fd1072badea2911416bbc7cb1bc6 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/18.exercises/solution.md" @@ -0,0 +1,52 @@ +# 旋转图像 + +

      给定一个 × n 的二维矩阵 matrix 表示一个图像。请你将图像顺时针旋转 90 度。

      你必须在 原地 旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要 使用另一个矩阵来旋转图像。

       

      示例 1:

      输入:matrix = [[1,2,3],[4,5,6],[7,8,9]]
      输出:
      [[7,4,1],[8,5,2],[9,6,3]]

      示例 2:

      输入:matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]
      输出:
      [[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]

      示例 3:

      输入:matrix = [[1]]
      输出:
      [[1]]

      示例 4:

      输入:matrix = [[1,2],[3,4]]
      输出:
      [[3,1],[4,2]]

       

      提示:

      • matrix.length == n
      • matrix[i].length == n
      • 1 <= n <= 20
      • -1000 <= matrix[i][j] <= 1000
      + +## template + +```python +class Solution(object): + def rotate(self, matrix): + if matrix is None or len(matrix) == 1: + return + ls = len(matrix) + for i in range(int(ls / 2)): + begin, end = i, ls - 1 - i + for k in range(ls - 2 * i - 1): + temp = matrix[end - k][begin] + matrix[end - k][begin] = matrix[end][end - k] + matrix[end][end - k] = matrix[begin + k][end] + matrix[begin + k][end] = matrix[begin][begin + k] + matrix[begin][begin + k] = temp + return matrix +if __name__ == '__main__': + s = Solution() + print(s.rotate([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]])) + print(s.rotate( [[1,2],[3,4]])) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/19.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/19.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..db5862400c30640a88f99f35f8e7b2063e3b9e4c --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/19.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-2ef84b3b69d24faf8a4723641a5248ea", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/19.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/19.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..ec0f2753ca516d177805388dea5e0f2e76c5e731 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/19.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "85446bec457947b2979d7f7cee8b9381" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/19.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/3.python/19.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..a7a762d86dd04cc71c4942a1a8382dc505aeb39b --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/19.exercises/solution.md" @@ -0,0 +1,53 @@ +# 盛最多水的容器 + +

      给你 n 个非负整数 a1,a2,...,an每个数代表坐标中的一个点 (i, ai) 。在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai)(i, 0) 。找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。

      说明:你不能倾斜容器。

       

      示例 1:

      输入:[1,8,6,2,5,4,8,3,7]
      输出:
      49
      解释:
      图中垂直线代表输入数组 [1,8,6,2,5,4,8,3,7]。在此情况下,容器能够容纳水(表示为蓝色部分)的最大值为 49。

      示例 2:

      输入:height = [1,1]
      输出:
      1

      示例 3:

      输入:height = [4,3,2,1,4]
      输出:
      16

      示例 4:

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

       

      提示:

      • n = height.length
      • 2 <= n <= 3 * 104
      • 0 <= height[i] <= 3 * 104
      + +## template + +```python +from typing import List +class Solution: + def maxArea(self, height: List[int]) -> int: + N = len(height) + i = 0 + j = N-1 + max_area = 0 + while i < j: + c = (j-i)*min(height[i], height[j]) + if c > max_area: + max_area = c + if height[i] > height[j]: + j -= 1 + else: + i += 1 + return max_area +# %% +s = Solution() +print(s.maxArea([1,8,6,2,5,4,8,3,7])) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/20.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/20.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..febf05aae91bd68d902b67199847707752d7ae10 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/20.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-d7dc348c424a4abab7679a8ac34e5ba6", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/20.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/20.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..66da978348a4ec9afec77b0aea68112da44fd439 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/20.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "0c5300e4347c44c3b3b15eacf81b17ee" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/20.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/3.python/20.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..a9f09e16d14a99239d756c45df253c79aff9bb8e --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/20.exercises/solution.md" @@ -0,0 +1,64 @@ +# 复原 IP 地址 + +

      给定一个只包含数字的字符串,用以表示一个 IP 地址,返回所有可能从 s 获得的 有效 IP 地址 。你可以按任何顺序返回答案。

      有效 IP 地址 正好由四个整数(每个整数位于 0 到 255 之间组成,且不能含有前导 0),整数之间用 '.' 分隔。

      例如:"0.1.2.201" 和 "192.168.1.1" 是 有效 IP 地址,但是 "0.011.255.245"、"192.168.1.312" 和 "192.168@1.1" 是 无效 IP 地址。

       

      示例 1:

      输入:s = "25525511135"
      输出:
      ["255.255.11.135","255.255.111.35"]

      示例 2:

      输入:s = "0000"
      输出:
      ["0.0.0.0"]

      示例 3:

      输入:s = "1111"
      输出:
      ["1.1.1.1"]

      示例 4:

      输入:s = "010010"
      输出:
      ["0.10.0.10","0.100.1.0"]

      示例 5:

      输入:s = "101023"
      输出:
      ["1.0.10.23","1.0.102.3","10.1.0.23","10.10.2.3","101.0.2.3"]

       

      提示:

      • 0 <= s.length <= 3000
      • s 仅由数字组成
      + +## template + +```python +class Solution(object): + def restoreIpAddresses(self, s): + ls = len(s) + if ls == 0 or ls > 12: + return [] + res = [] + for i in range(1, 4): + for j in range(1, 4): + for k in range(1, 4): + m = ls - i - j - k + if m > 0 and m <= 3: + add1 = s[0:i] + add2 = s[i:i + j] + add3 = s[i + j:i + j + k] + add4 = s[i + j + k:] + if self.isValid(add1) and self.isValid(add2) and \ + self.isValid(add3) and self.isValid(add4): + res.append(add1 + '.' + add2 + '.' + add3 + '.' + add4) + return res + def isValid(self, add): + if len(add) == 1: + return True + if add[0] == '0': + return False + if int(add) <= 255: + return True + return False +if __name__ == '__main__': + s = Solution() + print (s.restoreIpAddresses('25525511135')) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/21.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/21.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..d51686e774de167028f6138f31dbcc577f1daad0 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/21.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-59713affcfef4a7ca64e4fde22c8f3ea", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/21.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/21.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..d3a74ecc5ff66e027d57ca77a7f2dc4b79275d1a --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/21.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "8e1f3dd8bb234abd8a9335a93d8fdb87" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/21.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/3.python/21.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..1f446b62ee927518ecd94542c667945e6d032a3b --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/21.exercises/solution.md" @@ -0,0 +1,55 @@ +# 格雷编码 + +

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

      +

      给定一个代表编码总位数的非负整数 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]。
      + +## template + +```python +class Solution(object): + def grayCode(self, n): + """ + :type n: int + :rtype: List[int] + """ + res = [0] + for i in range(n): + for j in reversed(range(len(res))): + res.append(res[j] + (1 << i)) + return res +if __name__ == "__main__": + s = Solution() + print (s.grayCode(2)) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/22.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/22.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..543101585a3ce4db780d0feb61e1c89f01512430 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/22.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-4074f2ced3fb4d63ac7db2b01877f645", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/22.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/22.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..a097939a7abca0640027614ad82ded6dc44f11bc --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/22.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "563ea3cebb984e49a7a626d2848d831b" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/22.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/3.python/22.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..76dd0d86a73d40866f819fc779498dbb96b5eb61 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/22.exercises/solution.md" @@ -0,0 +1,60 @@ +# 下一个排列 + +

      实现获取 下一个排列 的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列。

      如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列)。

      必须 原地 修改,只允许使用额外常数空间。

       

      示例 1:

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

      示例 2:

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

      示例 3:

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

      示例 4:

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

       

      提示:

      • 1 <= nums.length <= 100
      • 0 <= nums[i] <= 100
      + +## template + +```python +class Solution(object): + def nextPermutation(self, nums): + ls = len(nums) + if ls <= 1: + return + pair = [] + for i in range(ls): + for j in range(i + 1, ls): + if nums[i] < nums[j]: + pair.append([i,j]) + pos = 0 + if len(pair) > 0: + self.swap(nums, pair[-1][0], pair[-1][1]) + pos = pair[-1][0] + 1 + for i in range(pos, ls): + for j in range(i + 1, ls): + if nums[i] > nums[j]: + self.swap(nums, i, j) + return nums + def swap(self, nums, index1, index2): + if index1 == index2: + return + nums[index1], nums[index2] = nums[index2], nums[index1] +# %% +s = Solution() +print(s.nextPermutation(nums = [1,2,3])) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/23.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/23.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..63d7e2f7e5bb51bb1bf32eb8e1d916b8e8d61d13 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/23.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-d9a9760518e1472895536c6599a71202", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/23.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/23.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..0040a711b465f731fdb373d342b6707cc07965a0 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/23.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "995f9efcfdd94419b155a684c72a7b39" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/23.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/3.python/23.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..58ac8c4cabd16d02800d2ddba4742e30469f769c --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/23.exercises/solution.md" @@ -0,0 +1,52 @@ +# 子集 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
      + +## template + +```python +class Solution(object): + def subsetsWithDup(self, nums): + nums.sort() + res = [[]] + begin = 0 + for index in range(len(nums)): + if index == 0 or nums[index] != nums[index - 1]: + begin = 0 + size = len(res) + for j in range(begin, size): + curr = list(res[j]) + curr.append(nums[index]) + res.append(curr) + begin = size + return res +# %% +s = Solution() +print(s.subsetsWithDup(nums = [1,2,2])) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/24.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/24.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..53b24ad44cb2cad9be404b1c3830bb8f1f332b94 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/24.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-d5f3196452154a6b9621f52a3a813ffc", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/24.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/24.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..0c4fe8e78474462b2deac6724fc9ad3d1e0ddc1a --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/24.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "63c27de25130410185dc96d0540fd02a" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/24.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/3.python/24.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..10dcbe12b0e5ae0369f084a684d9483bb016e6dd --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/24.exercises/solution.md" @@ -0,0 +1,55 @@ +# 简化路径 + +

      给你一个字符串 path ,表示指向某一文件或目录的 Unix 风格 绝对路径 (以 '/' 开头),请你将其转化为更加简洁的规范路径。

      在 Unix 风格的文件系统中,一个点(.)表示当前目录本身;此外,两个点 (..) 表示将目录切换到上一级(指向父目录);两者都可以是复杂相对路径的组成部分。任意多个连续的斜杠(即,'//')都被视为单个斜杠 '/' 。 对于此问题,任何其他格式的点(例如,'...')均被视为文件/目录名称。

      请注意,返回的 规范路径 必须遵循下述格式:

      • 始终以斜杠 '/' 开头。
      • 两个目录名之间必须只有一个斜杠 '/'
      • 最后一个目录名(如果存在)不能 '/' 结尾。
      • 此外,路径仅包含从根目录到目标文件或目录的路径上的目录(即,不含 '.''..')。

      返回简化后得到的 规范路径

       

      示例 1:

      输入:path = "/home/"
      输出:
      "/home"
      解释:
      注意,最后一个目录名后面没有斜杠。

      示例 2:

      输入:path = "/../"
      输出:
      "/"
      解释:
      从根目录向上一级是不可行的,因为根目录是你可以到达的最高级。

      示例 3:

      输入:path = "/home//foo/"
      输出:
      "/home/foo"
      解释:
      在规范路径中,多个连续斜杠需要用一个斜杠替换。

      示例 4:

      输入:path = "/a/./b/../../c/"
      输出:
      "/c"

       

      提示:

      • 1 <= path.length <= 3000
      • path 由英文字母,数字,'.''/''_' 组成。
      • path 是一个有效的 Unix 风格绝对路径。
      + +## template + +```python +class Solution(object): + def simplifyPath(self, path): + """ + :type path: str + :rtype: str + """ + result = [] + plist = path.split('/') + for pos in plist: + if pos: + if pos == '..': + try: + result.pop() + except: + result = [] + elif pos != '.': + result.append(pos) + return '/'+'/'.join(result) +# %% +s = Solution() +print(s.simplifyPath(path = "/home/")) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/25.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/25.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..43aa58f3a64547b2b135779370a44321f9373d52 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/25.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-edf3e1ba38a84ec18d99ed5bd318e058", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/25.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/25.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..6417a7c3276ba93174d3ac2da3cf0e3efb4ac145 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/25.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "03fcbb07306e4df6be90480c9db08f09" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/25.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/3.python/25.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..10e1c29276a0e209a1a1fe35cf0781ba83396340 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/25.exercises/solution.md" @@ -0,0 +1,84 @@ +# 两数相加 + +

      给你两个 非空 的链表,表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的,并且每个节点只能存储 一位 数字。

      请你将两个数相加,并以相同形式返回一个表示和的链表。

      你可以假设除了数字 0 之外,这两个数都不会以 0 开头。

       

      示例 1:

      输入:l1 = [2,4,3], l2 = [5,6,4]
      输出:
      [7,0,8]
      解释:
      342 + 465 = 807.

      示例 2:

      输入:l1 = [0], l2 = [0]
      输出:
      [0]

      示例 3:

      输入:l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
      输出:
      [8,9,9,9,0,0,0,1]

       

      提示:

      • 每个链表中的节点数在范围 [1, 100]
      • 0 <= Node.val <= 9
      • 题目数据保证列表表示的数字不含前导零
      + +## template + +```python +class ListNode: + def __init__(self, val=0, next=None): + self.val = val + self.next = next +class LinkList: + def __init__(self): + self.head=None + def initList(self, data): + self.head = ListNode(data[0]) + r=self.head + p = self.head + for i in data[1:]: + node = ListNode(i) + p.next = node + p = p.next + return r + def convert_list(self,head): + ret = [] + if head == None: + return + node = head + while node != None: + ret.append(node.val) + node = node.next + return ret +class Solution: + def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode: + lrr = l1 + while True: + l1.val = l1.val + l2.val + if l1.next is None and l2.next is None and l1.val < 10: + break + if l1.next is None: + l1.next = ListNode(0) + if l2.next is None: + l2.next = ListNode(0) + if l1.val >= 10: + l1.val = l1.val - 10 + l1.next.val += 1 + l1 = l1.next + l2 = l2.next + return lrr +# %% +l = LinkList() +list1 = [2,4,3] +list2 = [5,6,4] +l1 = l.initList(list1) +l2 = l.initList(list2) +s = Solution() +print(l.convert_list(s.addTwoNumbers(l1, l2))) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/26.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/26.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..3bf198862ce8caee2db395963089aa258e5f3413 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/26.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-d8ad69a954f24feeb15fe58eb97dc245", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/26.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/26.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..ab8461086b9620146d45a01f97592134f113924b --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/26.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "72e4a2be4d154bf9a5a2c3dea1258135" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/26.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/3.python/26.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..64fb4a9ed422980a3c1a31081379cde133eb5ba6 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/26.exercises/solution.md" @@ -0,0 +1,96 @@ +# 分隔链表 + +

      给你一个链表的头节点 head 和一个特定值 x ,请你对链表进行分隔,使得所有 小于 x 的节点都出现在 大于或等于 x 的节点之前。

      你应当 保留 两个分区中每个节点的初始相对位置。

       

      示例 1:

      输入:head = [1,4,3,2,5,2], x = 3
      输出
      :[1,2,2,4,3,5]

      示例 2:

      输入:head = [2,1], x = 2
      输出
      :[1,2]

       

      提示:

      • 链表中节点的数目在范围 [0, 200]
      • -100 <= Node.val <= 100
      • -200 <= x <= 200
      + +## template + +```python +class ListNode(object): + def __init__(self, x): + self.val = x + self.next = None +class LinkList: + def __init__(self): + self.head=None + def initList(self, data): + self.head = ListNode(data[0]) + r=self.head + p = self.head + for i in data[1:]: + node = ListNode(i) + p.next = node + p = p.next + return r + def convert_list(self,head): + ret = [] + if head == None: + return + node = head + while node != None: + ret.append(node.val) + node = node.next + return ret +class Solution(object): + def partition(self, head, x): + """ + :type head: ListNode + :type x: int + :rtype: ListNode + """ + if head is None: + return None + less = lesshead = None + last = pos = head + while pos is not None: + if pos.val < x: + if lesshead is None: + lesshead = pos + else: + less.next = pos + less = pos + if head == pos: + last = head = pos.next + else: + last.next = pos.next + else: + last = pos + pos = pos.next + if lesshead is not None: + less.next = head + else: + lesshead = head + return lesshead +# %% +l = LinkList() +list1 = [1,4,3,2,5,2] +l1 = l.initList(list1) +x = 3 +s = Solution() +print(l.convert_list(s.partition(l1, x))) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/27.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/27.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..eb9daea98ba1c2322630e5c88ffd0e3b3df204d6 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/27.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-7294755011f74909adf7dfbac2facc41", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/27.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/27.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..61b9734d972b08c2a96d2c1c256b0265a1091670 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/27.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "16b9f20b9778493cb641830bd4d75987" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/27.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/3.python/27.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..2b3b3f9e54f4d21c73568d80a10a8c68ca8ca1ee --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/27.exercises/solution.md" @@ -0,0 +1,145 @@ +# 整数转罗马数字 + +
      +

      罗马数字包含以下七种字符: I, V, X, LCD 和 M。 +

      + +
      字符          数值
      +I             1
      +V             5
      +X             10
      +L             50
      +C             100
      +D             500
      +M             1000
      + +

      例如, 罗马数字 2 写做 II ,即为两个并列的 1。12 + 写做 XII ,即为 X + II 。 27 + 写做  XXVII, + 即为 XX + V + II 。

      + +

      通常情况下,罗马数字中小的数字在大的数字的右边。但也存在特例,例如 4 不写做 IIII,而是 IV。数字 1 在数字 5 的左边,所表示的数等于大数 5 + 减小数 1 得到的数值 4 。同样地,数字 9 表示为 IX。这个特殊的规则只适用于以下六种情况:

      + +
        +
      • I 可以放在 V (5) 和 X (10) 的左边,来表示 4 和 9。
      • +
      • X 可以放在 L (50) 和 C (100) 的左边,来表示 40 + 和 90。 
      • +
      • C 可以放在 D (500) 和 M (1000) 的左边,来表示 400 + 和 900。
      • +
      + +

      给你一个整数,将其转为罗马数字。

      + +

       

      + +

      示例 1:

      + +
      输入: num = 3
      +
      输出:
      "III"
      + +

      示例 2:

      + +
      输入: num = 4
      +
      输出:
      "IV"
      + +

      示例 3:

      + +
      输入: num = 9
      +
      输出:
      "IX"
      + +

      示例 4:

      + +
      输入: num = 58
      +
      输出:
      "LVIII" +
      解释:
      L = 50, V = 5, III = 3. +
      + +

      示例 5:

      + +
      输入: num = 1994
      +
      输出:
      "MCMXCIV" +
      解释:
      M = 1000, CM = 900, XC = 90, IV = 4.
      + +

       

      + +

      提示:

      + +
        +
      • 1 <= num <= 3999
      • +
      +
      + +## template + +```python +from math import floor +class Solution: + def intToRoman(self, num: int) -> str: + f = 1000 + f2 = 1000 + sym = ['M', 'D', 'C', 'L', 'X', 'V', 'I'] + fsi = 0 + s = [2, 5] + si = 0 + s2 = [10, 1] + si2 = 0 + roman = [] + while num > 0: + d = floor(num/f) + r = num % f + d2 = floor(num/f2) + r2 = num % f2 + if d > 0: + if d == 4: + roman.append(sym[fsi]) + roman.append(sym[fsi-1]) + num = r + elif d2 == 9: + roman.append(sym[fsi+1]) + roman.append(sym[fsi-1]) + num = r2 + else: + i = 0 + while i < d: + roman.append(sym[fsi]) + i += 1 + num = r + f = f/s[si] + si += 1 + si %= 2 + f2 = f2/s2[si2] + si2 += 1 + si2 %= 2 + fsi += 1 + return ''.join(roman) +# %% +s = Solution() +print(s.intToRoman(num = 3)) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/28.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/28.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..5c3692b349be2e8c24019545b0122771a0c552fd --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/28.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-81e7dec30a8247bc8e18c61747cc8d8d", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/28.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/28.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..c9580da900faa93a327978675abc5b46c9b04671 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/28.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "bc8567238b3c46d9b9b2adb69bdb4163" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/28.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/3.python/28.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..61136184e7fe5c9201bedf0f14f36b33b792bb99 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/28.exercises/solution.md" @@ -0,0 +1,54 @@ +# 全排列 II + +

      给定一个可包含重复数字的序列 nums按任意顺序 返回所有不重复的全排列。

       

      示例 1:

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

      示例 2:

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

       

      提示:

      • 1 <= nums.length <= 8
      • -10 <= nums[i] <= 10
      + +## template + +```python +from typing import List +class Solution: + def permuteUnique(self, nums: List[int]) -> List[List[int]]: + ans = [] + if len(nums) == 0: + return + if len(nums) == 1: + return [nums] + for index,item in enumerate(nums): + res = nums[:index]+nums[index+1:] + for j in self.permuteUnique(res): + ans.append(j+[item]) + rel = [] + for i in ans: + if i not in rel: + rel.append(i) + return rel +# %% +s = Solution() +print(s.permuteUnique(nums = [1,2,3])) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/29.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/29.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..fac5fae0d5fdf433348fecd00063d5c27258227b --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/29.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-281b41a98fed4cb2a45d80cbba2fa67d", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/29.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/29.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..33c35666ca545d5f453138e8835c190c555faa43 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/29.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "d6a9774b1f684ad0ac3d753e7874e6fb" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/29.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/3.python/29.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..d236037dec0627492dbad3f7f51e6576f54e9885 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/29.exercises/solution.md" @@ -0,0 +1,53 @@ +# 无重复字符的最长子串 + +

      给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。

       

      示例 1:

      输入: s = "abcabcbb"
      输出:
      3
      解释:
      因为无重复字符的最长子串是 "abc",所以其长度为 3。

      示例 2:

      输入: s = "bbbbb"
      输出:
      1
      解释:
      因为无重复字符的最长子串是 "b",所以其长度为 1。

      示例 3:

      输入: s = "pwwkew"
      输出:
      3
      解释:
      因为无重复字符的最长子串是 "wke",所以其长度为 3。  +请注意,你的答案必须是 子串 的长度,"pwke" 是一个子序列,不是子串。

      示例 4:

      输入: s = ""
      输出:
      0

       

      提示:

      • 0 <= s.length <= 5 * 104
      • s 由英文字母、数字、符号和空格组成
      + +## template + +```python +class Solution: + def lengthOfLongestSubstring(self, s: str) -> int: + i = 0 + j = 0 + m = 0 + hset = {} + while j < len(s): + char = s[j] + index = hset.get(char) + if index is not None and index > i: + i = index + m = m if m > j - i + 1 else j - i + 1 + hset[char] = j + 1 + j += 1 + return m +# %% +s = Solution() +print(s.lengthOfLongestSubstring('abcabcbb')) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/30.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/30.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..4757864c7f265d5c9384ab25790ed9aae56b7eb2 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/30.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-336698aa2ca847c184df675c7dd539b4", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/30.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/30.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..52861d7a29a15e03eaab12758b201fcb8df617aa --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/30.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "4a9bb10986dc46a99c2e30e360ffc88e" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/30.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/3.python/30.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..19858be7a4c7b373a1773653226e411fafc079fb --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/30.exercises/solution.md" @@ -0,0 +1,85 @@ +# 解码方法 + +

      一条包含字母 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 只包含数字,并且可能包含前导零。
      • +
      + +## template + +```python +class Solution(object): + def numDecodings(self, s): + """ + :type s: str + :rtype: int + """ + ls = len(s) + if ls == 0: + return 0 + dp = [0] * ls + for index in range(ls): + if index >= 1 and int(s[index - 1:index + 1]) < 27 and int(s[index - 1:index + 1]) >= 10: + if index == 1: + dp[index] = 1 + else: + dp[index] += dp[index - 2] + if int(s[index]) != 0: + if index == 0: + dp[index] = 1 + else: + dp[index] += dp[index - 1] + return dp[ls - 1] +# %% +s = Solution() +print(s.numDecodings(s = "12")) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/31.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/31.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..03e2e33a4d57dd7ae39063aa47ca0f4409408e97 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/31.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-02312a276e3041458105e351fe2b29b8", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/31.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/31.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..2e9fa189131558bf44cfe62955d11b997bae3c90 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/31.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "7a5b15ef4feb4de1a2bb623025dc65ab" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/31.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/3.python/31.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..c60643a9b4719adda29125826dd508fa7c7c9015 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/31.exercises/solution.md" @@ -0,0 +1,52 @@ +# 两数相除 + +

      给定两个整数,被除数 dividend 和除数 divisor。将两数相除,要求不使用乘法、除法和 mod 运算符。

      返回被除数 dividend 除以除数 divisor 得到的商。

      整数除法的结果应当截去(truncate)其小数部分,例如:truncate(8.345) = 8 以及 truncate(-2.7335) = -2

       

      示例 1:

      输入: dividend = 10, divisor = 3
      输出:
      3
      解释:
      10/3 = truncate(3.33333..) = truncate(3) = 3

      示例 2:

      输入: dividend = 7, divisor = -3
      输出:
      -2
      解释:
      7/-3 = truncate(-2.33333..) = -2

       

      提示:

      • 被除数和除数均为 32 位有符号整数。
      • 除数不为 0。
      • 假设我们的环境只能存储 32 位有符号整数,其数值范围是 [−231,  231 − 1]。本题中,如果除法结果溢出,则返回 231 − 1。
      + +## template + +```python +import math +class Solution(object): + def divide(self, dividend, divisor): + if divisor == 0: + return MAX_INT + if dividend == 0: + return 0 + isPositive = (dividend < 0) == (divisor < 0) + m = abs(dividend) + n = abs(divisor) + res = math.log(m) - math.log(n) + res = int(math.exp(res)) + if isPositive: + return min(res, 2147483647) + return max(0 - res, -2147483648) +if __name__ == '__main__': + s = Solution() + print(s.divide(1, 1)) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/32.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/32.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..caa15fd0f19d7cff3f09b0c4890bad7ddd279e40 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/32.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-550bc25050a34e0e9c750a93e5ec0488", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/32.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/32.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..7860822956ec96d03e1d88e5e027407f4b37faa4 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/32.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "19991abda1fe4a7191e643316d63f7e5" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/32.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/3.python/32.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..26841d88f9ccfcf93996603d3162f84f66afa290 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/32.exercises/solution.md" @@ -0,0 +1,59 @@ +# 螺旋矩阵 + +

      给你一个 mn 列的矩阵 matrix ,请按照 顺时针螺旋顺序 ,返回矩阵中的所有元素。

       

      示例 1:

      输入:matrix = [[1,2,3],[4,5,6],[7,8,9]]
      输出:
      [1,2,3,6,9,8,7,4,5]

      示例 2:

      输入:matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
      输出:
      [1,2,3,4,8,12,11,10,9,5,6,7]

       

      提示:

      • m == matrix.length
      • n == matrix[i].length
      • 1 <= m, n <= 10
      • -100 <= matrix[i][j] <= 100
      + +## template + +```python +class Solution(object): + def spiralOrder(self, matrix): + """ + :type matrix: List[List[int]] + :rtype: List[int] + """ + if matrix is None or len(matrix) == 0: + return matrix + m, n = len(matrix), len(matrix[0]) + return self.get_spiralOrder(matrix, 0, m - 1, 0, n - 1) + def get_spiralOrder(self, matrix, r_start, r_end, c_start, c_end): + if r_start > r_end or c_start > c_end: + return [] + elif r_start == r_end: + return matrix[r_start][c_start:c_end + 1] + elif c_start == c_end: + return [matrix[j][c_end] for j in range(r_start, r_end + 1)] + curr = matrix[r_start][c_start:c_end + 1] + [matrix[j][c_end] for j in range(r_start + 1, r_end)] +\ + matrix[r_end][c_start:c_end + 1][::-1] +\ + [matrix[j][c_start] for j in reversed(range(r_start + 1, r_end))] + res = curr + self.get_spiralOrder(matrix, r_start + 1, r_end - 1, c_start + 1, c_end - 1) + return res +if __name__ == '__main__': + s = Solution() + print (s.spiralOrder([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]])) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/33.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/33.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..7350da63cc3cafcb755e9eaff97286bfba8893a6 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/33.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-3e2b19c3db294e4bb591ffcf7e1947de", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/33.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/33.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..d7f3041919623889e4f80f9375e66c7afa9ba1ca --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/33.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "b106e941868c4eadbfd690a027738467" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/33.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/3.python/33.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..019aa788bfe4cc95510db0fd5c016fb36de035a8 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/33.exercises/solution.md" @@ -0,0 +1,47 @@ +# 全排列 + +

      给定一个 没有重复 数字的序列,返回其所有可能的全排列。

      示例:

      输入: [1,2,3]
      输出:
      [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]]
      + +## template + +```python +class Solution: + def permute(self, nums): + e=[] + if(len(nums)==1): + return [nums] + for i in range(len(nums)): + q=self.permute(nums[:i]+nums[i+1:]) + for c in q: + e.append([nums[i]]+c) + return e +# %% +s = Solution() +print(s.permute(nums = [1,2,3])) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/34.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/34.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..100ef8c7c3898b69c2b0fe8ce9adb4c1b2dce38b --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/34.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-d4c45c3402624189a9a5ae4a0e6c23b6", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/34.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/34.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..f5e4f23216c7a1f6e1364605a669b39ef4d150ea --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/34.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "19ef7e3445e44476a8ecd11021c81cd8" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/34.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/3.python/34.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..db3e014ac8f0368407d68cd5425a78210c5f2801 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/34.exercises/solution.md" @@ -0,0 +1,82 @@ +# 搜索旋转排序数组 + +

      整数数组 nums 按升序排列,数组中的值 互不相同

      +

      在传递给函数之前,nums 在预先未知的某个下标 k0 <= k < nums.length)上进行了 旋转,使数组变为 + [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]](下标 从 0 开始 + 计数)。例如, [0,1,2,4,5,6,7] 在下标 3 处经旋转后可能变为 [4,5,6,7,0,1,2] 。 +

      +

      给你 旋转后 的数组 nums 和一个整数 target ,如果 nums 中存在这个目标值 + target ,则返回它的下标,否则返回 -1 。 +

      +

       

      +

      示例 1:

      +
      输入:nums = [4,5,6,7,0,1,2], target = 0
      输出:
      4
      +

      示例 2:

      +
      输入:nums = [4,5,6,7,0,1,2], target = 3
      输出:
      -1
      +

      示例 3:

      +
      输入:nums = [1], target = 0
      输出:
      -1
      +

       

      +

      提示:

      +
        +
      • 1 <= nums.length <= 5000
      • +
      • -10^4 <= nums[i] <= 10^4
      • +
      • nums 中的每个值都 独一无二
      • +
      • 题目数据保证 nums 在预先未知的某个下标上进行了旋转
      • +
      • -10^4 <= target <= 10^4
      • +
      +

       

      +

      进阶:你可以设计一个时间复杂度为 O(log n) 的解决方案吗?

      + +## template + +```python +class Solution: + def search(self, nums, target): + def get(start, end): + if start > end: + return -1 + mid = (start + end) / 2 + mid = int(mid) + if nums[mid] == target: + return mid + elif nums[mid] >= nums[start]: + if target >= nums[start] and target < nums[mid]: + return get(start, mid - 1) + else: + return get(mid + 1, end) + elif nums[mid] <= nums[end]: + if target > nums[mid] and target <= nums[end]: + return get(mid + 1, end) + else: + return get(start, mid - 1) + return get(0, len(nums) - 1) +# %% +s = Solution() +print(s.search(nums = [4,5,6,7,0,1,2], target = 0)) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/35.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/35.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..3ae3aecacea78d6f9f6c69c87aef156e685daf87 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/35.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-bcc74253a8564984bd776a5f045619eb", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/35.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/35.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..e7557768cdb80e90258e7adaf8a17b2a4f4781b2 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/35.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "b1bb1a3a445840eea953a86805d33d05" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/35.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/3.python/35.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..99bf19b09c43aa8330d62fe0bb753fca43e7815c --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/35.exercises/solution.md" @@ -0,0 +1,59 @@ +# 搜索二维矩阵 + +

      编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值。该矩阵具有如下特性:

      • 每行中的整数从左到右按升序排列。
      • 每行的第一个整数大于前一行的最后一个整数。

       

      示例 1:

      输入:matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3
      输出:
      true

      示例 2:

      输入:matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13
      输出:
      false

       

      提示:

      • m == matrix.length
      • n == matrix[i].length
      • 1 <= m, n <= 100
      • -104 <= matrix[i][j], target <= 104
      + +## template + +```python +class Solution(object): + def searchMatrix(self, matrix, target): + """ + :type matrix: List[List[int]] + :type target: int + :rtype: bool + """ + if not matrix or not matrix[0]: + return False + rows = len(matrix) + cols = len(matrix[0]) + row, col = 0, cols - 1 + while True: + if row < rows and col >= 0: + if matrix[row][col] == target: + return True + elif matrix[row][col] < target: + row += 1 + else: + col -= 1 + else: + return False +# %% +s = Solution() +print(s.searchMatrix(matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3)) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/36.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/36.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..13ea100d7e8efb9836a27c89896cd3ec2b8c860d --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/36.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-3a3c93b91fce4281b644113d8d6177ac", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/36.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/36.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..785571f081d82cba85093c26f57a2d9decff1456 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/36.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "53fc5f11510d49a59f587d2f4591bc73" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/36.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/3.python/36.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..e8ba50cb12e41c99bfdd4ac595eee13599f5d2b4 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/36.exercises/solution.md" @@ -0,0 +1,50 @@ +# Pow(x, n) + +

      实现 pow(x, n) ,即计算 x 的 n 次幂函数(即,xn)。

       

      示例 1:

      输入:x = 2.00000, n = 10
      输出:
      1024.00000

      示例 2:

      输入:x = 2.10000, n = 3
      输出:
      9.26100

      示例 3:

      输入:x = 2.00000, n = -2
      输出:
      0.25000
      解释:
      2-2 = 1/22 = 1/4 = 0.25

       

      提示:

      • -100.0 < x < 100.0
      • -231 <= n <= 231-1
      • -104 <= xn <= 104
      + +## template + +```python +class Solution: + def myPow(self, x, n): + if n == 0: + return 1 + res ,curr = 1, abs(n) + while curr > 0: + if curr & 1 == 1: + res *= x + curr >>= 1 + x *= x + if n < 0: + return 1 / res + return res +# %% +s = Solution() +print(s.myPow(x = 2.00000, n = 10)) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/37.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/37.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..ede07b431c3f67d524049ec8b4cbbc2aa475d2ab --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/37.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-972c6f9fc8104a85b96d38568684a73f", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/37.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/37.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..78009196432c778361a317abb6244efdd00a622a --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/37.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "4107b10e5a284a8f92d7c2ce4e65c74b" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/37.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/3.python/37.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..11635786f2e9d558829fc7750dc94d59d2168ff3 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/37.exercises/solution.md" @@ -0,0 +1,61 @@ +# 字母异位词分组 + +

      给定一个字符串数组,将字母异位词组合在一起。字母异位词指字母相同,但排列不同的字符串。

      +

      示例:

      +
      输入:[eat", "tea", "tan", "ate", "nat", "bat"]
      输出:
      [[ate","eat","tea"],["nat","tan"],["bat"]]
      +

      说明:

      +
        +
      • 所有输入均为小写字母。
      • +
      • 不考虑答案输出的顺序。
      • +
      + +## template + +```python +class Solution(object): + def groupAnagrams(self, strs): + strs.sort() + hash = {} + for s in strs: + key = self.hash_key(s) + try: + hash[key].append(s) + except KeyError: + hash[key] = [s] + return hash.values() + def hash_key(self, s): + table = [0] * 26 + for ch in s: + index = ord(ch) - ord('a') + table[index] += 1 + return str(table) +# %% +s = Solution() +print(s.groupAnagrams(strs = ["eat", "tea", "tan", "ate", "nat", "bat"])) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/38.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/38.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..fd3b483d80f49f9ca110324c052336de0ed2ede1 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/38.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-cd4fec4e81ff486b9e34793d061a6a72", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/38.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/38.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..dc6cd7ae9de9d8ba94d78150cf2aedf6be9b5d55 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/38.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "73c3a25659f74d3987aeb0b284c736c5" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/38.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/3.python/38.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..880dfe359c31ceedb6850e381a86756a2afd9b26 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/38.exercises/solution.md" @@ -0,0 +1,65 @@ +# 矩阵置零 + +

      给定一个 m x n 的矩阵,如果一个元素为 0 ,则将其所在行和列的所有元素都设为 0 。请使用 原地 算法

      进阶:

      • 一个直观的解决方案是使用  O(mn) 的额外空间,但这并不是一个好的解决方案。
      • 一个简单的改进方案是使用 O(m + n) 的额外空间,但这仍然不是最好的解决方案。
      • 你能想出一个仅使用常量空间的解决方案吗?

       

      示例 1:

      输入:matrix = [[1,1,1],[1,0,1],[1,1,1]]
      输出:
      [[1,0,1],[0,0,0],[1,0,1]]

      示例 2:

      输入:matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]]
      输出:
      [[0,0,0,0],[0,4,5,0],[0,3,1,0]]

       

      提示:

      • m == matrix.length
      • n == matrix[0].length
      • 1 <= m, n <= 200
      • -231 <= matrix[i][j] <= 231 - 1
      + +## template + +```python +class Solution(object): + def setZeroes(self, matrix): + """ + :type matrix: List[List[int]] + :rtype: void Do not return anything, modify matrix in-place instead. + """ + if not matrix: + return + m = len(matrix) + if m == 0: + return + r = [] + c = [] + n = len(matrix[0]) + for i in range(m): + for j in range(n): + if matrix[i][j] == 0: + r.append(i) + c.append(j) + r = set(r) + c = set(c) + for i in r: + for j in range(n): + matrix[i][j] = 0 + for i in range(m): + for j in c: + matrix[i][j] = 0 + return matrix +# %% +s = Solution() +print(s.setZeroes(matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]])) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/39.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/39.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..02cdd94c23578ba2186e7e0cb245e7a4f2ab935c --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/39.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-3b46e16ff285445b8133adec97078c1b", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/39.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/39.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..d33e5db52e8475940696fd6556c409c4203e90c5 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/39.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "8cd8927783a44530a03a3f8edd1a7b6c" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/39.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/3.python/39.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..8f586d9ccb66d461062e47403a4e01e6e5b74e17 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/39.exercises/solution.md" @@ -0,0 +1,79 @@ +# 在排序数组中查找元素的第一个和最后一个位置 + +

      给定一个按照升序排列的整数数组 nums,和一个目标值 target。找出给定目标值在数组中的开始位置和结束位置。

      +

      如果数组中不存在目标值 target,返回 [-1, -1]

      +

      进阶:

      +
        +
      • 你可以设计并实现时间复杂度为 O(log n) 的算法解决此问题吗?
      • +
      +

       

      +

      示例 1:

      +
      输入:nums = [5,7,7,8,8,10], target = 8
      输出:
      [3,4]
      +

      示例 2:

      +
      输入:nums = [5,7,7,8,8,10], target = 6
      输出:
      [-1,-1]
      +

      示例 3:

      +
      输入:nums = [], target = 0
      输出:
      [-1,-1]
      +

       

      +

      提示:

      +
        +
      • 0 <= nums.length <= 105
      • +
      • -109 <= nums[i] <= 109
      • +
      • nums 是一个非递减数组
      • +
      • -109 <= target <= 109
      • +
      + +## template + +```python +class Solution(object): + def searchRange(self, nums, target): + length = len(nums) + if length == 0: + return [-1, -1] + min = 0 + max = length - 1 + while min <= max: + pos = (min + max) / 2 + pos = int(pos) + if nums[pos] > target: + max = pos - 1 + elif nums[pos] < target: + min = pos + 1 + else: + for i in range(min, max + 1): + if nums[i] == target: + if min < i and nums[min] != nums[i]: + min = i + max = i + return [min, max] + return [-1, -1] +# %% +s = Solution() +print(s.searchRange(nums = [5,7,7,8,8,10], target = 8)) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/40.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/40.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..e5a57ffd322db9c5c1c582cc13b5c05f0b5f9e54 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/40.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-277632402f2a4278a9349e9fb214ff2a", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/40.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/40.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..7a6a818731ea476145e9de04c7a95ee2d964562d --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/40.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "fdd0ce89ea3142ee863efd52d0905557" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/40.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/3.python/40.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..1a8118786e9bea7fb31f184dcf7ede3c00cdea5a --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/40.exercises/solution.md" @@ -0,0 +1,58 @@ +# 跳跃游戏 II + +

      给定一个非负整数数组,你最初位于数组的第一个位置。

      +

      数组中的每个元素代表你在该位置可以跳跃的最大长度。

      +

      你的目标是使用最少的跳跃次数到达数组的最后一个位置。

      +

      示例:

      +
      输入: [2,3,1,1,4]
      输出:
      2
      解释:
      跳到最后一个位置的最小跳跃数是 2。从下标为 0 跳到下标为 1 的位置,跳 1 步,然后跳 3 步到达数组的最后一个位置。
      +

      说明:

      +

      假设你总是可以到达数组的最后一个位置。

      + +## template + +```python +class Solution: + def jump(self, nums): + if len(nums) <= 1: + return 0 + end = 0 + nums[0] + start = 0 + step = 1 + maxDis = 0 + nums[0] + while end < len(nums) - 1: + for i in range(start + 1, end + 1): + maxDis = max(maxDis, nums[i] + i) + start = end + end = maxDis + step += 1 + return step +# %% +s = Solution() +print(s.jump(nums = [2,3,0,1,4])) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/41.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/41.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..f41720a991725395220fcaeaae6f5cb41d56975e --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/41.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-d7a6df3099bb428d9d021f7fe409f142", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/41.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/41.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..589bef524e829746ee0ffe08d8f27e5a57d51cc3 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/41.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "46582e067af94456bc8c6ba07a089d96" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/41.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/3.python/41.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..b9ae47a3c5b781f432d7b9d6ba207d9ea5959259 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/41.exercises/solution.md" @@ -0,0 +1,99 @@ +# 反转链表 II + +给你单链表的头指针 head 和两个整数 leftright ,其中 left <= right 。请你反转从位置 left 到位置 right 的链表节点,返回 反转后的链表

       

      示例 1:

      输入:head = [1,2,3,4,5], left = 2, right = 4
      输出:
      [1,4,3,2,5]

      示例 2:

      输入:head = [5], left = 1, right = 1
      输出:
      [5]

       

      提示:

      • 链表中节点数目为 n
      • 1 <= n <= 500
      • -500 <= Node.val <= 500
      • 1 <= left <= right <= n

       

      进阶: 你可以使用一趟扫描完成反转吗?

      + +## template + +```python +class ListNode(object): + def __init__(self, x): + self.val = x + self.next = None +class LinkList: + def __init__(self): + self.head=None + def initList(self, data): + self.head = ListNode(data[0]) + r=self.head + p = self.head + for i in data[1:]: + node = ListNode(i) + p.next = node + p = p.next + return r + def convert_list(self,head): + ret = [] + if head == None: + return + node = head + while node != None: + ret.append(node.val) + node = node.next + return ret +class Solution(object): + def reverseBetween(self, head, m, n): + """ + :type head: ListNode + :type m: int + :type n: int + :rtype: ListNode + """ + if m == n: + return head + split_node, prev, curr = None, None, head + count = 1 + while count <= m and curr is not None: + if count == m: + split_node = prev + prev = curr + curr = curr.next + count += 1 + tail, next_node = prev, None + while curr is not None and count <= n: + next_temp = curr.next + curr.next = prev + prev = curr + curr = next_temp + count += 1 + if split_node is not None: + split_node.next = prev + if tail is not None: + tail.next = curr + if m == 1: + return prev + return head +# %% +l = LinkList() +list1 = [1,2,3,4,5] +l1 = l.initList(list1) +left = 2 +right = 4 +s = Solution() +print(l.convert_list(s.reverseBetween(l1, left, right))) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/42.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/42.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..14119a723b3ed3b797f96549633b461b9645082e --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/42.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-128691a90f0e43a9906704c6d330037f", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/42.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/42.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..da2d22a874c9740bed502f5230617c51e00d09a2 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/42.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "9e431861d1f14134b62dd59a6c4379d7" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/42.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/3.python/42.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..a8135c3207030f91c7034d2b694fc9ab2863fa3a --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/42.exercises/solution.md" @@ -0,0 +1,60 @@ +# 最小路径和 + +

      给定一个包含非负整数的 m x n 网格 grid ,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小。

      说明:每次只能向下或者向右移动一步。

       

      示例 1:

      输入:grid = [[1,3,1],[1,5,1],[4,2,1]]
      输出:
      7
      解释:
      因为路径 1→3→1→1→1 的总和最小。

      示例 2:

      输入:grid = [[1,2,3],[4,5,6]]
      输出:
      12

       

      提示:

      • m == grid.length
      • n == grid[i].length
      • 1 <= m, n <= 200
      • 0 <= grid[i][j] <= 100
      + +## template + +```python +class Solution(object): + def minPathSum(self, grid): + """ + :type grid: List[List[int]] + :rtype: int + """ + height = len(grid) + if height == 0: + return 0 + width = len(grid[0]) + pathmap = [] + for i in range(height): + pathmap.append([100000000000] * width) + pathmap[0][0] = grid[0][0] + for i in range(height): + for j in range(width): + compare = [pathmap[i][j]] + if i - 1 >= 0: + compare.append(pathmap[i - 1][j] + grid[i][j]) + if j - 1 >= 0: + compare.append(pathmap[i][j - 1] + grid[i][j]) + pathmap[i][j] = min(compare) + return pathmap[-1][-1] +# %% +s = Solution() +print(s.minPathSum(grid = [[1,3,1],[1,5,1],[4,2,1]])) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/43.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/43.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..3e0796e0307f19e28e52a7168f5942b8d60a05ae --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/43.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-033168b7d413422893ebb706716f6a9a", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/43.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/43.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..ae43394e21ef372106e53e76456b4ad60f7587fb --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/43.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "3f3da1b772e44b1ea18fc35896293db4" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/43.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/3.python/43.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..8eb6eb9e47c0682cabf175271e97da2c0bc60fa2 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/43.exercises/solution.md" @@ -0,0 +1,79 @@ +# 删除链表的倒数第 N 个结点 + +

      给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点。

      进阶:你能尝试使用一趟扫描实现吗?

       

      示例 1:

      输入:head = [1,2,3,4,5], n = 2
      输出:
      [1,2,3,5]

      示例 2:

      输入:head = [1], n = 1
      输出:
      []

      示例 3:

      输入:head = [1,2], n = 1
      输出:
      [1]

       

      提示:

      • 链表中结点的数目为 sz
      • 1 <= sz <= 30
      • 0 <= Node.val <= 100
      • 1 <= n <= sz
      + +## template + +```python +class ListNode: + def __init__(self, val=0, next=None): + self.val = val + self.next = next +class LinkList: + def __init__(self): + self.head=None + def initList(self, data): + self.head = ListNode(data[0]) + r=self.head + p = self.head + for i in data[1:]: + node = ListNode(i) + p.next = node + p = p.next + return r + def convert_list(self,head): + ret = [] + if head == None: + return + node = head + while node != None: + ret.append(node.val) + node = node.next + return ret +class Solution: + def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode: + v = ListNode(0, head) + handle = v + index = [] + while v is not None: + index.append(v) + v = v.next + pre = len(index)-n-1 + next = len(index)-n+1 + index[pre].next = index[next] if next >= 0 and next < len( + index) else None + return handle.next +# %% +l = LinkList() +list1 = [1,2,3,4,5] +head = l.initList(list1) +n = 2 +s = Solution() +print(l.convert_list(s.removeNthFromEnd(head, n))) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/44.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/44.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..944f6208c5d8da753f3ff80832d61713c29133fe --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/44.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-9dc64ccbad74475692b575540d87e645", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/44.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/44.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..39833af02446731cb6aa94ba2d0098bda53b2e6d --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/44.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "655bcde30c05420fab17269e866162a5" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/44.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/3.python/44.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..9680e54e3d510c092b26d91ca9f2169b0eceed79 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/44.exercises/solution.md" @@ -0,0 +1,72 @@ +# 组合总和 + +

      给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。 +

      +

      candidates 中的数字可以无限制重复被选取。

      +

      说明:

      +
        +
      • 所有数字(包括 target)都是正整数。
      • +
      • 解集不能包含重复的组合。 
      • +
      +

      示例 1:

      +
      输入:candidates = [2,3,6,7], target = 7,
      输出:
      [[7],[2,2,3]]
      +

      示例 2:

      +
      输入:candidates = [2,3,5], target = 8,
      输出:
      [[2,2,2,2],[2,3,3],[3,5]]
      +

       

      +

      提示:

      +
        +
      • 1 <= candidates.length <= 30
      • +
      • 1 <= candidates[i] <= 200
      • +
      • candidate 中的每个元素都是独一无二的。
      • +
      • 1 <= target <= 500
      • +
      + +## template + +```python +class Solution(object): + def combinationSum(self, candidates, target): + candidates.sort() + dp = [[] for _ in range(target + 1)] + dp[0].append([]) + for i in range(1, target + 1): + for j in range(len(candidates)): + if candidates[j] > i: + break + for k in range(len(dp[i - candidates[j]])): + temp = dp[i - candidates[j]][k][:] + if len(temp) > 0 and temp[-1] > candidates[j]: + continue + temp.append(candidates[j]) + dp[i].append(temp) + return dp[target] +if __name__ == '__main__': + s = Solution() + print (s.combinationSum([8,7,4,3], 11)) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/45.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/45.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..83dab89d576864d33717c12c78ed1b18fe766a47 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/45.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-2948603ab5ae47ecabc06a5e731ca8a6", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/45.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/45.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..d0974ab90875cca1b01669a835691ceba7930a51 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/45.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "20c7515e58264f97a3ccbe20632c38c8" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/45.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/3.python/45.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..758e2ee88c9f2b70615556fa09f30936c20fe210 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/45.exercises/solution.md" @@ -0,0 +1,69 @@ +# 最长回文子串 + +

      给你一个字符串 s,找到 s 中最长的回文子串。

       

      示例 1:

      输入:s = "babad"
      输出:
      "bab"
      解释:
      "aba" 同样是符合题意的答案。

      示例 2:

      输入:s = "cbbd"
      输出:
      "bb"

      示例 3:

      输入:s = "a"
      输出:
      "a"

      示例 4:

      输入:s = "ac"
      输出:
      "a"

       

      提示:

      • 1 <= s.length <= 1000
      • s 仅由数字和英文字母(大写和/或小写)组成
      + +## template + +```python +class Solution: + def longestPalindrome(self, s: str) -> str: + ti = 0 + maxlen = 0 + i = 0 + while i < len(s): + t = 1 + while t <= i and i + t < len(s): + if s[i + t] == s[i - t]: + t += 1 + else: + break + t -= 1 + if 2 * t + 1 > maxlen: + ti = i - t + maxlen = 2 * t + 1 + i += 1 + i = 0 + while i < len(s): + t = 1 + while t <= i + 1 and i + t < len(s): + if s[i - t + 1] == s[i + t]: + t += 1 + else: + break + t -= 1 + if 2 * t > maxlen: + ti = i - t + 1 + maxlen = 2 * t + i += 1 + return s[ti:ti+maxlen] +# %% +s = Solution() +print(s.longestPalindrome('babad')) +print(s.longestPalindrome('cbbd')) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/46.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/46.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..aa2221a02cb49299d273ecbe5f049f57ef28b55b --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/46.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-dc835225fbfd461e8cba066b571083ec", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/46.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/46.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..82acc01d40ed6311656b65a0090f805f005bde9c --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/46.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "b4e6f4ae25c54db188616ec8aef71a25" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/46.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/3.python/46.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..2122ed73a5b8a4021a8a9aae152370c06271080a --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/46.exercises/solution.md" @@ -0,0 +1,99 @@ +# 搜索旋转排序数组 II + +

      已知存在一个按非降序排列的整数数组 nums ,数组中的值不必互不相同。

      +

      在传递给函数之前,nums 在预先未知的某个下标 k0 <= k < nums.length)上进行了 旋转 + ,使数组变为 [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]](下标 从 0 + 开始 计数)。例如, [0,1,2,4,4,4,5,6,6,7] 在下标 5 处经旋转后可能变为 + [4,5,6,6,7,0,1,2,4,4] 。 +

      +

      给你 旋转后 的数组 nums 和一个整数 target ,请你编写一个函数来判断给定的目标值是否存在于数组中。如果 + nums 中存在这个目标值 target ,则返回 true ,否则返回 false 。 +

      +

       

      +

      示例 1:

      +
      输入:nums = [2,5,6,0,0,1,2], target = 0
      输出:
      true
      +

      示例 2:

      +
      输入:nums = [2,5,6,0,0,1,2], target = 3
      输出:
      false
      +

       

      +

      提示:

      +
        +
      • 1 <= nums.length <= 5000
      • +
      • -104 <= nums[i] <= 104
      • +
      • 题目数据保证 nums 在预先未知的某个下标上进行了旋转
      • +
      • -104 <= target <= 104
      • +
      +

       

      +

      进阶:

      +
        +
      • 这是 搜索旋转排序数组 的延伸题目,本题中的 nums  + 可能包含重复元素。
      • +
      • 这会影响到程序的时间复杂度吗?会有怎样的影响,为什么?
      • +
      + +## template + +```python +class Solution(object): + def search(self, nums, target): + """ + :type nums: List[int] + :type target: int + :rtype: bool + """ + def get(start, end): + if start > end: + return False + mid = (start + end) / 2 + mid = int(mid) + while mid < end and nums[mid + 1] == nums[mid]: + mid += 1 + while start < mid and nums[start + 1] == nums[start]: + start += 1 + if nums[mid] == target: + return True + elif mid == end: + return get(start, mid - 1) + elif start == mid: + return get(mid + 1, end) + elif nums[mid] >= nums[start]: + if target >= nums[start] and target < nums[mid]: + return get(start, mid - 1) + else: + return get(mid + 1, end) + elif nums[mid] <= nums[end]: + if target > nums[mid] and target <= nums[end]: + return get(mid + 1, end) + else: + return get(start, mid - 1) + return get(0, len(nums) - 1) +# %% +s = Solution() +print(s.search(nums = [2,5,6,0,0,1,2], target = 0)) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/47.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/47.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..d3e0248e331802aa180be829028297fde538cd9c --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/47.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-5f85982f797147978e7cc0895f5ea521", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/47.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/47.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..d55b1ee7a2606f14294f1857ddba4d3dc4d732f3 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/47.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "531449e3891e4798a5a5b761c15b28ac" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/47.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/3.python/47.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..bb0de702e6fca153622a43c15863b0fb4a9fb354 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/47.exercises/solution.md" @@ -0,0 +1,91 @@ +# 删除有序数组中的重复项 II + +

      给你一个有序数组 nums ,请你 原地 删除重复出现的元素,使每个元素 最多出现两次 ,返回删除后数组的新长度。

      +

      不要使用额外的数组空间,你必须在 原地 修改输入数组 并在使用 O(1) 额外空间的条件下完成。

      +

       

      +

      说明:

      +

      为什么返回数值是整数,但输出的答案是数组呢?

      +

      请注意,输入数组是以「引用」方式传递的,这意味着在函数里修改输入数组对于调用者是可见的。

      +

      你可以想象内部操作如下:

      +
      +    // nums 是以“引用”方式传递的。也就是说,不对实参做任何拷贝
      +    int len = removeDuplicates(nums);// 在函数里修改输入数组对于调用者是可见的。
      +    // 根据你的函数返回的长度, 它会打印出数组中 该长度范围内 的所有元素。
      +    for (int i = 0; i < len; i++) {
      +            print(nums[i]);
      +    }
      +

       

      +

      示例 1:

      +
      输入:nums = [1,1,1,2,2,3]
      输出:
      5, nums = [1,1,2,2,3]
      解释:
      函数应返回新长度 length = 5, 并且原数组的前五个元素被修改为 1, 1, 2, 2, 3 。 不需要考虑数组中超出新长度后面的元素。
      +

      示例 2:

      +
      输入:nums = [0,0,1,1,1,1,2,3,3]
      输出:
      7, nums = [0,0,1,1,2,3,3]
      解释:
      函数应返回新长度 length = 7, 并且原数组的前五个元素被修改为 0, 0, 1, 1, 2, 3, 3 。 不需要考虑数组中超出新长度后面的元素。
      +

       

      +

      提示:

      +
        +
      • 1 <= nums.length <= 3 * 104
      • +
      • -104 <= nums[i] <= 104
      • +
      • nums 已按升序排列
      • +
      + +## template + +```python +class Solution: + def removeDuplicates(self, nums): + """ + :type nums: List[int] + :rtype: int + """ + if nums is None: + return 0 + length = len(nums) + result = 0 + i = j = 0 + while i < length: + j = i + while j < length: + if nums[j] != nums[i]: + break + j += 1 + if j-i > 2: + length -= j-i-2 + for k in range(j-i-2): + del nums[i] + result += 2 + j = i+2 + else: + result += (j-i) + i = j + return result +# %% +s = Solution() +print(s.removeDuplicates(nums = [1,1,1,2,2,3])) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/48.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/48.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..7489b0ffb28e94c0f11b5402f7b26fc0aeb15296 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/48.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-5b3ea367edd04bcbac04bc67d97cd05f", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/48.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/48.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..3f9eea62fec8702f7af06108fe7bb740e21a1af3 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/48.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "7364f93150ab4e3db188cc5640b82a90" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/48.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/3.python/48.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..58fab504f1f991ddb5054bd2dbae318844960887 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/48.exercises/solution.md" @@ -0,0 +1,69 @@ +# 交错字符串 + +

      给定三个字符串 s1s2s3,请你帮忙验证 s3 是否是由 s1 和 s2 交错 组成的。

      两个字符串 st 交错 的定义与过程如下,其中每个字符串都会被分割成若干 非空 子字符串:

      • s = s1 + s2 + ... + sn
      • t = t1 + t2 + ... + tm
      • |n - m| <= 1
      • 交错s1 + t1 + s2 + t2 + s3 + t3 + ... 或者 t1 + s1 + t2 + s2 + t3 + s3 + ...

      提示:a + b 意味着字符串 ab 连接。

       

      示例 1:

      输入:s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac"
      输出:
      true

      示例 2:

      输入:s1 = "aabcc", s2 = "dbbca", s3 = "aadbbbaccc"
      输出:
      false

      示例 3:

      输入:s1 = "", s2 = "", s3 = ""
      输出:
      true

       

      提示:

      • 0 <= s1.length, s2.length <= 100
      • 0 <= s3.length <= 200
      • s1s2、和 s3 都由小写英文字母组成
      + +## template + +```python +class Solution(object): + def isInterleave(self, s1, s2, s3): + """ + :type s1: str + :type s2: str + :type s3: str + :rtype: bool + """ + if len(s1) + len(s2) != len(s3): + return False + queue = [(0, 0), (-1, -1)] + visited = set() + isSuccess = False + index = 0 + while len(queue) != 1 or queue[0][0] != -1: + p = queue.pop(0) + if p[0] == len(s1) and p[1] == len(s2): + return True + if p[0] == -1: + queue.append(p) + index += 1 + continue + if p in visited: + continue + visited.add(p) + if p[0] < len(s1): + if s1[p[0]] == s3[index]: + queue.append((p[0] + 1, p[1])) + if p[1] < len(s2): + if s2[p[1]] == s3[index]: + queue.append((p[0], p[1] + 1)) + return False +# %% +s = Solution() +print(s.isInterleave(s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac")) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/49.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/49.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..da518a7acf4aaaf3505438cd42f4f5f1d589811b --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/49.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-ecb044361bb3478699aca0b281579c3b", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/49.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/49.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..840645fcbd23a58930709a070b52cd632ed07ba4 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/49.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "1932a6fe787e4361b8b69dc21b539749" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/49.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/3.python/49.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..d468400db0330175870754639a839e2a7f928b59 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/49.exercises/solution.md" @@ -0,0 +1,79 @@ +# 合并区间 + +

      以数组 intervals 表示若干个区间的集合,其中单个区间为 intervals[i] = [starti, endi] 。请你合并所有重叠的区间,并返回一个不重叠的区间数组,该数组需恰好覆盖输入中的所有区间。

       

      示例 1:

      输入:intervals = [[1,3],[2,6],[8,10],[15,18]]
      输出:
      [[1,6],[8,10],[15,18]]
      解释:
      区间 [1,3] 和 [2,6] 重叠, 将它们合并为 [1,6].

      示例 2:

      输入:intervals = [[1,4],[4,5]]
      输出:
      [[1,5]]
      解释:
      区间 [1,4] 和 [4,5] 可被视为重叠区间。

       

      提示:

      • 1 <= intervals.length <= 104
      • intervals[i].length == 2
      • 0 <= starti <= endi <= 104
      + +## template + +```python +class Interval(object): + def __init__(self, s=0, e=0): + self.start = s + self.end = e +class Solution(object): + def list2interval(self, list_interval): + ret = [] + for i in list_interval: + interval = Interval(i[0], i[1]) + ret.append(interval) + return ret + def interval2list(self, interval): + ret = [] + x = [0,0] + for i in interval: + x[0] = i.start + x[1] = i.end + ret.append(x) + x = [0,0] + return ret + def merge(self, intervals): + """ + :type intervals: List[Interval] + :rtype: List[Interval] + """ + if intervals is None: + return + ls = len(intervals) + if ls <= 1: + return intervals + intervals = self.list2interval(intervals) + intervals.sort(key=lambda x: x.start) + pos = 0 + while pos < len(intervals) - 1: + if intervals[pos].end >= intervals[pos + 1].start: + next = intervals.pop(pos + 1) + if next.end > intervals[pos].end: + intervals[pos].end = next.end + else: + pos += 1 + intervals = self.interval2list(intervals) + return intervals +if __name__ == '__main__': + s = Solution() + print (s.merge(intervals = [[1,4],[4,5]])) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/5.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/5.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..0914a890251a4c6959a9e402270e047e1ea566a1 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/5.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-d535ce62303f48e6a819f9c2f3cf57be", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/5.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/5.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..5af4dba42b978d7a1f27147acb6ffdb68a38cea6 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/5.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "d8e6b22aaf684f9980881df498fc6e44" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/5.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/3.python/5.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..0c987050711609a3be641b2499e65933a09ab94c --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/5.exercises/solution.md" @@ -0,0 +1,121 @@ +# 恢复二叉搜索树 + +

      给你二叉搜索树的根节点 root ,该树中的两个节点被错误地交换。请在不改变其结构的情况下,恢复这棵树。

      进阶:使用 O(n) 空间复杂度的解法很容易实现。你能想出一个只使用常数空间的解决方案吗?

       

      示例 1:

      输入:root = [1,3,null,null,2]
      输出:
      [3,1,null,null,2]
      解释:
      3 不能是 1 左孩子,因为 3 > 1 。交换 1 和 3 使二叉搜索树有效。

      示例 2:

      输入:root = [3,1,4,null,null,2]
      输出:
      [2,1,4,null,null,3]
      解释:
      2 不能在 3 的右子树中,因为 2 < 3 。交换 2 和 3 使二叉搜索树有效。

       

      提示:

      • 树上节点的数目在范围 [2, 1000]
      • -231 <= Node.val <= 231 - 1
      + +## template + +```python +import sys +class TreeNode(object): + def __init__(self, x): + self.val = x + self.left = None + self.right = None + def to_list(self, count): + queue = [] + queue.append(self) + result = [] + while len(queue) > 0: + if count == 0: + break + node = queue.pop(0) + if node is None: + result.append('null') + else: + count -= 1 + result.append(node.val) + queue.append(node.left) + queue.append(node.right) + return result +class List2Tree(object): + def __init__(self, nums: list): + self.nums = nums + self.queue = [] + if len(nums) == 1: + self.root = TreeNode(self.nums.pop(0)) + else: + a = self.nums.pop(0) + b = self.nums.pop(0) + c = self.nums.pop(0) + self.root = TreeNode(a) + if b is not None: + self.root.left = TreeNode(b) + else: + self.root.left = b + if c is not None: + self.root.right = TreeNode(c) + else: + self.root.right = c + self.queue.append(self.root.left) + self.queue.append(self.root.right) + def convert(self): + while len(self.nums) > 0 and len(self.queue) > 0: + node = self.queue.pop(0) + if node is not None: + num = self.nums.pop(0) + if num is not None: + node.left = TreeNode(num) + else: + node.left = num + if len(self.nums) > 0: + num = self.nums.pop(0) + else: + num = None + if num is not None: + node.right = TreeNode(num) + else: + node.right = num + self.queue.append(node.left) + self.queue.append(node.right) + return self.root +class Solution(object): + def __init__(self): + self.first = self.second = None + self.pre = TreeNode(-sys.maxsize - 1) + def recoverTree(self, root): + length = len(root) + root = List2Tree(root).convert() + self.traverse(root) + self.first.val, self.second.val = self.second.val, self.first.val + return root.to_list(length) + def traverse(self, root): + if root is None: + return + self.traverse(root.left) + if self.pre.val >= root.val: + if self.first is None: + self.first = self.pre + if self.first is not None: + self.second = root + self.pre = root + self.traverse(root.right) +# %% +s = Solution() +print(s.recoverTree(root=[1, 3, None, None, 2])) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/50.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/50.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..6de343f9e515a4e1301ae2eaa73015c79ab90302 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/50.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-4358355a51b2455dbac78b459103f467", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/50.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/50.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..b954c6dfc7cf0c578c09fb9c4e5ebf5f9acb4330 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/50.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "42b6841bc34f4bddb043de59248fe376" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/50.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/3.python/50.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..4cfaa80bdeb50286ee4a307090a05319d739d5be --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/50.exercises/solution.md" @@ -0,0 +1,75 @@ +# 三数之和 + +

      给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有和为 0 且不重复的三元组。

      注意:答案中不可以包含重复的三元组。

       

      示例 1:

      输入:nums = [-1,0,1,2,-1,-4]
      输出:
      [[-1,-1,2],[-1,0,1]]

      示例 2:

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

      示例 3:

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

       

      提示:

      • 0 <= nums.length <= 3000
      • -105 <= nums[i] <= 105
      + +## template + +```python +from typing import List +class Solution: + def threeSum(self, nums: List[int]) -> List[List[int]]: + if len(nums) == 0: + return [] + result = [] + unique = {} + inv = {} + left = None + right = None + nums.sort() + i = 0 + while i < len(nums): + if left == None and nums[i] >= 0: + left = i + if right == None and nums[i] > 0: + right = i + inv[nums[i]] = i + i += 1 + if left == 0: + right = len(nums) + if right is None: + return [] + i = 0 + while i < right: + j = i+1 + while j < len(nums) and (-nums[i] >= nums[j] * 2): + last = 0-nums[i]-nums[j] + k = inv.get(last) + if k and k > j: + list = [nums[i], nums[j], last] + hash = f'{list[0]}_{list[1]}_{list[2]}' + if unique.get(hash) is None: + unique[hash] = True + result.append(list) + j += 1 + i += 1 + return result +# %% +s = Solution() +print(s.threeSum(nums = [-1,0,1,2,-1,-4])) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/51.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/51.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..4c96e9719c2e0ce5801a7ba2b75b01a2f718130a --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/51.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-db1d6c24ed4b48e79d3d6e2f6016778c", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/51.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/51.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..577b31483ac5bfa2f3a5e3fec343dc688a0d4b66 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/51.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "900b070487914f3897416058756bca83" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/51.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/3.python/51.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..2de1efcbc17a356b58a6b92cc226bb07cdfb8dcd --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/51.exercises/solution.md" @@ -0,0 +1,58 @@ +# 字符串相乘 + +

      给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式。

      示例 1:

      输入: num1 = "2", num2 = "3"
      输出:
      "6"

      示例 2:

      输入: num1 = "123", num2 = "456"
      输出:
      "56088"

      说明:

      1. num1 和 num2 的长度小于110。
      2. num1 和 num2 只包含数字 0-9
      3. num1 和 num2 均不以零开头,除非是数字 0 本身。
      4. 不能使用任何标准库的大数类型(比如 BigInteger)直接将输入转换为整数来处理
      + +## template + +```python +class Solution(object): + def multiply(self, num1, num2): + if num1 == '0' or num2 == '0': + return '0' + res = '' + ls1, ls2, = len(num1), len(num2) + ls = ls1 + ls2 + arr = [0] * ls + for i in reversed(range(ls1)): + for j in reversed(range(ls2)): + arr[i + j + 1] += int(num1[i]) * int(num2[j]) + for i in reversed(range(1, ls)): + arr[i - 1] += arr[i] / 10 + arr[i] %= 10 + pos = 0 + if arr[pos] == 0: + pos += 1 + while pos < ls: + res = res + str(arr[pos]) + pos += 1 + return res +if __name__ == '__main__': + s = Solution() + print (s.multiply("98", "9")) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/52.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/52.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..25c079097405156f3beeb3e4b7bf63a58cdee2b8 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/52.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-b48dd0f35de344dda5b205971f898804", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/52.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/52.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..5b7387cc58845f392bda88b69de9e6c5e466fb72 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/52.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "05250e01663c4149a6ea9b367e440b03" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/52.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/3.python/52.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..028852279f742562e5a88bb4df023d4ba930253c --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/52.exercises/solution.md" @@ -0,0 +1,67 @@ +# 最接近的三数之和 + +

      给定一个包括 n 个整数的数组 nums 和 一个目标值 target。找出 nums 中的三个整数,使得它们的和与 target 最接近。返回这三个数的和。假定每组输入只存在唯一答案。

       

      示例:

      输入:nums = [-1,2,1,-4], target = 1
      输出:
      2
      解释:
      与 target 最接近的和是 2 (-1 + 2 + 1 = 2) 。

       

      提示:

      • 3 <= nums.length <= 10^3
      • -10^3 <= nums[i] <= 10^3
      • -10^4 <= target <= 10^4
      + +## template + +```python +from typing import List +class Solution: + def threeSumClosest(self, nums: List[int], target: int) -> int: + nums.sort() + cur = 0 + closest = nums[0] + nums[1] + nums[2] + while cur < len(nums) - 2: + left = cur + 1 + right = len(nums) - 1 + while left < right: + n = nums[cur] + nums[left] + nums[right] + if abs(target - n) < abs(target - closest): + closest = n + if n == target: + break + elif n > target: + t = right - 1 + while (t > left and nums[t] == nums[right]): + t -= 1 + right = t + else: + t = left + 1 + while (t < right and nums[t] == nums[left]): + t += 1 + left = t + t = cur + 1 + while (t < len(nums) and nums[t] == nums[cur]): + t += 1 + cur = t + return closest +# %% +s = Solution() +print(s.threeSumClosest(nums = [-1,2,1,-4], target = 1)) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/53.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/53.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..652f6ff5681d70879bc39c7a0a3bff463e77a9c3 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/53.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-99fb2b9507a94fb381b84b06305410d6", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/53.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/53.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..cc0011762cf0d42102fc9512bbea6eb5c9d528e8 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/53.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "adf1ec79818e4431b7223ec849c10a6e" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/53.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/3.python/53.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..68578d53101fb0e3e4a977333253b9ca5552963d --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/53.exercises/solution.md" @@ -0,0 +1,49 @@ +# 跳跃游戏 + +

      给定一个非负整数数组 nums ,你最初位于数组的 第一个下标

      数组中的每个元素代表你在该位置可以跳跃的最大长度。

      判断你是否能够到达最后一个下标。

       

      示例 1:

      输入:nums = [2,3,1,1,4]
      输出:
      true
      解释:
      可以先跳 1 步,从下标 0 到达下标 1, 然后再从下标 1 跳 3 步到达最后一个下标。

      示例 2:

      输入:nums = [3,2,1,0,4]
      输出:
      false
      解释:
      无论怎样,总会到达下标为 3 的位置。但该下标的最大跳跃长度是 0 , 所以永远不可能到达最后一个下标。

       

      提示:

      • 1 <= nums.length <= 3 * 104
      • 0 <= nums[i] <= 105
      + +## template + +```python +class Solution(object): + def canJump(self, nums): + """ + :type nums: List[int] + :rtype: bool + """ + length = len(nums) + begin = length - 1 + for i in reversed(range(length - 1)): + if i + nums[i] >= begin: + begin = i + return not begin +# %% +s = Solution() +print(s.canJump(nums = [2,3,1,1,4])) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/54.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/54.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..e9d3d923f19f7b78a08abda1e8cb6bbf3eb062af --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/54.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-822f7468eacf491382a371ff4e9a5d01", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/54.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/54.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..31a515f97e2dcf859cb8940690a643f13f80f700 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/54.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "d3a1fc8ea9614b09aa98feb179baabb5" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/54.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/3.python/54.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..8235c3cbdcb47cc66cb1bbd2a7c201f96cad8701 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/54.exercises/solution.md" @@ -0,0 +1,68 @@ +# 单词搜索 + +

      给定一个 m x n 二维字符网格 board 和一个字符串单词 word 。如果 word 存在于网格中,返回 true ;否则,返回 false

      单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。

       

      示例 1:

      输入:board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"
      输出:
      true

      示例 2:

      输入:board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "SEE"
      输出:
      true

      示例 3:

      输入:board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCB"
      输出:
      false

       

      提示:

      • m == board.length
      • n = board[i].length
      • 1 <= m, n <= 6
      • 1 <= word.length <= 15
      • boardword 仅由大小写英文字母组成

       

      进阶:你可以使用搜索剪枝的技术来优化解决方案,使其在 board 更大的情况下可以更快解决问题?

      + +## template + +```python +class Solution(object): + def exist(self, board, word): + """ + :type board: List[List[str]] + :type word: str + :rtype: bool + """ + check_board = [[True] * len(board[0]) for _ in range(len(board))] + for i in range(len(board)): + for j in range(len(board[0])): + if board[i][j] == word[0] and check_board: + check_board[i][j] = False + res = self.check_exist(check_board, board, word, 1, len(word), i, j) + if res: + return True + check_board[i][j] = True + return False + def check_exist(self, check_board, board, word, index, ls, row, col): + if index == ls: + return True + for temp in [(0, 1),(0, -1),(1, 0),(-1, 0)]: + curr_row = row + temp[0] + curr_col = col + temp[1] + if curr_row >= 0 and curr_row < len(board) and curr_col >= 0 and curr_col < len(board[0]): + if check_board[curr_row][curr_col] and board[curr_row][curr_col] == word[index]: + check_board[curr_row][curr_col] = False + res = self.check_exist(check_board, board, word, index + 1, len(word), curr_row, curr_col) + if res: + return res + check_board[curr_row][curr_col] = True + return False +if __name__ == "__main__": + s = Solution() + print (s.exist(board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED")) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/55.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/55.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..7c089d77b9a2e3fd5e33a28b48469c028d0422af --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/55.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-7be7c56471b448ea82bf422d4a6e480c", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/55.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/55.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..9039c20a108c2a01cd39c96a542bd9059557c540 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/55.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "88be681be092499f8a2ec1698067ed2a" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/55.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/3.python/55.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..b03fe400e3fc22330baa4171d32e38f801e7b291 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/55.exercises/solution.md" @@ -0,0 +1,128 @@ +# 验证二叉搜索树 + +
      +

      给你一个二叉树的根节点 root ,判断其是否是一个有效的二叉搜索树。

      + +

      有效 二叉搜索树定义如下:

      + +
        +
      • 节点的左子树只包含 小于 当前节点的数。
      • +
      • 节点的右子树只包含 大于 当前节点的数。
      • +
      • 所有左子树和右子树自身必须也是二叉搜索树。
      • +
      + +

       

      + +

      示例 1:

      + +
      输入:root = [2,1,3]
      +输出:true
      +
      + +

      示例 2:

      + +
      输入:root = [5,1,4,null,null,3,6]
      +输出:false
      +解释:根节点的值是 5 ,但是右子节点的值是 4 。
      +
      + +

       

      + +

      提示:

      + +
        +
      • 树中节点数目范围在[1, 104]
      • +
      • -231 <= Node.val <= 231 - 1
      • +
      +
      + +## template + +```python +import sys +class TreeNode(object): + def __init__(self, x): + self.val = x + self.left = None + self.right = None +class List2Tree(object): + def __init__(self, nums: list): + self.nums = nums + self.queue = [] + if len(nums) == 1: + self.root = TreeNode(self.nums.pop(0)) + else: + a = self.nums.pop(0) + b = self.nums.pop(0) + c = self.nums.pop(0) + self.root = TreeNode(a) + if b is not None: + self.root.left = TreeNode(b) + else: + self.root.left = b + if c is not None: + self.root.right = TreeNode(c) + else: + self.root.right = c + self.queue.append(self.root.left) + self.queue.append(self.root.right) + def convert(self): + while len(self.nums) > 0 and len(self.queue)> 0: + node = self.queue.pop(0) + if node is not None: + num= self.nums.pop(0) + if num is not None: + node.left = TreeNode(num) + else: + node.left = num + if len(self.nums) > 0: + num = self.nums.pop(0) + else: + num = None + if num is not None: + node.right = TreeNode(num) + else: + node.right = num + self.queue.append(node.left) + self.queue.append(node.right) + return self.root +class Solution(object): + def isValidBST(self, root): + root = List2Tree(root).convert() + return self.isVaild_helper(root, -sys.maxsize - 1, sys.maxsize) + def isVaild_helper(self, root, minVal, maxVal): + if root is None: + return True + if root.val >= maxVal or root.val <= minVal: + return False + return self.isVaild_helper(root.left, minVal, root.val) and self.isVaild_helper(root.right, root.val, maxVal) +# %% +s = Solution() +print(s.isValidBST([5,1,4,None,None,3,6])) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/56.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/56.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..d619cb4124f92a79087b8805f1da63805d25c356 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/56.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-d7267ceb26e5438bbd931e8f0a417d07", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/56.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/56.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..35117f8fce63a60a36cc55a90c236aaebaff883d --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/56.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "a4c947a3baf448458fa4acd54ac731eb" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/56.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/3.python/56.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..0f118aceb0088e51897cd5174eb27d9dd91a7ee4 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/56.exercises/solution.md" @@ -0,0 +1,88 @@ +# 删除排序链表中的重复元素 II + +

      存在一个按升序排列的链表,给你这个链表的头节点 head ,请你删除链表中所有存在数字重复情况的节点,只保留原始链表中 没有重复出现 的数字。

      返回同样按升序排列的结果链表。

       

      示例 1:

      输入:head = [1,2,3,3,4,4,5]
      输出:
      [1,2,5]

      示例 2:

      输入:head = [1,1,1,2,3]
      输出:
      [2,3]

       

      提示:

      • 链表中节点数目在范围 [0, 300]
      • -100 <= Node.val <= 100
      • 题目数据保证链表已经按升序排列
      + +## template + +```python +class ListNode(object): + def __init__(self, x): + self.val = x + self.next = None +class LinkList: + def __init__(self): + self.head=None + def initList(self, data): + self.head = ListNode(data[0]) + r=self.head + p = self.head + for i in data[1:]: + node = ListNode(i) + p.next = node + p = p.next + return r + def convert_list(self,head): + ret = [] + if head == None: + return + node = head + while node != None: + ret.append(node.val) + node = node.next + return ret +class Solution(object): + def deleteDuplicates(self, head): + """ + :type head: ListNode + :rtype: ListNode + """ + newnodehead = None + newnode = None + node = head + while node: + lastval = node.val + if node.next and node.next.val == lastval: + while node and node.val == lastval: + node=node.next + continue + if not newnodehead: + newnode=ListNode(node.val) + newnodehead=newnode + else: + newnode.next=ListNode(node.val) + newnode=newnode.next + node = node.next + return newnodehead +# %% +l = LinkList() +list1 = [1,2,3,3,4,4,5] +l1 = l.initList(list1) +s = Solution() +print(l.convert_list(s.deleteDuplicates(l1))) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/57.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/57.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..df1a28390caf53df4db57a6513def2296bb135a2 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/57.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-71745d4b56d540c6aac972e016794483", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/57.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/57.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..bc2cee0a45f45a24db38642894aedbdfb9c7e717 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/57.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "67145aae9659416c9e724069ebf358bb" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/57.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/3.python/57.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..786552047df4417f28b43c110f5b0309421ba42d --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/57.exercises/solution.md" @@ -0,0 +1,123 @@ +# Z 字形变换 + +
      +

      将一个给定字符串 s 根据给定的行数 numRows ,以从上往下、从左到右进行 Z 字形排列。

      + +

      比如输入字符串为 "PAYPALISHIRING" 行数为 3 时,排列如下:

      + +
      +    P   A   H   N
      +    A P L S I I G
      +    Y   I   R
      + +

      之后,你的输出需要从左往右逐行读取,产生出一个新的字符串,比如:"PAHNAPLSIIGYIR"

      + +

      请你实现这个将字符串进行指定行数变换的函数:

      + +
      string convert(string s, int numRows);
      + +

       

      + +

      示例 1:

      + +
      输入:s = "PAYPALISHIRING", numRows = 3
      +输出:"PAHNAPLSIIGYIR"
      +
      + 示例 2: + +
      输入:s = "PAYPALISHIRING", numRows = 4
      +输出:"PINALSIGYAHRPI"
      +解释:
      +P     I    N
      +A   L S  I G
      +Y A   H R
      +P     I
      +
      + +

      示例 3:

      + +
      输入:s = "A", numRows = 1
      +输出:"A"
      +
      + +

       

      + +

      提示:

      + +
        +
      • 1 <= s.length <= 1000
      • +
      • s 由英文字母(小写和大写)、',''.' 组成
      • +
      • 1 <= numRows <= 1000
      • +
      +
      + +## template + +```python +import math +class Solution: + def convert(self, s: str, numRows: int) -> str: + n = len(s) + N = numRows + if n == 1 or N == 1: + return s + S = N-2 + C = 2*N-2 + R = int(math.floor(n/C)) + RS = n % (C) + CE = n-R*C + RR = 1 if (RS <= N) else 1+(RS-N) + RX = R*(N-1) + RR + output = [] + i = 0 + while i < N: + j = 0 + k = (N-1-i) + while j < RX: + r = int(math.floor(j/(N-1))) + rs = j % (N-1) + offset = i if rs == 0 else N+rs-1 + index = r*C+offset + if index < len(s): + output.append(s[index]) + if i > 0 and i < N-1: + r = int(math.floor(k/(N-1))) + rs = k % (N-1) + offset = i if rs == 0 else N+rs-1 + index = r*C+offset + if index < len(s): + output.append(s[index]) + j += (N-1) + k += (N-1) + i += 1 + return ''.join(output) +# %% +s = Solution() +print(s.convert('PAYPALISHIRING', 3)) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/58.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/58.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..fd65cb7ab41de6b164ad8330932b2fd8f0ea23fb --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/58.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-08cd96ad9d334c80a262fe04c5b6bf21", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/58.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/58.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..d246f7a97f673698ae85aa54cf6b095001edeadb --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/58.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "e20a0612eae545ee916b48ae6887f724" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/58.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/3.python/58.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..6f2937cd61c08ae03dc7fd388ac365783714c300 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/58.exercises/solution.md" @@ -0,0 +1,78 @@ +# 组合总和 II + +

      给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。 +

      +

      candidates 中的每个数字在每个组合中只能使用一次。

      +

      说明:

      +
        +
      • 所有数字(包括目标数)都是正整数。
      • +
      • 解集不能包含重复的组合。 
      • +
      +

      示例 1:

      +
      输入: candidates = [10,1,2,7,6,1,5], target = 8,
      所求解集为:
      [[1, 7],[1, 2, 5],[2, 6],[1, 1, 6]]
      +

      示例 2:

      +
      输入: candidates = [2,5,2,1,2], target = 5,
      所求解集为:
      [[1,2,2],[5]]
      + +## template + +```python +class Solution(object): + def combinationSum2(self, candidates, target): + """ + :type candidates: List[int] + :type target: int + :rtype: List[List[int]] + """ + candidates.sort() + dp = [[] for _ in range(target + 1)] + dp[0].append([]) + for i in range(1, target + 1): + for j in range(len(candidates)): + if candidates[j] > i: + break + for k in range(len(dp[i - candidates[j]])): + temp = dp[i - candidates[j]][k][:] + if len(temp) > 0 and temp[-1] >= j: + continue + temp.append(j) + dp[i].append(temp) + res = [] + check = {} + for temp in dp[target]: + value = [candidates[t] for t in temp] + try: + check[str(value)] += 1 + except KeyError: + check[str(value)] = 1 + res.append(value) + return res +# %% +s = Solution() +print(s.combinationSum2(candidates = [2,5,2,1,2], target = 5)) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/59.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/59.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..998ba3619ecf8aeae736d61568b6a2cfa43a8d55 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/59.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-de79c36da4c04b35b24c32ba2146247f", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/59.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/59.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..e129587e99b8df6dad38cd7565d64aff18aa07f1 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/59.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "9e9ce080fa5f4e54814ec71542a2bb6e" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/59.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/3.python/59.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..04e96eae2b33601d2b683804e76aef20c450fcce --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/59.exercises/solution.md" @@ -0,0 +1,53 @@ +# 螺旋矩阵 II + +

      给你一个正整数 n ,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的 n x n 正方形矩阵 matrix

       

      示例 1:

      输入:n = 3
      输出:
      [[1,2,3],[8,9,4],[7,6,5]]

      示例 2:

      输入:n = 1
      输出:
      [[1]]

       

      提示:

      • 1 <= n <= 20
      + +## template + +```python +class Solution(object): + def generateMatrix(self, n): + """ + :type n: int + :rtype: List[List[int]] + """ + res = [[0] * n for _ in range(n)] + pos = [0, 0] + move = (0, 1) + for index in range(1, n * n + 1): + res[pos[0]][pos[1]] = index + if res[(pos[0] + move[0]) % n][(pos[1] + move[1]) % n] > 0: + move = (move[1], -1 * move[0]) + pos[0] = pos[0] + move[0] + pos[1] = pos[1] + move[1] + return res +if __name__ == '__main__': + s = Solution() + print (s.generateMatrix(2)) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/6.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/6.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..704083e7e9972880a5d73ab2dffbd3cf53db2b13 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/6.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-058775ec19854ea2891221ff1d7f5114", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/6.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/6.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..7ec1e11f68e755f65adccda94b2dd6590fb34286 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/6.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "4c1dc35211684a928aaeb8a14f2145d2" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/6.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/3.python/6.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..428c63c1545f8b2ca24294b1ea6ca44b5b05a829 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/6.exercises/solution.md" @@ -0,0 +1,48 @@ +# 子集 + +

      给你一个整数数组 nums ,数组中的元素 互不相同 。返回该数组所有可能的子集(幂集)。

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

       

      示例 1:

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

      示例 2:

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

       

      提示:

      • 1 <= nums.length <= 10
      • -10 <= nums[i] <= 10
      • nums 中的所有元素 互不相同
      + +## template + +```python +class Solution(object): + def subsets(self, nums): + nums.sort() + res = [[]] + for index in range(len(nums)): + size = len(res) + for j in range(size): + curr = list(res[j]) + curr.append(nums[index]) + res.append(curr) + return res +if __name__ == "__main__": + s = Solution() + print (s.subsets([1,2,3])) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/60.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/60.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..bea38443c3d1423a645f2b6861780cd2980b649b --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/60.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-9b1f17fb796a4788a53d409fc3d0af39", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/60.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/60.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..a0250b95dd7384e06dde0c9477e3774e54fae77f --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/60.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "ced426d25758494baad05df1380c2cf8" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/60.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/3.python/60.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..d88bdb2db37761573f03b3da04e6fe778e467ffa --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/60.exercises/solution.md" @@ -0,0 +1,78 @@ +# 两两交换链表中的节点 + +

      给定一个链表,两两交换其中相邻的节点,并返回交换后的链表。

      你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。

       

      示例 1:

      输入:head = [1,2,3,4]
      输出:
      [2,1,4,3]

      示例 2:

      输入:head = []
      输出:
      []

      示例 3:

      输入:head = [1]
      输出:
      [1]

       

      提示:

      • 链表中节点的数目在范围 [0, 100]
      • 0 <= Node.val <= 100

       

      进阶:你能在不修改链表节点值的情况下解决这个问题吗?(也就是说,仅修改节点本身。)

      + +## template + +```python +class ListNode(object): + def __init__(self, x): + self.val = x + self.next = None +class LinkList: + def __init__(self): + self.head=None + def initList(self, data): + self.head = ListNode(data[0]) + r=self.head + p = self.head + for i in data[1:]: + node = ListNode(i) + p.next = node + p = p.next + return r + def convert_list(self,head): + ret = [] + if head == None: + return + node = head + while node != None: + ret.append(node.val) + node = node.next + return ret +class Solution(object): + def swapPairs(self, head): + dummyHead = ListNode(-1) + dummyHead.next = head + prev, p = dummyHead, head + while p != None and p.next != None: + q, r = p.next, p.next.next + prev.next = q + q.next = p + p.next = r + prev = p + p = r + return dummyHead.next +# %% +l = LinkList() +head = [1,2,3,4] +l1 = l.initList(head) +s = Solution() +print(l.convert_list(s.swapPairs(l1))) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/61.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/61.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..9f752ec9333087bd450f1e35b1018aa12efee266 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/61.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-a8bf9fba303146ed81cbc9495713b609", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/61.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/61.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..662b108757747db3fded181b79ad0dd5fe60a753 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/61.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "ce911abe25874fe49d667e2992981217" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/61.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/3.python/61.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..5d320a4b4abae3591f632c0fb8fa83fd75439b6b --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/61.exercises/solution.md" @@ -0,0 +1,51 @@ +# 颜色分类 + +

      给定一个包含红色、白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色、白色、蓝色顺序排列。

      此题中,我们使用整数 0、 12 分别表示红色、白色和蓝色。

         

        示例 1:

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

        示例 2:

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

        示例 3:

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

        示例 4:

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

         

        提示:

        • n == nums.length
        • 1 <= n <= 300
        • nums[i]012

         

        进阶:

        • 你可以不使用代码库中的排序函数来解决这道题吗?
        • 你能想出一个仅使用常数空间的一趟扫描算法吗?
        + +## template + +```python +class Solution(object): + def sortColors(self, nums): + low, mid, high = 0, 0, len(nums) - 1 + while mid <= high: + if nums[mid] == 0: + nums[low], nums[mid] = nums[mid], nums[low] + low += 1 + mid += 1 + elif nums[mid] == 1: + mid += 1 + else: + nums[high], nums[mid] = nums[mid], nums[high] + high -= 1 + return nums +# %% +s = Solution() +print(s.sortColors(nums = [2,0,2,1,1,0])) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/62.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/62.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..afbd6238c278cab352ebd95b82b544af946d324e --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/62.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-62e563bf451c4507bb3b37fc3aac51af", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/62.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/62.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..d4e50b6ebe44af9761eeefebc8d9367f6aadeb98 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/62.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "5e791071c5d14878a47d7ce3411965f2" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/62.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/3.python/62.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..506cf68deb1e425f99269ce378ef707e4e580a98 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/62.exercises/solution.md" @@ -0,0 +1,73 @@ +# 不同路径 II + +

        一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” )。

        +

        机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为“Finish”)。

        +

        现在考虑网格中有障碍物。那么从左上角到右下角将会有多少条不同的路径?

        +

        +

        网格中的障碍物和空位置分别用 10 来表示。

        +

         

        +

        示例 1:

        +
        输入:obstacleGrid = [[0,0,0],[0,1,0],[0,0,0]]
        输出:
        2
        解释:
        3x3 网格的正中间有一个障碍物。从左上角到右下角一共有 2 条不同的路径:
        1. 向右 -> 向右 -> 向下 -> 向下
        2. 向下 -> 向下 -> 向右 -> 向右
        +

        示例 2:

        +
        输入:obstacleGrid = [[0,1],[0,0]]
        输出:
        1
        +

         

        +

        提示:

        +
          +
        • m == obstacleGrid.length
        • +
        • n == obstacleGrid[i].length
        • +
        • 1 <= m, n <= 100
        • +
        • obstacleGrid[i][j]01
        • +
        + +## template + +```python +class Solution(object): + def uniquePathsWithObstacles(self, obstacleGrid): + m, n = len(obstacleGrid), len(obstacleGrid[0]) + if m == 0: + return 0 + dmap = [[0] * (n + 1) for _ in range(m + 1)] + dmap[m - 1][n] = 1 + for i in range(m - 1, -1, -1): + for j in range(n - 1, -1, -1): + if obstacleGrid[i][j] == 1: + dmap[i][j] = 0 + else: + dmap[i][j] = dmap[i][j + 1] + dmap[i + 1][j] + return dmap[0][0] +# %% +s = Solution() +print(s.uniquePathsWithObstacles(obstacleGrid = [[0,1],[0,0]])) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/63.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/63.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..cecd7810c8a46af0748b2442cecb4ded19fd2c5b --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/63.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-07a1c73140e840098d9fed5a3a1f5a3f", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/63.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/63.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..23596b0714c94511b3bc661af7fe4a2e4027ec93 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/63.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "5feba09aff924b67b4fee76d96c450cb" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/63.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/3.python/63.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..a64f6287d3c7d05943468ac65437b5f0cb2ccdf7 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/63.exercises/solution.md" @@ -0,0 +1,115 @@ +# 有效的数独 + +

        请你判断一个 9x9 的数独是否有效。只需要 根据以下规则 ,验证已经填入的数字是否有效即可。

        +
          +
        1. 数字 1-9 在每一行只能出现一次。
        2. +
        3. 数字 1-9 在每一列只能出现一次。
        4. +
        5. 数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。(请参考示例图)
        6. +
        +

        数独部分空格内已填入了数字,空白格用 '.' 表示。

        +

        注意:

        +
          +
        • 一个有效的数独(部分已被填充)不一定是可解的。
        • +
        • 只需要根据以上规则,验证已经填入的数字是否有效即可。
        • +
        +

         

        +

        示例 1:

        +
        输入:board = 
        +    [["5","3",".",".","7",".",".",".","."]
        +    ,["6",".",".","1","9","5",".",".","."]
        +    ,[".","9","8",".",".",".",".","6","."]
        +    ,["8",".",".",".","6",".",".",".","3"]
        +    ,["4",".",".","8",".","3",".",".","1"]
        +    ,["7",".",".",".","2",".",".",".","6"]
        +    ,[".","6",".",".",".",".","2","8","."]
        +    ,[".",".",".","4","1","9",".",".","5"]
        +    ,[".",".",".",".","8",".",".","7","9"]]
        +输出:true
        +
        +

        示例 2:

        +
        输入:board = 
        +    [["8","3",".",".","7",".",".",".","."]
        +    ,["6",".",".","1","9","5",".",".","."]
        +    ,[".","9","8",".",".",".",".","6","."]
        +    ,["8",".",".",".","6",".",".",".","3"]
        +    ,["4",".",".","8",".","3",".",".","1"]
        +    ,["7",".",".",".","2",".",".",".","6"]
        +    ,[".","6",".",".",".",".","2","8","."]
        +    ,[".",".",".","4","1","9",".",".","5"]
        +    ,[".",".",".",".","8",".",".","7","9"]]
        +输出:false
        +解释:除了第一行的第一个数字从 5 改为 8 以外,空格内其他数字均与 示例1 相同。 但由于位于左上角的 3x3 宫内有两个 8 存在, 因此这个数独是无效的。
        +

         

        +

        提示:

        +
          +
        • board.length == 9
        • +
        • board[i].length == 9
        • +
        • board[i][j] 是一位数字或者 '.'
        • +
        + +## template + +```python +from typing import List +class Solution: + def isValidSudoku(self, board): + """ + :type board: List[List[str]] + :rtype: bool + """ + raw = [{},{},{},{},{},{},{},{},{}] + col = [{},{},{},{},{},{},{},{},{}] + cell = [{},{},{},{},{},{},{},{},{}] + for i in range(9): + for j in range(9): + num = (3*(i//3) + j//3) + temp = board[i][j] + if temp != ".": + if temp not in raw[i] and temp not in col[j] and temp not in cell[num]: + raw [i][temp] = 1 + col [j][temp] = 1 + cell [num][temp] =1 + else: + return False + return True +# %% +s = Solution() +board = [["5","3",".",".","7",".",".",".","."] + ,["6",".",".","1","9","5",".",".","."] + ,[".","9","8",".",".",".",".","6","."] + ,["8",".",".",".","6",".",".",".","3"] + ,["4",".",".","8",".","3",".",".","1"] + ,["7",".",".",".","2",".",".",".","6"] + ,[".","6",".",".",".",".","2","8","."] + ,[".",".",".","4","1","9",".",".","5"] + ,[".",".",".",".","8",".",".","7","9"]] +print(s.isValidSudoku(board)) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/7.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/7.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..18abf87479393f5990b04cd5893a3229e6edd3dc --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/7.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-31b8d23b8ae64c169b0a77c468d0f2fd", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/7.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/7.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..81b78f74303dc9d4fda5a17003fd6d7e6f5776bc --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/7.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "074a7f49e09f4df3ad8d8e1e62d2c475" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/7.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/3.python/7.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..eeff7b84b021d4b340a895cda11f9881fdf49788 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/7.exercises/solution.md" @@ -0,0 +1,76 @@ +# 不同路径 + +

        一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为 “Start” )。

        +

        机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为 “Finish” )。

        +

        问总共有多少条不同的路径?

        +

         

        +

        示例 1:

        +
        输入:m = 3, n = 7
        输出:
        28
        +

        示例 2:

        +
        输入:m = 3, n = 2
        输出:
        3
        解释:
        从左上角开始,总共有 3 条路径可以到达右下角。
        1. 向右 -> 向下 -> 向下
        2. 向下 -> 向下 -> 向右
        3. 向下 -> 向右 -> 向下
        +

        示例 3:

        +
        输入:m = 7, n = 3
        输出:
        28
        +

        示例 4:

        +
        输入:m = 3, n = 3
        输出:
        6
        +

         

        +

        提示:

        +
          +
        • 1 <= m, n <= 100
        • +
        • 题目数据保证答案小于等于 2 * 109
        • +
        + +## template + +```python +class Solution: + def uniquePaths(self, m, n): + """ + :type m: int + :type n: int + :rtype: int + """ + dmap = [[0] * n for _ in range(m)] + for i in range(m): + dmap[i][0] = 1 + for j in range(n): + dmap[0][j] = 1 + for i in range(1, m): + for j in range(1, n): + l = u = 0 + if i-1 >= 0: + u = dmap[i-1][j] + if j-1>= 0: + l = dmap[i][j-1] + dmap[i][j] = l + u + return dmap[m-1][n-1] +# %% +s = Solution() +print(s.uniquePaths(m = 3, n = 7)) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/8.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/8.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..9ce84ff48d642470dd17bc031b127ec80636ef10 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/8.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-f0d5b528d01143009d990abbe6e7a59b", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/8.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/8.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..7d5fad4b6479744221d68b2e3c6ee6484443779f --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/8.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "a2b97d4bc0ee416b9236f86e82412f81" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/8.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/3.python/8.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..0fd25b8102288895705b7243368bc4026bc18f70 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/8.exercises/solution.md" @@ -0,0 +1,80 @@ +# 电话号码的字母组合 + +

        给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。答案可以按 任意顺序 返回。

        给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。

         

        示例 1:

        输入:digits = "23"
        输出:
        ["ad","ae","af","bd","be","bf","cd","ce","cf"]

        示例 2:

        输入:digits = ""
        输出:
        []

        示例 3:

        输入:digits = "2"
        输出:
        ["a","b","c"]

         

        提示:

        • 0 <= digits.length <= 4
        • digits[i] 是范围 ['2', '9'] 的一个数字。
        + +## template + +```python +from typing import List +class Solution: + def letterCombinations(self, digits: str) -> List[str]: + letters = [ + [], + [], + ['a', 'b', 'c'], + ['d', 'e', 'f'], + ['g', 'h', 'i'], + ['j', 'k', 'l'], + ['m', 'n', 'o'], + ['p', 'q', 'r', 's'], + ['t', 'u', 'v'], + ['w', 'x', 'y', 'z'], + ] + combinations = [] + i = 0 + for d in digits: + letter = letters[int(d)] + if i == 0: + for l in letter: + combinations.append([l]) + else: + added = [] + for c in combinations: + j = 0 + origin_c = [] + origin_c += c + for l in letter: + if j == 0: + c.append(l) + else: + new_c = [] + new_c += origin_c + new_c.append(l) + added.append(new_c) + j += 1 + combinations += added + i += 1 + output = [] + for c in combinations: + output.append(''.join(c)) + return output +# %% +s = Solution() +print(s.letterCombinations(digits = "23")) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/9.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/9.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..0224d246053415e7f569149dd148304b64a356b8 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/9.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-a1df98e5cfe94818bac58e03cb132efd", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/9.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/3.python/9.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..a476fd02d5a514201e66f92ba99874d61f694704 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/9.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "88c9e810c8df4cd1a8e6f6629ec83b1d" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/3.python/9.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/3.python/9.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..0294458bceae40daa95868123f4fac3db0cb6dfb --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/3.python/9.exercises/solution.md" @@ -0,0 +1,122 @@ +# 不同的二叉搜索树 II + +
        +

        给你一个整数 n ,请你生成并返回所有由 n 个节点组成且节点值从 1n 互不相同的不同 + 二叉搜索树 。可以按 任意顺序 返回答案。 +

        + +

         

        + +
        +
        +

        示例 1:

        + +
        输入:n = 3
        +输出:[[1,null,2,null,3],[1,null,3,2],[2,1,3],[3,1,null,null,2],[3,2,null,1]]
        +    
        + +

        示例 2:

        + +
        输入:n = 1
        +输出:[[1]]
        +    
        + +

         

        + +

        提示:

        + +
          +
        • 1 <= n <= 8
        • +
        +
        +
        +
        + +## template + +```python +class TreeNode(object): + def __init__(self, x): + self.val = x + self.left = None + self.right = None + def to_list(self, count): + queue = [] + queue.append(self) + result = [] + while len(queue) > 0: + if count == 0: + break + node = queue.pop(0) + if node is None: + result.append('null') + else: + count -= 1 + result.append(node.val) + queue.append(node.left) + queue.append(node.right) + return result +class Solution(object): + def generateTrees(self, n): + """ + :type n: int + :rtype: List[TreeNode] + """ + if n == 0: + return [] + return self.get_trees(1, n) + def get_trees_impl(self, start, end): + trees = [] + if start > end: + trees.append(None) + return trees + for i in range(start, end + 1): + lefts = self.get_trees_impl(start, i - 1) + rights = self.get_trees_impl(i + 1, end) + for j in range(len(lefts)): + for k in range(len(rights)): + root = TreeNode(i) + root.left = lefts[j] + root.right = rights[k] + trees.append(root) + return trees + def get_trees(self, start, end): + trees = self.get_trees_impl(start, end) + results = [] + for tree in trees: + if tree is None: + results.append([]) + else: + results.append(tree.to_list(end)) + return results +# %% +s = Solution() +print(s.generateTrees(n=3)) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/21.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/21.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..fef639c0c4e7dce62b145e743609b011c57ff5ae --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/21.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-a51756df573f42928d4a18d988bfcbcf", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/21.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/21.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..0996b2d42ee57a6dac3b3d2503f5cc7dcb02a4cd --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/21.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "865c5099b3fc48d4923176b9f33e261b" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/21.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/21.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..679ebb8699de1564adc2ffdab0875c3244a072e9 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/21.exercises/solution.md" @@ -0,0 +1,159 @@ +# 解数独 + +

        编写一个程序,通过填充空格来解决数独问题。

        +

        数独的解法需 遵循如下规则

        +
          +
        1. 数字 1-9 在每一行只能出现一次。
        2. +
        3. 数字 1-9 在每一列只能出现一次。
        4. +
        5. 数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。(请参考示例图)
        6. +
        +

        数独部分空格内已填入了数字,空白格用 '.' 表示。

        +

         

        +
        +
        +
        +

        示例:

        +
        输入:board = 
        +    [["5","3",".",".","7",".",".",".","."],
        +    ["6",".",".","1","9","5",".",".","."],
        +    [".","9","8",".",".",".",".","6","."],
        +    ["8",".",".",".","6",".",".",".","3"],
        +    ["4",".",".","8",".","3",".",".","1"],
        +    ["7",".",".",".","2",".",".",".","6"],
        +    [".","6",".",".",".",".","2","8","."],
        +    [".",".",".","4","1","9",".",".","5"],
        +    [".",".",".",".","8",".",".","7","9"]]
        +输出:
        +    [["5","3","4","6","7","8","9","1","2"],
        +    ["6","7","2","1","9","5","3","4","8"],
        +    ["1","9","8","3","4","2","5","6","7"],
        +    ["8","5","9","7","6","1","4","2","3"],
        +    ["4","2","6","8","5","3","7","9","1"],
        +    ["7","1","3","9","2","4","8","5","6"],
        +    ["9","6","1","5","3","7","2","8","4"],
        +    ["2","8","7","4","1","9","6","3","5"],
        +    ["3","4","5","2","8","6","1","7","9"]]
        +解释:输入的数独如上图所示,唯一有效的解决方案如下所示:
        +

         

        +
        + + +

         

        +

        提示:

        +
          +
        • board.length == 9
        • +
        • board[i].length == 9
        • +
        • board[i][j] 是一位数字或者 '.'
        • +
        • 题目数据 保证 输入数独仅有一个解
        • +
        +
        +
        +
        + +## template + +```cpp +#include +using namespace std; +class Solution +{ +public: + void solveSudoku(vector> &board) + { + int size = board.size(); + vector> rows(size, vector(10)); + vector> cols(size, vector(10)); + vector> boxes(size, vector(10)); + for (int i = 0; i < size; i++) + { + for (int j = 0; j < size; j++) + { + if (board[i][j] != '.') + { + int num = board[i][j] - '0'; + int idx = i / 3 * 3 + j / 3; + rows[i][num] = true; + cols[j][num] = true; + boxes[idx][num] = true; + } + } + } + dfs(board, 0, rows, cols, boxes); + } +private: + bool valid(int num, int row, int col, int idx, vector> &rows, + vector> &cols, vector> &boxes) + { + return !rows[row][num] && !cols[col][num] && !boxes[idx][num]; + } + bool dfs(vector> &board, int size, vector> &rows, + vector> &cols, vector> &boxes) + { + if (size == 9 * 9) + { + return true; + } + else + { + bool ok = false; + int row = size / 9; + int col = size % 9; + int idx = row / 3 * 3 + col / 3; + if (board[row][col] == '.') + { + for (int i = 1; i <= 9; i++) + { + if (valid(i, row, col, idx, rows, cols, boxes)) + { + board[row][col] = i + '0'; + rows[row][i] = true; + cols[col][i] = true; + boxes[idx][i] = true; + ok = dfs(board, size + 1, rows, cols, boxes); + if (!ok) + { + rows[row][i] = false; + cols[col][i] = false; + boxes[idx][i] = false; + board[row][col] = '.'; + } + } + } + } + else + { + ok = dfs(board, size + 1, rows, cols, boxes); + } + return ok; + } + } +}; +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/22.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/22.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..eea63ed5a4eb85dcd35e7dbc66628e04b7a06662 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/22.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-67e4939fcc9745eca46d76f21e815ef3", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/22.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/22.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..9f2a5bceeadd58111c930979e5bc0a6cabc78c30 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/22.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "85b86d5555e64501acac8896e084fe76" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/22.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/22.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..b087ab5e04362f43b7a1e0fc33b34ba8c611d29b --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/22.exercises/solution.md" @@ -0,0 +1,74 @@ +# 接雨水 + +

        给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。

         

        示例 1:

        输入:height = [0,1,0,2,1,0,1,3,2,1,2,1]
        输出:
        6
        解释:
        上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况下,可以接 6 个单位的雨水(蓝色部分表示雨水)。

        示例 2:

        输入:height = [4,2,0,3,2,5]
        输出:
        9

         

        提示:

        • n == height.length
        • 0 <= n <= 3 * 104
        • 0 <= height[i] <= 105
        + +## template + +```cpp +#include +using namespace std; +class Solution +{ +public: + int trap(vector &height) + { + int res = 0; + int left = 0, left_max = 0; + int right = height.size() - 1, right_max = 0; + while (left < right) + { + if (height[left] < height[right]) + { + if (height[left] > left_max) + { + left_max = height[left]; + } + else + { + res += left_max - height[left]; + } + left++; + } + else + { + if (height[right] > right_max) + { + right_max = height[right]; + } + else + { + res += right_max - height[right]; + } + right--; + } + } + return res; + } +}; +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/23.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/23.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..921e39a77739001f68cf4a795603f5a79af5beb3 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/23.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-eda9311b71a24e5fbc9fe11952282642", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/23.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/23.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..c3e119980c2e698feda5d17194c45efd4bda5c7f --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/23.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "90b9917e4068435e8e878fb94d1184aa" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/23.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/23.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..b9b512a30a5fa9e4ede57a37501a9025d3e7bb78 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/23.exercises/solution.md" @@ -0,0 +1,76 @@ +# 柱状图中最大的矩形 + +

        给定 n 个非负整数,用来表示柱状图中各个柱子的高度。每个柱子彼此相邻,且宽度为 1 。

        求在该柱状图中,能够勾勒出来的矩形的最大面积。

         

        以上是柱状图的示例,其中每个柱子的宽度为 1,给定的高度为 [2,1,5,6,2,3]

         

        图中阴影部分为所能勾勒出的最大矩形面积,其面积为 10 个单位。

         

        示例:

        输入: [2,1,5,6,2,3]
        输出:
        10
        + +## template + +```cpp +#include +#include +static int largestRectangleArea(int *heights, int heightsSize) +{ + int *indexes = malloc(heightsSize * sizeof(int)); + int *left = malloc(heightsSize * sizeof(int)); + int *right = malloc(heightsSize * sizeof(int)); + int i, pos = 0; + for (i = 0; i < heightsSize; i++) + { + while (pos > 0 && heights[indexes[pos - 1]] >= heights[i]) + { + pos--; + } + left[i] = pos == 0 ? -1 : indexes[pos - 1]; + indexes[pos++] = i; + } + pos = 0; + for (i = heightsSize - 1; i >= 0; i--) + { + while (pos > 0 && heights[indexes[pos - 1]] >= heights[i]) + { + pos--; + } + right[i] = pos == 0 ? heightsSize : indexes[pos - 1]; + indexes[pos++] = i; + } + int max_area = 0; + for (i = 0; i < heightsSize; i++) + { + int area = heights[i] * (right[i] - left[i] - 1); + max_area = area > max_area ? area : max_area; + } + return max_area; +} +int main(void) +{ + int nums[] = {2, 1, 5, 6, 2, 3}; + int count = sizeof(nums) / sizeof(*nums); + printf("%d\n", largestRectangleArea(nums, count)); + return 0; +} +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/24.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/24.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..359d029b4dd051fb127eb5e3958f7464e87f9ccf --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/24.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-86fcd8c9fd054121a6e095675e7db5f0", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/24.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/24.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..a0a394369163101cfe66373e65b38727e4f433a7 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/24.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "ed8d916add1c4a3899f1bf340ce3850f" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/24.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/24.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..869babd087285e71435d6e7d806271ce8f343ad3 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/24.exercises/solution.md" @@ -0,0 +1,67 @@ +# 缺失的第一个正数 + +

        给你一个未排序的整数数组 nums ,请你找出其中没有出现的最小的正整数。

         

        进阶:你可以实现时间复杂度为 O(n) 并且只使用常数级别额外空间的解决方案吗?

         

        示例 1:

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

        示例 2:

        输入:nums = [3,4,-1,1]
        输出:
        2

        示例 3:

        输入:nums = [7,8,9,11,12]
        输出:
        1

         

        提示:

        • 0 <= nums.length <= 300
        • -231 <= nums[i] <= 231 - 1
        + +## template + +```cpp +#include +using namespace std; +class Solution +{ +public: + int firstMissingPositive(vector &nums) + { + if (nums.size() == 0) + { + return 1; + } + int i = 0; + while (i < nums.size()) + { + if (nums[i] > 0 && nums[i] != i + 1 && nums[i] - 1 < nums.size() && nums[nums[i] - 1] != nums[i]) + { + swap(nums[i], nums[nums[i] - 1]); + } + else + { + i++; + } + } + for (i = 0; i < nums.size(); i++) + { + if (nums[i] != i + 1) + { + break; + } + } + return i + 1; + } +}; +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/25.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/25.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..c7e3b4aa5ee69b92d7ef90a18cd66383e1e900b9 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/25.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-9cf1cbd5546c43479e105a25aaae4223", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/25.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/25.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..50058cd63e391c4f6b2a7ee45e2dd4e6bfdff15f --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/25.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "46d5841fa984479cbe7c482363d3b51e" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/25.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/25.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..4fbd3061d4af8a5a6e5c5c90f7777a06407720c5 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/25.exercises/solution.md" @@ -0,0 +1,110 @@ +# N皇后 II + +
        +

        n 皇后问题 研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击。

        + +

        给你一个整数 n ,返回 n 皇后问题 不同的解决方案的数量。

        + +

         

        + +
        +
        +

        示例 1:

        + +
        输入:n = 4
        +输出:2
        +解释:如上图所示,4 皇后问题存在两个不同的解法。
        +    
        + +

        示例 2:

        + +
        输入:n = 1
        +输出:1
        +    
        + +

         

        + +

        提示:

        + +
          +
        • 1 <= n <= 9
        • +
        • 皇后彼此不能相互攻击,也就是说:任何两个皇后都不能处于同一条横行、纵行或斜线上。
        • +
        +
        +
        +
        + +## template + +```cpp +#include +using namespace std; +class Solution +{ +public: + int totalNQueens(int n) + { + vector stack(n); + return dfs(n, 0, stack); + } +private: + int dfs(int n, int row, vector &stack) + { + int count = 0; + if (row == n) + { + return count + 1; + } + else + { + for (int i = 0; i < n; i++) + { + if (row == 0 || !conflict(stack, row, i)) + { + stack[row] = i; + count += dfs(n, row + 1, stack); + } + } + return count; + } + } + bool conflict(vector &stack, int row, int col) + { + for (int i = 0; i < row; i++) + { + if (col == stack[i] || abs(row - i) == abs(col - stack[i])) + { + return true; + } + } + return false; + } +} +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/26.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/26.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..9b67d7be4d58b53efce95ac5e7a0dc3a192126a6 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/26.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-eb1373e25770417fb84ce8df64a245eb", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/26.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/26.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..5be66e057ddf2ac27db6f6ee999be43de3e3d8c3 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/26.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "d930a40f5d124f82a9e02f8b1f68996a" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/26.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/26.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..e5570506ac6266ff91bed4bc991cfd76e6310448 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/26.exercises/solution.md" @@ -0,0 +1,107 @@ +# 最大矩形 + +

        给定一个仅包含 01 、大小为 rows x cols 的二维二进制矩阵,找出只包含 1 的最大矩形,并返回其面积。

         

        示例 1:

        输入:matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]
        输出:
        6
        解释:
        最大矩形如上图所示。

        示例 2:

        输入:matrix = []
        输出:
        0

        示例 3:

        输入:matrix = [["0"]]
        输出:
        0

        示例 4:

        输入:matrix = [["1"]]
        输出:
        1

        示例 5:

        输入:matrix = [["0","0"]]
        输出:
        0

         

        提示:

        • rows == matrix.length
        • cols == matrix[0].length
        • 0 <= row, cols <= 200
        • matrix[i][j]'0''1'
        + +## template + +```cpp +#include +#include +#include +#include +static inline int max(int a, int b) +{ + return a > b ? a : b; +} +static int area_calc(int *heights, int size) +{ + int *indexes = malloc(size * sizeof(int)); + int *lhist = malloc(size * sizeof(int)); + int *rhist = malloc(size * sizeof(int)); + int i, pos = 0; + for (i = 0; i < size; i++) + { + while (pos > 0 && heights[indexes[pos - 1]] >= heights[i]) + { + pos--; + } + lhist[i] = pos == 0 ? -1 : indexes[pos - 1]; + indexes[pos++] = i; + } + pos = 0; + for (i = size - 1; i >= 0; i--) + { + while (pos > 0 && heights[indexes[pos - 1]] >= heights[i]) + { + pos--; + } + rhist[i] = pos == 0 ? size : indexes[pos - 1]; + indexes[pos++] = i; + } + int max_area = 0; + for (i = 0; i < size; i++) + { + int area = heights[i] * (rhist[i] - lhist[i] - 1); + max_area = max(area, max_area); + } + return max_area; +} +static int maximalRectangle(char **matrix, int matrixRowSize, int matrixColSize) +{ + int i, j, max_area = 0; + int *heights = malloc(matrixColSize * sizeof(int)); + memset(heights, 0, matrixColSize * sizeof(int)); + for (i = 0; i < matrixRowSize; i++) + { + for (j = 0; j < matrixColSize; j++) + { + heights[j] = matrix[i][j] == '1' ? heights[j] + 1 : 0; + } + max_area = max(max_area, area_calc(heights, matrixColSize)); + } + return max_area; +} +int main(int argc, char **argv) +{ + if (argc < 2) + { + fprintf(stderr, "Usage: ./test row1 row2...\n"); + exit(-1); + } + int i, j; + int row_size = argc - 1; + int col_size = strlen(argv[1]); + for (i = 0; i < row_size; i++) + { + printf("%s\n", argv[i + 1]); + } + printf("%d\n", maximalRectangle(argv + 1, argc - 1, strlen(argv[1]))); + return 0; +} +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/27.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/27.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..914cdd0e7f8dfcc510b1510c7a58170ed1eccee6 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/27.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-32953a2cc45d4c89926f73432ad499f8", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/27.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/27.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..b3f7e3154163ae7f7b32906a4841af11b95c383f --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/27.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "df4c754329a94348a239a488329d8d68" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/27.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/27.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..d5b70a04f3458decd4d3209e613b7480337affa2 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/27.exercises/solution.md" @@ -0,0 +1,70 @@ +# 编辑距离 + +

        给你两个单词 word1 和 word2,请你计算出将 word1 转换成 word2 所使用的最少操作数 。

        你可以对一个单词进行如下三种操作:

        • 插入一个字符
        • 删除一个字符
        • 替换一个字符

         

        示例 1:

        输入:word1 = "horse", word2 = "ros"
        输出:
        3
        解释:
        horse -> rorse (将 'h' 替换为 'r')rorse -> rose (删除 'r')rose -> ros (删除 'e')

        示例 2:

        输入:word1 = "intention", word2 = "execution"
        输出:
        5
        解释:
        intention -> inention (删除 't')inention -> enention (将 'i' 替换为 'e')enention -> exention (将 'n' 替换为 'x')exention -> exection (将 'n' 替换为 'c')exection -> execution (插入 'u')

         

        提示:

        • 0 <= word1.length, word2.length <= 500
        • word1word2 由小写英文字母组成
        + +## template + +```cpp +#include +using namespace std; +class Solution +{ +public: + int minDistance(string word1, string word2) + { + int l1 = word1.length(); + int l2 = word2.length(); + vector dp(l2 + 1); + for (int i = 0; i <= l2; i++) + { + dp[i] = i; + } + int up = 0; + for (int i = 1; i <= l1; i++) + { + int left_up = dp[0]; + dp[0] = i; + for (int j = 1; j <= l2; j++) + { + up = dp[j]; + if (word1[i - 1] == word2[j - 1]) + { + dp[j] = left_up; + } + else + { + dp[j] = 1 + min(left_up, min(up, dp[j - 1])); + } + left_up = up; + } + } + return dp[l2]; + } +}; +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/28.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/28.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..ebb66ca4ab778ac2aa44e1804f98ed85b0f356aa --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/28.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-6731520a26df4d47a57523eaecef0c02", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/28.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/28.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..3201fee55292c83472767a7f43a11e059c5146bf --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/28.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "6e362907346845e58e2c69959cf790a3" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/28.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/28.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..6de7cc7967d31c687f0983e27cfcb652ac8068c8 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/28.exercises/solution.md" @@ -0,0 +1,214 @@ +# 文本左右对齐 + +
        +

        给定一个单词数组和一个长度 maxWidth,重新排版单词,使其成为每行恰好有 maxWidth 个字符,且左右两端对齐的文本。

        + +

        你应该使用“贪心算法”来放置给定的单词;也就是说,尽可能多地往每行中放置单词。必要时可用空格 ' ' 填充,使得每行恰好有 maxWidth 个字符。 +

        + +

        要求尽可能均匀分配单词间的空格数量。如果某一行单词间的空格不能均匀分配,则左侧放置的空格数要多于右侧的空格数。

        + +

        文本的最后一行应为左对齐,且单词之间不插入额外的空格。

        + +

        说明:

        + +
          +
        • 单词是指由非空格字符组成的字符序列。
        • +
        • 每个单词的长度大于 0,小于等于 maxWidth
        • +
        • 输入单词数组 words 至少包含一个单词。
        • +
        + +

        示例:

        + +
        输入:
        +    words = ["This", "is", "an", "example", "of", "text", "justification."]
        +    maxWidth = 16
        +输出:
        +    [
        +       "This    is    an",
        +       "example  of text",
        +       "justification.  "
        +    ]
        +    
        + +

        示例 2:

        + +
        输入:
        +    words = ["What","must","be","acknowledgment","shall","be"]
        +    maxWidth = 16
        +输出:
        +    [
        +      "What   must   be",
        +      "acknowledgment  ",
        +      "shall be        "
        +    ]
        +解释: 注意最后一行的格式应为 "shall be    " 而不是 "shall     be"
        +    因为最后一行应为左对齐,而不是左右两端对齐,第二行同样为左对齐,这是因为这行只包含一个单词。
        +    
        + +

        示例 3:

        + +
        输入:
        +    words = ["Science","is","what","we","understand","well","enough","to","explain",
        +             "to","a","computer.","Art","is","everything","else","we","do"]
        +    maxWidth = 20
        +输出:
        +    [
        +      "Science  is  what we",
        +      "understand      well",
        +      "enough to explain to",
        +      "a  computer.  Art is",
        +      "everything  else  we",
        +      "do                  "
        +    ]
        +    
        +
        + +## template + +```cpp +#include +#include +#include +static void line_fill(char *line, int len, char **words, int *word_lens, int max_size, + int even_spaces, int remain_spaces, int start, int end) +{ + int i, j; + char *p = line; + for (i = start; i < end; i++) + { + memcpy(p, words[i], word_lens[i]); + p += word_lens[i]; + if (i < end - 1) + { + for (j = 0; j < even_spaces; j++) + { + *p++ = ' '; + } + if (remain_spaces > 0) + { + *p++ = ' '; + remain_spaces--; + } + } + } + while (p - line < max_size) + { + *p++ = ' '; + } + *p++ = '\0'; +} +static char **fullJustify(char **words, int wordsSize, int maxWidth, int *returnSize) +{ + int i, j, k, cap = 100, count = 0; + char **lines = malloc(cap * sizeof(char *)); + char *buf = malloc(cap * (maxWidth + 1)); + for (i = 0; i < cap; i++) + { + lines[i] = buf + i * (maxWidth + 1); + } + int *word_lens = malloc(wordsSize * sizeof(int)); + for (i = 0; i < wordsSize; i++) + { + word_lens[i] = strlen(words[i]); + } + int wc = 0; + int len = 0; + int start = 0; + int chars = 0; + for (i = 0, j = 0; i < wordsSize; i++) + { + if (len + word_lens[i] > maxWidth) + { + int even_spaces = wc == 1 ? 0 : (maxWidth - chars) / (wc - 1); + int remain_spaces = wc == 1 ? 0 : (maxWidth - chars) % (wc - 1); + line_fill(lines[count], len, words, word_lens, maxWidth, even_spaces, remain_spaces, start, i); + count++; + wc = 1; + len = word_lens[i] + 1; + chars = word_lens[i]; + start = i; + } + else if (len + word_lens[i] == maxWidth) + { + chars += word_lens[i]; + int even_spaces = wc == 0 ? 0 : (maxWidth - chars) / wc; + int remain_spaces = wc == 0 ? 0 : (maxWidth - chars) % wc; + line_fill(lines[count], len, words, word_lens, maxWidth, even_spaces, remain_spaces, start, i + 1); + count++; + wc = 0; + len = 0; + chars = 0; + start = i + 1; + } + else + { + chars += word_lens[i]; + len += word_lens[i] + 1; + wc++; + } + } + if (wc > 0) + { + char *p = lines[count]; + for (i = start; i < start + wc; i++) + { + memcpy(p, words[i], word_lens[i]); + p += word_lens[i]; + if (i < start + wc - 1) + { + *p++ = ' '; + } + } + while (p - lines[count] < maxWidth) + { + *p++ = ' '; + } + *p++ = '\0'; + count++; + } + *returnSize = count; + return lines; +} +int main(int argc, char **argv) +{ + if (argc <= 2) + { + fprintf(stderr, "Usage: ./test maxsize words...\n"); + exit(-1); + } + int i, count; + char **lines = fullJustify(argv + 2, argc - 2, atoi(argv[1]), &count); + for (i = 0; i < count; i++) + { + printf("%s\n", lines[i]); + } + return 0; +} +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/29.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/29.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..152769809602147cad9d2b1bfb5aed409e5bb334 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/29.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-eb4b978a641e4c63b0409c1a4b237653", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/29.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/29.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..fa8803ea85e3a05b8d0011e4a1b1d0cd63979934 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/29.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "7ad282774998489e8a09855c971a0152" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/29.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/29.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..a980273e7c88c2e1ac71461a7037fd8f4a2a9cad --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/29.exercises/solution.md" @@ -0,0 +1,73 @@ +# 最长有效括号 + +

        给你一个只包含 '(' 和 ')' 的字符串,找出最长有效(格式正确且连续)括号子串的长度。

         

        示例 1:

        输入:s = "(()"
        输出:
        2
        解释:
        最长有效括号子串是 "()"

        示例 2:

        输入:s = ")()())"
        输出:
        4
        解释:
        最长有效括号子串是 "()()"

        示例 3:

        输入:s = ""
        输出:
        0

         

        提示:

        • 0 <= s.length <= 3 * 104
        • s[i]'('')'
        + +## template + +```cpp +#include +using namespace std; +class Solution +{ +public: + int longestValidParentheses(string s) + { + stack stk; + int invalid = -1; + int len = 0, max_len = 0; + for (int i = 0; i < s.length(); i++) + { + if (s[i] == '(') + { + stk.push(i); + } + else + { + if (stk.empty()) + { + invalid = i; + } + else + { + stk.pop(); + if (stk.empty()) + { + max_len = max(i - invalid, max_len); + } + else + { + max_len = max(i - stk.top(), max_len); + } + } + } + } + return max_len; + } +}; +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/30.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/30.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..aa8f123bdeab783107b7bd14fa24821e851ef247 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/30.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-796d1cdaf31242d1a64eb862d645333e", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/30.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/30.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..6afe27cdf562525bb5d3bf4f8e84ea24760571b7 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/30.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "d412bf226b0241b3bee635de384fd357" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/30.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/30.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..aae16c1e34a806e0b3a74addb30f8ce5fd72e05c --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/30.exercises/solution.md" @@ -0,0 +1,80 @@ +# N 皇后 + +

        n 皇后问题 研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击。

        给你一个整数 n ,返回所有不同的 n 皇后问题 的解决方案。

        每一种解法包含一个不同的 n 皇后问题 的棋子放置方案,该方案中 'Q''.' 分别代表了皇后和空位。

         

        示例 1:

        输入:n = 4
        输出:
        [[".Q..","...Q","Q...","..Q."],["..Q.","Q...","...Q",".Q.."]]
        解释:
        如上图所示,4 皇后问题存在两个不同的解法。

        示例 2:

        输入:n = 1
        输出:
        [["Q"]]

         

        提示:

        • 1 <= n <= 9
        • 皇后彼此不能相互攻击,也就是说:任何两个皇后都不能处于同一条横行、纵行或斜线上。
        + +## template + +```cpp +#include +using namespace std; +class Solution +{ +public: + vector> solveNQueens(int n) + { + vector> res; + vector stack(n); + vector solution(n, string(n, '.')); + dfs(n, 0, stack, solution, res); + return res; + } +private: + void dfs(int n, int row, vector &stack, vector &solution, vector> &res) + { + if (row == n) + { + res.push_back(solution); + } + else + { + for (int i = 0; i < n; i++) + { + if (row == 0 || !conflict(stack, row, i)) + { + solution[row][i] = 'Q'; + stack[row] = i; + dfs(n, row + 1, stack, solution, res); + solution[row][i] = '.'; + } + } + } + } + bool conflict(vector &stack, int row, int col) + { + for (int i = 0; i < row; i++) + { + if (col == stack[i] || abs(row - i) == abs(col - stack[i])) + { + return true; + } + } + return false; + } +} +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/31.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/31.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..1d8a814f5a09a89acfe7ad992858e5a05f8615ac --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/31.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-7e24d340fbd64c748ae24961e24da7c9", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/31.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/31.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..58d7c1cb6cbc540c7e40b7ab0c3d36fea398b32c --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/31.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "29743c9fc7474dbf924a83b07c16eb20" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/31.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/31.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..f304bde011575c35bc7e84fe5065ceda969720ef --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/31.exercises/solution.md" @@ -0,0 +1,155 @@ +# 扰乱字符串 + +
        使用下面描述的算法可以扰乱字符串 s 得到字符串 t : +
          +
        1. 如果字符串的长度为 1 ,算法停止
        2. +
        3. 如果字符串的长度 > 1 ,执行下述步骤: +
            +
          • 在一个随机下标处将字符串分割成两个非空的子字符串。即,如果已知字符串 s ,则可以将其分成两个子字符串 xy + ,且满足 s = x + y
          • +
          • 随机 决定是要「交换两个子字符串」还是要「保持这两个子字符串的顺序不变」。即,在执行这一步骤之后,s 可能是 + s = x + y 或者 s = y + x 。 +
          • +
          • xy 这两个子字符串上继续从步骤 1 开始递归执行此算法。
          • +
          +
        4. +
        + +

        给你两个 长度相等 的字符串 s1 + 和 s2,判断 s2 是否是 s1 的扰乱字符串。如果是,返回 + true ;否则,返回 false 。 +

        + +

         

        + +

        示例 1:

        + +
        输入:s1 = "great", s2 = "rgeat"
        +输出:true
        +解释:s1 上可能发生的一种情形是:
        +"great" --> "gr/eat" // 在一个随机下标处分割得到两个子字符串
        +"gr/eat" --> "gr/eat" // 随机决定:「保持这两个子字符串的顺序不变」
        +"gr/eat" --> "g/r / e/at" // 在子字符串上递归执行此算法。两个子字符串分别在随机下标处进行一轮分割
        +"g/r / e/at" --> "r/g / e/at" // 随机决定:第一组「交换两个子字符串」,第二组「保持这两个子字符串的顺序不变」
        +"r/g / e/at" --> "r/g / e/ a/t" // 继续递归执行此算法,将 "at" 分割得到 "a/t"
        +"r/g / e/ a/t" --> "r/g / e/ a/t" // 随机决定:「保持这两个子字符串的顺序不变」
        +算法终止,结果字符串和 s2 相同,都是 "rgeat"
        +这是一种能够扰乱 s1 得到 s2 的情形,可以认为 s2 是 s1 的扰乱字符串,返回 true
        +
        + +

        示例 2:

        + +
        输入:s1 = "abcde", s2 = "caebd"
        +输出:false
        +
        + +

        示例 3:

        + +
        输入:s1 = "a", s2 = "a"
        +输出:true
        +
        + +

         

        + +

        提示:

        + +
          +
        • s1.length == s2.length
        • +
        • 1 <= s1.length <= 30
        • +
        • s1s2 由小写英文字母组成
        • +
        +
        + +## template + +```cpp +#include +#include +#include +#include +static bool scramble(char *s1, int low1, int high1, char *s2, int low2, int high2) +{ + if (high1 - low1 != high2 - low2) + { + return false; + } + else if (!memcmp(s1 + low1, s2 + low2, high1 - low1 + 1)) + { + return true; + } + else + { + int i, c1[128] = {0}, c2[128] = {0}; + for (i = low1; i <= high1; i++) + { + c1[s1[i]]++; + } + for (i = low2; i <= high2; i++) + { + c2[s2[i]]++; + } + if (memcmp(c1, c2, 128 * sizeof(int))) + { + return false; + } + else + { + int len = high1 - low1 + 1; + for (i = 1; i < len; i++) + { + if (scramble(s1, low1, low1 + i - 1, s2, low2, low2 + i - 1) && + scramble(s1, low1 + i, high1, s2, low2 + i, high2)) + { + return true; + } + if (scramble(s1, low1, low1 + i - 1, s2, high2 - i + 1, high2) && + scramble(s1, low1 + i, high1, s2, low2, high2 - i)) + { + return true; + } + } + return false; + } + } +} +static bool isScramble(char *s1, char *s2) +{ + return scramble(s1, 0, strlen(s1) - 1, s2, 0, strlen(s2) - 1); +} +int main(int argc, char **argv) +{ + if (argc != 3) + { + fprintf(stderr, "Usage: ./test s1 s2\n"); + exit(-1); + } + printf("%s\n", isScramble(argv[1], argv[2]) ? "true" : "false"); + return 0; +} +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/32.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/32.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..721b2e77f7db7599dcf1f3b684141bcfd5deea6b --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/32.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-7be27f0938eb4b04bc866fd60eb9d66d", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/32.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/32.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..8364e8042a64f851f530c978a14e9ef0fb2bdae5 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/32.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "5b843d4ffdea4c6daf1d8e998e95c236" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/32.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/32.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..38331007724978cda78a38fff6c20d94fd856c92 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/32.exercises/solution.md" @@ -0,0 +1,71 @@ +# K 个一组翻转链表 + +

        给你一个链表,每 个节点一组进行翻转,请你返回翻转后的链表。

        是一个正整数,它的值小于或等于链表的长度。

        如果节点总数不是 的整数倍,那么请将最后剩余的节点保持原有顺序。

        进阶:

        • 你可以设计一个只使用常数额外空间的算法来解决此问题吗?
        • 你不能只是单纯的改变节点内部的值,而是需要实际进行节点交换。

         

        示例 1:

        输入:head = [1,2,3,4,5], k = 2
        输出:
        [2,1,4,3,5]

        示例 2:

        输入:head = [1,2,3,4,5], k = 3
        输出:
        [3,2,1,4,5]

        示例 3:

        输入:head = [1,2,3,4,5], k = 1
        输出:
        [1,2,3,4,5]

        示例 4:

        输入:head = [1], k = 1
        输出:
        [1]

          提示:

          • 列表中节点的数量在范围 sz
          • 1 <= sz <= 5000
          • 0 <= Node.val <= 1000
          • 1 <= k <= sz
          + +## template + +```cpp +#include +using namespace std; +struct ListNode +{ + int val; + ListNode *next; + ListNode() : val(0), next(nullptr) {} + ListNode(int x) : val(x), next(nullptr) {} + ListNode(int x, ListNode *next) : val(x), next(next) {} +}; +class Solution +{ +public: + ListNode *reverseGroup(ListNode *head, int k) + { + int len = 0; + struct ListNode dummy, *prev = &dummy; + dummy.next = head; + for (; head != nullptr; head = head->next) + { + if (++len % k == 0) + { + struct ListNode *p = prev->next; + while (prev->next != head) + { + struct ListNode *q = p->next; + p->next = q->next; + q->next = prev->next; + prev->next = q; + } + prev = p; + head = p; + } + } + return dummy.next; + } +}; +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/33.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/33.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..c1efc890b1fd960f09bb123cf6050a0ad194cca5 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/33.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-7b797c91e9324852b4c71ed881c874ff", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/33.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/33.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..74c552eb86f65b7ee50d38d01e55f3b0b8090933 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/33.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "f9df5e9fa4c044efb8542ae24dd64471" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/33.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/33.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..c832568bd0f0bd42ef4918faec908c2b57508945 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/33.exercises/solution.md" @@ -0,0 +1,83 @@ +# 有效数字 + +

          有效数字(按顺序)可以分成以下几个部分:

          1. 一个 小数 或者 整数
          2. (可选)一个 'e''E' ,后面跟着一个 整数

          小数(按顺序)可以分成以下几个部分:

          1. (可选)一个符号字符('+''-'
          2. 下述格式之一:
            1. 至少一位数字,后面跟着一个点 '.'
            2. 至少一位数字,后面跟着一个点 '.' ,后面再跟着至少一位数字
            3. 一个点 '.' ,后面跟着至少一位数字

          整数(按顺序)可以分成以下几个部分:

          1. (可选)一个符号字符('+''-'
          2. 至少一位数字

          部分有效数字列举如下:

          • ["2", "0089", "-0.1", "+3.14", "4.", "-.9", "2e10", "-90E3", "3e+7", "+6e-1", "53.5e93", "-123.456e789"]

          部分无效数字列举如下:

          • ["abc", "1a", "1e", "e3", "99e2.5", "--6", "-+3", "95a54e53"]

          给你一个字符串 s ,如果 s 是一个 有效数字 ,请返回 true

           

          示例 1:

          输入:s = "0"
          输出:
          true

          示例 2:

          输入:s = "e"
          输出:
          false

          示例 3:

          输入:s = "."
          输出:
          false

          示例 4:

          输入:s = ".1"
          输出:
          true

           

          提示:

          • 1 <= s.length <= 20
          • s 仅含英文字母(大写和小写),数字(0-9),加号 '+' ,减号 '-' ,或者点 '.'
          + +## template + +```cpp +#include +#include +#include +#include +static bool isNumber(const char *s) +{ + while (*s == ' ') + ++s; + bool if_find_num = false; + if (*s == '-' || *s == '+') + ++s; + while (isdigit(*s)) + { + if_find_num = true; + ++s; + } + if (*s == '.') + ++s; + while (isdigit(*s)) + { + if_find_num = true; + ++s; + } + if (if_find_num == true && *s == 'e') + { + ++s; + if (*s == '+' || *s == '-') + ++s; + if_find_num = false; + while (isdigit(*s)) + { + if_find_num = true; + ++s; + } + } + while (*s == ' ') + ++s; + return *s == '\0' && if_find_num == true; +} +int main(int argc, char **argv) +{ + if (argc != 2) + { + fprintf(stderr, "Usage: ./test number\n"); + exit(-1); + } + printf("%s\n", isNumber(argv[1]) ? "true" : "false"); + return 0; +} +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/34.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/34.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..9d471531d7611ea7eeda46b52fbee0712f519835 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/34.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-509708a8cac74e3bb863ef1b1fe6033b", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/34.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/34.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..42bbc9ceed2be947aa0e110b2a6278a5e0001f7b --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/34.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "c38cad63cd904f86a00211f243b5ff34" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/34.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/34.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..9b56524bc49e5264a55717125dbb260bd9e3c639 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/34.exercises/solution.md" @@ -0,0 +1,78 @@ +# 串联所有单词的子串 + +

          给定一个字符串 和一些长度相同的单词 words。找出 s + 中恰好可以由 words 中所有单词串联形成的子串的起始位置。

          +

          注意子串要与 words 中的单词完全匹配,中间不能有其他字符,但不需要考虑 words 中单词串联的顺序。

          +

           

          +

          示例 1:

          +
          输入:  s = "barfoothefoobarman",  words = ["foo","bar"]
          输出:
          [0,9]
          解释:
          从索引 0 和 9 开始的子串分别是 "barfoo" 和 "foobar" 。输出的顺序不重要, [9,0] 也是有效答案。
          +

          示例 2:

          +
          输入:  s = "wordgoodgoodgoodbestword",  words = ["word","good","best","word"]
          输出:
          []
          + +## template + +```cpp +#include +using namespace std; +class Solution +{ +public: + vector findSubstring(string s, vector &words) + { + vector res; + if (s.empty() || words.empty()) + { + return res; + } + unordered_map ht; + for (const auto &w : words) + { + ht[w]++; + } + int len = words[0].length(); + for (int i = 0, j = 0; i < s.length() - words.size() * len + 1; i++) + { + unordered_map counting; + for (j = 0; j < words.size(); j++) + { + string word = s.substr(i + j * len, len); + if (++counting[word] > ht[word]) + { + break; + } + } + if (j == words.size()) + { + res.push_back(i); + } + } + return res; + } +}; +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/35.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/35.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..8c8cda814c92c70cda9a0fb7de04d304b38dfa37 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/35.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-7126805cf93544e2b7c71bbc93d3da4b", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/35.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/35.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..ef8e3830eebe17627d7c9654297fc5886ca0e8c4 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/35.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "a3af7e16d5434918be495a723ec5565d" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/35.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/35.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..4a39a47bda1d5cd59ef971434f9ef1657dc69e23 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/35.exercises/solution.md" @@ -0,0 +1,69 @@ +# 寻找两个正序数组的中位数 + +

          给定两个大小分别为 mn 的正序(从小到大)数组 nums1 和 nums2。请你找出并返回这两个正序数组的 中位数

           

          示例 1:

          输入:nums1 = [1,3], nums2 = [2]
          输出:
          2.00000
          解释:
          合并数组 = [1,2,3] ,中位数 2

          示例 2:

          输入:nums1 = [1,2], nums2 = [3,4]
          输出:
          2.50000
          解释:
          合并数组 = [1,2,3,4] ,中位数 (2 + 3) / 2 = 2.5

          示例 3:

          输入:nums1 = [0,0], nums2 = [0,0]
          输出:
          0.00000

          示例 4:

          输入:nums1 = [], nums2 = [1]
          输出:
          1.00000

          示例 5:

          输入:nums1 = [2], nums2 = []
          输出:
          2.00000

           

          提示:

          • nums1.length == m
          • nums2.length == n
          • 0 <= m <= 1000
          • 0 <= n <= 1000
          • 1 <= m + n <= 2000
          • -106 <= nums1[i], nums2[i] <= 106

           

          进阶:你能设计一个时间复杂度为 O(log (m+n)) 的算法解决此问题吗?

          + +## template + +```cpp +class Solution +{ +public: + double findMedianSortedArrays(vector &nums1, vector &nums2) + { + int nums1Size = nums1.size(); + int nums2Size = nums2.size(); + int na = nums1Size + nums2Size; + int *ns = (int *)malloc(4 * na); + int i = 0, j = 0, d = 0; + int m = na / 2 + 1; + while (d < m) + { + int n; + if (i < nums1Size && j < nums2Size) + { + n = (nums1[i] < nums2[j]) ? nums1[i++] : nums2[j++]; + } + else if (i < nums1Size) + { + n = nums1[i++]; + } + else if (j < nums2Size) + { + n = nums2[j++]; + } + ns[d++] = n; + } + if (na % 2) + { + return ns[d - 1]; + } + return (ns[d - 1] + ns[d - 2]) / 2.0; + } +}; +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/36.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/36.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..cb80ec56bce65f189d7fb6b0dfacda14840c9e88 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/36.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-de91f80714d04e79a9f867bf36a39979", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/36.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/36.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..62b2a44ac6777c48cbdad1aa299fe2020bbc451e --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/36.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "5fcb7dde2e3c49cb9214eadc195df6e6" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/36.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/36.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..ac79f74a619bf30451f08cb05826f257b79efbbd --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/36.exercises/solution.md" @@ -0,0 +1,71 @@ +# 最小覆盖子串 + +

          给你一个字符串 s 、一个字符串 t 。返回 s 中涵盖 t 所有字符的最小子串。如果 s 中不存在涵盖 t 所有字符的子串,则返回空字符串 ""

          注意:如果 s 中存在这样的子串,我们保证它是唯一的答案。

           

          示例 1:

          输入:s = "ADOBECODEBANC", t = "ABC"
          输出:
          "BANC"

          示例 2:

          输入:s = "a", t = "a"
          输出:
          "a"

           

          提示:

          • 1 <= s.length, t.length <= 105
          • st 由英文字母组成

           

          进阶:你能设计一个在 o(n) 时间内解决此问题的算法吗? + +## template + +```cpp +#include +using namespace std; +class Solution +{ +public: + string minWindow(string s, string t) + { + vector count(128); + for (char c : t) + { + count[c]++; + } + int l = 0, r = 0; + int need_to_meet = t.length(); + int start, min_len = INT_MAX; + while (r < s.length()) + { + if (--count[s[r++]] >= 0) + { + need_to_meet--; + } + while (need_to_meet == 0) + { + if (r - l < min_len) + { + start = l; + min_len = r - l; + } + if (++count[s[l++]] > 0) + { + need_to_meet++; + } + } + } + return min_len == INT_MAX ? "" : s.substr(start, min_len); + } +}; +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/37.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/37.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..71eff7d19ce3c34dbb89fd4dbb140055d3adbb1d --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/37.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-7f8f6810f2f74911aad1aa7fd95a1864", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/37.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/37.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..9919476cbb97f4c088280f55e07981a8a7b0642a --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/37.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "4632c2b673d644829ed8b1c8976cd2c7" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/37.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/37.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..5ea8efca98a072a477bf1cc7aa3e0412cf02ece9 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/37.exercises/solution.md" @@ -0,0 +1,77 @@ +# 合并K个升序链表 + +

          给你一个链表数组,每个链表都已经按升序排列。

          请你将所有链表合并到一个升序链表中,返回合并后的链表。

           

          示例 1:

          输入:lists = [[1,4,5],[1,3,4],[2,6]]
          输出:
          [1,1,2,3,4,4,5,6]
          解释:
          链表数组如下:[ 1->4->5, 1->3->4, 2->6]将它们合并到一个有序链表中得到。1->1->2->3->4->4->5->6

          示例 2:

          输入:lists = []
          输出:
          []

          示例 3:

          输入:lists = [[]]
          输出:
          []

           

          提示:

          • k == lists.length
          • 0 <= k <= 10^4
          • 0 <= lists[i].length <= 500
          • -10^4 <= lists[i][j] <= 10^4
          • lists[i]升序 排列
          • lists[i].length 的总和不超过 10^4
          + +## template + +```cpp +#include +using namespace std; +struct ListNode +{ + int val; + ListNode *next; + ListNode() : val(0), next(nullptr) {} + ListNode(int x) : val(x), next(nullptr) {} + ListNode(int x, ListNode *next) : val(x), next(next) {} +}; +class Solution +{ +public: + ListNode *mergeKLists(vector &lists) + { + auto cmp = [](struct ListNode *n1, struct ListNode *n2) + { + return n1->val > n2->val; + } priority_queue, decltype(cmp)> + queue(cmp); + for (int i = 0; i < lists.size(); i++) + { + if (lists[i] != nullptr) + { + queue.push(lists[i]); + } + } + struct ListNode dummy, *p = &dummy; + ; + while (!queue.empty()) + { + ListNode *node = queue.top(); + queue.pop(); + p->next = node; + p = node; + if (node->next != nullptr) + { + queue.push(node->next); + } + } + return dummy.next; + } +}; +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/38.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/38.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..e486a78f63e9a2eae77107d2ab292c4a9934b7aa --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/38.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-cb6c63cc770c4312a022e1e729a4b8f5", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/38.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/38.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..fea0e2425850b3a119dca1db78be202b93124146 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/38.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "14d954836c6d401499882ffc11cd6d22" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/38.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/38.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..1f6e0d5c5fff963606a35f7084e192b90fd9e6f4 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/38.exercises/solution.md" @@ -0,0 +1,81 @@ +# 排列序列 + +

          给出集合 [1,2,3,...,n],其所有元素共有 n! 种排列。

          按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下:

          1. "123"
          2. "132"
          3. "213"
          4. "231"
          5. "312"
          6. "321"

          给定 n 和 k,返回第 k 个排列。

           

          示例 1:

          输入:n = 3, k = 3
          输出:
          "213"

          示例 2:

          输入:n = 4, k = 9
          输出:
          "2314"

          示例 3:

          输入:n = 3, k = 1
          输出:
          "123"

           

          提示:

          • 1 <= n <= 9
          • 1 <= k <= n!
          + +## template + +```cpp +#include +#include +#include +#include +static char *getPermutation(int n, int k) +{ + int i; + char *result = malloc(n + 1); + bool *used = malloc(n * sizeof(bool)); + memset(used, false, n * sizeof(bool)); + int total = 1; + for (i = 1; i <= n; i++) + { + total *= i; + } + k = k - 1; + for (i = 0; i < n; i++) + { + total /= (n - i); + int gid = k / total; + k %= total; + int x = -1; + int count = 0; + while (count <= gid) + { + x = (x + 1) % n; + if (!used[x]) + { + count++; + } + } + used[x] = true; + result[i] = x + 1 + '0'; + } + result[n] = '\0'; + return result; +} +int main(int argc, char **argv) +{ + if (argc != 3) + { + fprintf(stderr, "Usage: ./test n, k\n"); + exit(-1); + } + printf("%s\n", getPermutation(atoi(argv[1]), atoi(argv[2]))); + return 0; +} +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/39.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/39.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..0aa3e64fc8d2809af461f3ed9b04af10820da440 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/39.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-a48e149da00141d382fd9b414e6d6b4a", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/39.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/39.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..d9ec749556f1fee3af1fa91020448be6339782a0 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/39.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/begeekmyfriend/leetcode", + "source": "solution.md", + "exercise_id": "9e0eb3f30b01436f99fd027d15b49b37" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/39.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/39.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..947907ffd6670b4ecc91fa370769b8fb472702d9 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/39.exercises/solution.md" @@ -0,0 +1,84 @@ +# 通配符匹配 + +

          给定一个字符串 (s) 和一个字符模式 (p) ,实现一个支持 '?' 和 '*' 的通配符匹配。

          '?' 可以匹配任何单个字符。'*' 可以匹配任意字符串(包括空字符串)。

          两个字符串完全匹配才算匹配成功。

          说明:

          • s 可能为空,且只包含从 a-z 的小写字母。
          • p 可能为空,且只包含从 a-z 的小写字母,以及字符 ? 和 *

          示例 1:

          输入:s = "aa"p = "a"
          输出:
          false
          解释:
          "a" 无法匹配 "aa" 整个字符串。

          示例 2:

          输入:s = "aa"p = "*"
          输出:
          true
          解释:
           '*' 可以匹配任意字符串。

          示例 3:

          输入:s = "cb"p = "?a"
          输出:
          false
          解释:
           '?' 可以匹配 'c', 但第二个 'a' 无法匹配 'b'。

          示例 4:

          输入:s = "adceb"p = "*a*b"
          输出:
          true
          解释:
           第一个 '*' 可以匹配空字符串, 第二个 '*' 可以匹配字符串 "dce".

          示例 5:

          输入:s = "acdcb"p = "a*c?b"
          输出:
          false
          + +## template + +```cpp +#include +#include +#include +#include +static bool isMatch(char *s, char *p) +{ + char *last_s = NULL; + char *last_p = NULL; + while (*s != '\0') + { + if (*p == '*') + { + if (*++p == '\0') + { + return true; + } + last_s = s; + last_p = p; + } + else if (*p == '?' || *s == *p) + { + s++; + p++; + } + else if (last_s != NULL) + { + p = last_p; + s = ++last_s; + } + else + { + return false; + } + } + while (*p == '*') + { + p++; + } + return *p == '\0'; +} +int main(int argc, char **argv) +{ + if (argc != 3) + { + fprintf(stderr, "Usage: ./test string pattern\n"); + exit(-1); + } + printf("%s\n", isMatch(argv[1], argv[2]) ? "true" : "false"); + return 0; +} +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/40.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/40.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..8194efafcd38eabf7390718953a4f5110440ab6d --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/40.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-9057ff55a0e44859a1c875ed9c0f3309", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/40.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/40.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..2fb57c4061cd4ff202610cbc29fc59745948cada --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/40.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "1340e2fd0a1c45adb3222da4a507e472" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/40.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/40.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..b10bdcf165cd215f9d4fab32e935250b94fea6f2 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/40.exercises/solution.md" @@ -0,0 +1,48 @@ +# 正则表达式匹配 + +

          给你一个字符串 s 和一个字符规律 p,请你来实现一个支持 '.' 和 '*' 的正则表达式匹配。

          • '.' 匹配任意单个字符
          • '*' 匹配零个或多个前面的那一个元素

          所谓匹配,是要涵盖 整个 字符串 s的,而不是部分字符串。

          示例 1:

          输入:s = "aa" p = "a"
          输出:
          false
          解释:
          "a" 无法匹配 "aa" 整个字符串。

          示例 2:

          输入:s = "aa" p = "a*"
          输出:
          true
          解释:
          因为 '*' 代表可以匹配零个或多个前面的那一个元素, 在这里前面的元素就是 'a'。因此,字符串 "aa" 可被视为 'a' 重复了一次。

          示例 3:

          输入:s = "ab" p = ".*"
          输出:
          true
          解释:
          ".*" 表示可匹配零个或多个('*')任意字符('.')。

          示例 4:

          输入:s = "aab" p = "c*a*b"
          输出:
          true
          解释:
          因为 '*' 表示零个或多个,这里 'c' 为 0 个, 'a' 被重复一次。因此可以匹配字符串 "aab"。

          示例 5:

          输入:s = "mississippi" p = "mis*is*p*."
          输出:
          false

           

          提示:

          • 0 <= s.length <= 20
          • 0 <= p.length <= 30
          • s 可能为空,且只包含从 a-z 的小写字母。
          • p 可能为空,且只包含从 a-z 的小写字母,以及字符 . 和 *
          • 保证每次出现字符 * 时,前面都匹配到有效的字符
          + +## template + +```cpp +bool isMatch(char *s, char *p) +{ + if (!p || p[0] == NULL) + return (!s || s[0] == NULL); + bool head_match = (s && s[0] && (s[0] == p[0] || p[0] == '.')); + if (p[1] && p[1] == '*') + { + return (head_match && isMatch(s + 1, p)) || isMatch(s, p + 2); + } + else + { + return head_match && isMatch(s + 1, p + 1); + } +} +``` + +## 答案 + +```cpp + +``` + +## 选项 + +### A + +```cpp + +``` + +### B + +```cpp + +``` + +### C + +```cpp + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/1.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/1.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..199de1cb8712ed8405f883f46cade9f08e187417 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/1.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-200296fc62754d9686cf2bfb1e7064cb", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/1.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/1.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..081a4eff08260667b9f24e35a8e0fcc53ec8e252 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/1.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "c14af05254cc428ca81db2e4da335187" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/1.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/2.java/1.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..a28cda88167e0b9f32fad11c0418676b25176ef4 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/1.exercises/solution.md" @@ -0,0 +1,140 @@ +# 解数独 + +

          编写一个程序,通过填充空格来解决数独问题。

          +

          数独的解法需 遵循如下规则

          +
            +
          1. 数字 1-9 在每一行只能出现一次。
          2. +
          3. 数字 1-9 在每一列只能出现一次。
          4. +
          5. 数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。(请参考示例图)
          6. +
          +

          数独部分空格内已填入了数字,空白格用 '.' 表示。

          +

           

          +
          +
          +
          +

          示例:

          +
          输入:board = 
          +    [["5","3",".",".","7",".",".",".","."],
          +    ["6",".",".","1","9","5",".",".","."],
          +    [".","9","8",".",".",".",".","6","."],
          +    ["8",".",".",".","6",".",".",".","3"],
          +    ["4",".",".","8",".","3",".",".","1"],
          +    ["7",".",".",".","2",".",".",".","6"],
          +    [".","6",".",".",".",".","2","8","."],
          +    [".",".",".","4","1","9",".",".","5"],
          +    [".",".",".",".","8",".",".","7","9"]]
          +输出:
          +    [["5","3","4","6","7","8","9","1","2"],
          +    ["6","7","2","1","9","5","3","4","8"],
          +    ["1","9","8","3","4","2","5","6","7"],
          +    ["8","5","9","7","6","1","4","2","3"],
          +    ["4","2","6","8","5","3","7","9","1"],
          +    ["7","1","3","9","2","4","8","5","6"],
          +    ["9","6","1","5","3","7","2","8","4"],
          +    ["2","8","7","4","1","9","6","3","5"],
          +    ["3","4","5","2","8","6","1","7","9"]]
          +解释:输入的数独如上图所示,唯一有效的解决方案如下所示:
          +

           

          +
          + + +

           

          +

          提示:

          +
            +
          • board.length == 9
          • +
          • board[i].length == 9
          • +
          • board[i][j] 是一位数字或者 '.'
          • +
          • 题目数据 保证 输入数独仅有一个解
          • +
          +
          +
          +
          + +## template + +```java +class Solution { + + boolean row[][] = new boolean[9][9]; + boolean col[][] = new boolean[9][9]; + + boolean cell[][][] = new boolean[3][3][9]; + + public void solveSudoku(char[][] board) { + + for (int i = 0; i < 9; i++) { + for (int j = 0; j < 9; j++) { + if (board[i][j] != '.') { + + int t = board[i][j] - '1'; + + row[i][t] = col[j][t] = cell[i / 3][j / 3][t] = true; + } + } + } + + dfs(board, 0, 0); + } + + public boolean dfs(char[][] board, int x, int y) { + + if (y == 9) { + + x++; + y = 0; + } + + if (x == 9) + return true; + + if (board[x][y] != '.') + return dfs(board, x, y + 1); + + for (int num = 0; num < 9; num++) { + + if (!row[x][num] && !col[y][num] && !cell[x / 3][y / 3][num]) { + + board[x][y] = (char) (num + '1'); + row[x][num] = col[y][num] = cell[x / 3][y / 3][num] = true; + + if (dfs(board, x, y + 1)) + return true; + + board[x][y] = '.'; + row[x][num] = col[y][num] = cell[x / 3][y / 3][num] = false; + } + } + + return false; + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/10.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/10.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..69a117b2b9b4f01b6aaad90df35b19b2e3ae3b6b --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/10.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-ba79701447d749a69620404728163aa9", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/10.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/10.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..6facd9f7bc46d18fad9ad38509fae0c2dd825277 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/10.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "a6290ce6b7df4665bcf7614432e4f648" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/10.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/2.java/10.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..ba7c713f6537f80b3e84e2e6476b43423dd02643 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/10.exercises/solution.md" @@ -0,0 +1,82 @@ +# N 皇后 + +

          n 皇后问题 研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击。

          给你一个整数 n ,返回所有不同的 n 皇后问题 的解决方案。

          每一种解法包含一个不同的 n 皇后问题 的棋子放置方案,该方案中 'Q''.' 分别代表了皇后和空位。

           

          示例 1:

          输入:n = 4
          输出:
          [[".Q..","...Q","Q...","..Q."],["..Q.","Q...","...Q",".Q.."]]
          解释:
          如上图所示,4 皇后问题存在两个不同的解法。

          示例 2:

          输入:n = 1
          输出:
          [["Q"]]

           

          提示:

          • 1 <= n <= 9
          • 皇后彼此不能相互攻击,也就是说:任何两个皇后都不能处于同一条横行、纵行或斜线上。
          + +## template + +```java +import java.util.List; +import java.util.ArrayList; +public class Solution { + public List> solveNQueens(int n) { + List> res = new ArrayList>(); + int[] queenList = new int[n]; + placeQueen(queenList, 0, n, res); + return res; + } + private void placeQueen(int[] queenList, int row, int n, List> res) { + if (row == n) { + ArrayList list = new ArrayList(); + for (int i = 0; i < n; i++) { + String str = ""; + for (int col = 0; col < n; col++) { + if (queenList[i] == col) { + str += "Q"; + } else { + str += "."; + } + } + list.add(str); + } + res.add(list); + } + for (int col = 0; col < n; col++) { + if (isValid(queenList, row, col)) { + queenList[row] = col; + placeQueen(queenList, row + 1, n, res); + } + } + } + private boolean isValid(int[] queenList, int row, int col) { + for (int i = 0; i < row; i++) { + int pos = queenList[i]; + if (pos == col) { + return false; + } + if (pos + row - i == col) { + return false; + } + if (pos - row + i == col) { + return false; + } + } + return true; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/11.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/11.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..c30e55f93e81c3ae007669bbec054caab53ed448 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/11.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-0056895bd0864ea2b0ff55bc782488e2", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/11.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/11.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..52ba47a20eca25e21b071fd34d817f6c7792ddfa --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/11.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "47546046b02346d2a901719b2264a703" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/11.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/2.java/11.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..4a5f27cdfbd4e9bbd2b7479120236c406db3127f --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/11.exercises/solution.md" @@ -0,0 +1,128 @@ +# 扰乱字符串 + +
          使用下面描述的算法可以扰乱字符串 s 得到字符串 t : +
            +
          1. 如果字符串的长度为 1 ,算法停止
          2. +
          3. 如果字符串的长度 > 1 ,执行下述步骤: +
              +
            • 在一个随机下标处将字符串分割成两个非空的子字符串。即,如果已知字符串 s ,则可以将其分成两个子字符串 xy + ,且满足 s = x + y
            • +
            • 随机 决定是要「交换两个子字符串」还是要「保持这两个子字符串的顺序不变」。即,在执行这一步骤之后,s 可能是 + s = x + y 或者 s = y + x 。 +
            • +
            • xy 这两个子字符串上继续从步骤 1 开始递归执行此算法。
            • +
            +
          4. +
          + +

          给你两个 长度相等 的字符串 s1 + 和 s2,判断 s2 是否是 s1 的扰乱字符串。如果是,返回 + true ;否则,返回 false 。 +

          + +

           

          + +

          示例 1:

          + +
          输入:s1 = "great", s2 = "rgeat"
          +输出:true
          +解释:s1 上可能发生的一种情形是:
          +"great" --> "gr/eat" // 在一个随机下标处分割得到两个子字符串
          +"gr/eat" --> "gr/eat" // 随机决定:「保持这两个子字符串的顺序不变」
          +"gr/eat" --> "g/r / e/at" // 在子字符串上递归执行此算法。两个子字符串分别在随机下标处进行一轮分割
          +"g/r / e/at" --> "r/g / e/at" // 随机决定:第一组「交换两个子字符串」,第二组「保持这两个子字符串的顺序不变」
          +"r/g / e/at" --> "r/g / e/ a/t" // 继续递归执行此算法,将 "at" 分割得到 "a/t"
          +"r/g / e/ a/t" --> "r/g / e/ a/t" // 随机决定:「保持这两个子字符串的顺序不变」
          +算法终止,结果字符串和 s2 相同,都是 "rgeat"
          +这是一种能够扰乱 s1 得到 s2 的情形,可以认为 s2 是 s1 的扰乱字符串,返回 true
          +
          + +

          示例 2:

          + +
          输入:s1 = "abcde", s2 = "caebd"
          +输出:false
          +
          + +

          示例 3:

          + +
          输入:s1 = "a", s2 = "a"
          +输出:true
          +
          + +

           

          + +

          提示:

          + +
            +
          • s1.length == s2.length
          • +
          • 1 <= s1.length <= 30
          • +
          • s1s2 由小写英文字母组成
          • +
          +
          + +## template + +```java +class Solution { + public boolean isScramble(String s1, String s2) { + if (s1.length() == 0 && s2.length() == 0) + return true; + if (s1.length() != s2.length()) + return false; + return dfs(s1.toCharArray(), s2.toCharArray(), 0, 0, s1.length()); + } + private boolean dfs(char[] s1, char[] s2, int start1, int start2, int len) { + if (len == 1) { + return s1[start1] == s2[start2]; + } + if (!equals(s1, s2, start1, start2, len)) { + return false; + } + for (int i = 1; i < len; i++) { + if (dfs(s1, s2, start1, start2, i) && dfs(s1, s2, start1 + i, start2 + i, len - i)) + return true; + if (dfs(s1, s2, start1, start2 + len - i, i) && dfs(s1, s2, start1 + i, start2, len - i)) + return true; + } + return false; + } + public boolean equals(char[] s1, char[] s2, int start1, int start2, int len) { + int[] charArr = new int[26]; + for (int i = 0; i < len; i++) { + charArr[s1[start1 + i] - 'a']++; + charArr[s2[start2 + i] - 'a']--; + } + for (int item : charArr) { + if (item != 0) + return false; + } + return true; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/12.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/12.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..82e98cf47f523204ac0f63dfe32198c6c2fae311 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/12.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-3b110dd840324f6b89f7d65b3bff6987", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/12.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/12.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..40b4f2a77f02c74146215575271ff794b1f15726 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/12.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "b2a7a3b063ec45f2ac9145a89cbaf735" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/12.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/2.java/12.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..298d54b3fa091e149c7c75240222b138c42fa012 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/12.exercises/solution.md" @@ -0,0 +1,86 @@ +# K 个一组翻转链表 + +

          给你一个链表,每 个节点一组进行翻转,请你返回翻转后的链表。

          是一个正整数,它的值小于或等于链表的长度。

          如果节点总数不是 的整数倍,那么请将最后剩余的节点保持原有顺序。

          进阶:

          • 你可以设计一个只使用常数额外空间的算法来解决此问题吗?
          • 你不能只是单纯的改变节点内部的值,而是需要实际进行节点交换。

           

          示例 1:

          输入:head = [1,2,3,4,5], k = 2
          输出:
          [2,1,4,3,5]

          示例 2:

          输入:head = [1,2,3,4,5], k = 3
          输出:
          [3,2,1,4,5]

          示例 3:

          输入:head = [1,2,3,4,5], k = 1
          输出:
          [1,2,3,4,5]

          示例 4:

          输入:head = [1], k = 1
          输出:
          [1]

            提示:

            • 列表中节点的数量在范围 sz
            • 1 <= sz <= 5000
            • 0 <= Node.val <= 1000
            • 1 <= k <= sz
            + +## template + +```java + +public class ListNode { + int val; + ListNode next; + + ListNode() { + } + + ListNode(int val) { + this.val = val; + } + + ListNode(int val, ListNode next) { + this.val = val; + this.next = next; + } +} + +class Solution { + public ListNode reverseKGroup(ListNode head, int k) { + if (head == null) { + return null; + } + ListNode a = head, b = head; + + for (int i = 0; i < k; i++) { + if (b == null) { + return a; + } + b = b.next; + } + + ListNode newHead = reverse(a, b); + + a.next = reverseKGroup(b, k); + return newHead; + } + + public ListNode reverse(ListNode a, ListNode b) { + ListNode pre, cur, nxt; + pre = null; + cur = a; + nxt = a; + while (nxt != b) { + nxt = cur.next; + cur.next = pre; + pre = cur; + cur = nxt; + } + return pre; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/13.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/13.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..3ead09c22990f265ba5dc7218eae68fbc49a4b2b --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/13.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-83b55abc178c4be99002812d523f8006", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/13.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/13.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..4e2a664db51c284a5f2b12470a7c4e7f7dc1075a --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/13.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "3bb60106b8ef43fb90916581ab522e82" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/13.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/2.java/13.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..e1b3cf519dc6bc122283bb38e4f096deb78620e7 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/13.exercises/solution.md" @@ -0,0 +1,118 @@ +# 有效数字 + +

            有效数字(按顺序)可以分成以下几个部分:

            1. 一个 小数 或者 整数
            2. (可选)一个 'e''E' ,后面跟着一个 整数

            小数(按顺序)可以分成以下几个部分:

            1. (可选)一个符号字符('+''-'
            2. 下述格式之一:
              1. 至少一位数字,后面跟着一个点 '.'
              2. 至少一位数字,后面跟着一个点 '.' ,后面再跟着至少一位数字
              3. 一个点 '.' ,后面跟着至少一位数字

            整数(按顺序)可以分成以下几个部分:

            1. (可选)一个符号字符('+''-'
            2. 至少一位数字

            部分有效数字列举如下:

            • ["2", "0089", "-0.1", "+3.14", "4.", "-.9", "2e10", "-90E3", "3e+7", "+6e-1", "53.5e93", "-123.456e789"]

            部分无效数字列举如下:

            • ["abc", "1a", "1e", "e3", "99e2.5", "--6", "-+3", "95a54e53"]

            给你一个字符串 s ,如果 s 是一个 有效数字 ,请返回 true

             

            示例 1:

            输入:s = "0"
            输出:
            true

            示例 2:

            输入:s = "e"
            输出:
            false

            示例 3:

            输入:s = "."
            输出:
            false

            示例 4:

            输入:s = ".1"
            输出:
            true

             

            提示:

            • 1 <= s.length <= 20
            • s 仅含英文字母(大写和小写),数字(0-9),加号 '+' ,减号 '-' ,或者点 '.'
            + +## template + +```java +class Solution { + char[] chars; + boolean point = false; + boolean exponent = false; + public boolean isNumber(String s) { + s = s.trim(); + int length = s.length(); + if (length == 0) { + return false; + } + chars = s.toCharArray(); + String[] ss = s.split("e"); + if (ss.length == 0) { + return false; + } + if (ss[0].length() == 0) + return false; + if (ss[0].length() < length) + exponent = true; + if (ss[0].length() == length - 1) { + return false; + } + String[] pre = ss[0].split("\\."); + if (pre.length == 0) { + return false; + } + if (pre[0].length() < ss[0].length()) + point = true; + boolean result = pre(0, pre[0].length()); + result = result && middle(pre[0].length() + 1, ss[0].length()); + if (exponent) { + result = result && is(ss[0].length() + 1, length); + } + return result; + } + public boolean pre(int i, int length) { + if (i >= length) { + return true; + } + if (chars[i] == '+' || chars[i] == '-') { + i++; + } + if (i == length && !point) { + return false; + } + for (; i < length; i++) { + if (chars[i] < '0' || chars[i] > '9') { + return false; + } + } + return true; + } + public boolean middle(int i, int length) { + if (i >= length && point) { + if (chars[i - 2] >= '0' && chars[i - 2] <= '9') { + return true; + } + return false; + } + for (; i < length; i++) { + if (chars[i] < '0' || chars[i] > '9') { + return false; + } + } + return true; + } + public boolean is(int i, int length) { + if (i == 1) { + return false; + } + if (chars[i] == '+' || chars[i] == '-') { + i++; + } + if (i == length) { + return false; + } + for (; i < length; i++) { + if (chars[i] < '0' || chars[i] > '9') { + return false; + } + } + return true; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/14.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/14.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..2a0a4c2f5aecab79e12857ff4a7fb0c608d9bb09 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/14.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-0ea41e8ce50247f1b24462a97030e8b6", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/14.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/14.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..ed8e8b47fa76759daa271b5f1d494041b274637a --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/14.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "de8bfdfe57c1478b844a53534a28fdb5" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/14.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/2.java/14.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..ec7486c3cc965f0cda8369ac2a66f62aeefdcc23 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/14.exercises/solution.md" @@ -0,0 +1,80 @@ +# 串联所有单词的子串 + +

            给定一个字符串 和一些长度相同的单词 words。找出 s + 中恰好可以由 words 中所有单词串联形成的子串的起始位置。

            +

            注意子串要与 words 中的单词完全匹配,中间不能有其他字符,但不需要考虑 words 中单词串联的顺序。

            +

             

            +

            示例 1:

            +
            输入:  s = "barfoothefoobarman",  words = ["foo","bar"]
            输出:
            [0,9]
            解释:
            从索引 0 和 9 开始的子串分别是 "barfoo" 和 "foobar" 。输出的顺序不重要, [9,0] 也是有效答案。
            +

            示例 2:

            +
            输入:  s = "wordgoodgoodgoodbestword",  words = ["word","good","best","word"]
            输出:
            []
            + +## template + +```java +class Solution { + public List findSubstring(String s, String[] words) { + List res = new ArrayList<>(); + if (s == null || s.length() == 0 || words == null || words.length == 0) + return res; + HashMap map = new HashMap<>(); + int one_word = words[0].length(); + int word_num = words.length; + int all_len = one_word * word_num; + for (String word : words) { + map.put(word, map.getOrDefault(word, 0) + 1); + } + for (int i = 0; i < one_word; i++) { + int left = i, right = i, count = 0; + HashMap tmp_map = new HashMap<>(); + while (right + one_word <= s.length()) { + String w = s.substring(right, right + one_word); + right += one_word; + if (!map.containsKey(w)) { + count = 0; + left = right; + tmp_map.clear(); + } else { + tmp_map.put(w, tmp_map.getOrDefault(w, 0) + 1); + count++; + while (tmp_map.getOrDefault(w, 0) > map.getOrDefault(w, 0)) { + String t_w = s.substring(left, left + one_word); + count--; + tmp_map.put(t_w, tmp_map.getOrDefault(t_w, 0) - 1); + left += one_word; + } + if (count == word_num) + res.add(left); + } + } + } + return res; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/15.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/15.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..ffb38faa5a3fb95d6a1bdb89574284d40a6da907 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/15.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-f3a84fdc09184ef1a37a126740fc3744", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/15.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/15.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..b787029f9a5719caa73f61fe80844dbfeaf95d7d --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/15.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "96919316b4e64e07a4307e3444699689" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/15.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/2.java/15.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..99233bb0b6f6e5c0cce3e88184e8173c3b092e20 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/15.exercises/solution.md" @@ -0,0 +1,59 @@ +# 寻找两个正序数组的中位数 + +

            给定两个大小分别为 mn 的正序(从小到大)数组 nums1 和 nums2。请你找出并返回这两个正序数组的 中位数

             

            示例 1:

            输入:nums1 = [1,3], nums2 = [2]
            输出:
            2.00000
            解释:
            合并数组 = [1,2,3] ,中位数 2

            示例 2:

            输入:nums1 = [1,2], nums2 = [3,4]
            输出:
            2.50000
            解释:
            合并数组 = [1,2,3,4] ,中位数 (2 + 3) / 2 = 2.5

            示例 3:

            输入:nums1 = [0,0], nums2 = [0,0]
            输出:
            0.00000

            示例 4:

            输入:nums1 = [], nums2 = [1]
            输出:
            1.00000

            示例 5:

            输入:nums1 = [2], nums2 = []
            输出:
            2.00000

             

            提示:

            • nums1.length == m
            • nums2.length == n
            • 0 <= m <= 1000
            • 0 <= n <= 1000
            • 1 <= m + n <= 2000
            • -106 <= nums1[i], nums2[i] <= 106

             

            进阶:你能设计一个时间复杂度为 O(log (m+n)) 的算法解决此问题吗?

            + +## template + +```java +class Solution { + public double findMedianSortedArrays(int[] nums1, int[] nums2) { + int nums1Size = nums1.length; + int nums2Size = nums2.length; + int na = nums1Size + nums2Size; + int[] ns = new int[4 * na]; + int i = 0, j = 0, d = 0; + int m = na / 2 + 1; + while (d < m) { + int n = 0; + if (i < nums1Size && j < nums2Size) { + n = (nums1[i] < nums2[j]) ? nums1[i++] : nums2[j++]; + } else if (i < nums1Size) { + n = nums1[i++]; + } else if (j < nums2Size) { + n = nums2[j++]; + } + ns[d++] = n; + } + if (na % 2 == 1) { + return ns[d - 1]; + } + return (ns[d - 1] + ns[d - 2]) / 2.0; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/16.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/16.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..998f2b2a38d8a37a9fa7251674217658a45e8661 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/16.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-348a8876daed4f449b2b660c94c9f6cd", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/16.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/16.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..3b486dab9eafc186e5169963a2370476483a5dcc --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/16.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "820c1f17a5be4ce2bf05446e7aa7764a" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/16.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/2.java/16.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..1a81f546b377f3ab559e4a6b89532f76a066452a --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/16.exercises/solution.md" @@ -0,0 +1,71 @@ +# 最小覆盖子串 + +

            给你一个字符串 s 、一个字符串 t 。返回 s 中涵盖 t 所有字符的最小子串。如果 s 中不存在涵盖 t 所有字符的子串,则返回空字符串 ""

            注意:如果 s 中存在这样的子串,我们保证它是唯一的答案。

             

            示例 1:

            输入:s = "ADOBECODEBANC", t = "ABC"
            输出:
            "BANC"

            示例 2:

            输入:s = "a", t = "a"
            输出:
            "a"

             

            提示:

            • 1 <= s.length, t.length <= 105
            • st 由英文字母组成

             

            进阶:你能设计一个在 o(n) 时间内解决此问题的算法吗? + +## template + +```java +public class Min_Win_Sub { + public String minWindow(String s, String t) { + int[] ta = new int[128]; + int[] sa = new int[128]; + int min = Integer.MAX_VALUE; + String minwin = ""; + for (int i = 0; i < t.length(); i++) { + ta[t.charAt(i)]++; + } + int count = 0; + int end = 0; + int start = 0; + while (end < s.length()) { + if (ta[s.charAt(end)] != 0) { + if (sa[s.charAt(end)] < ta[s.charAt(end)]) { + count++; + } + sa[s.charAt(end)]++; + } + if (count == t.length()) { + while (ta[s.charAt(start)] == 0 || sa[s.charAt(start)] > ta[s.charAt(start)]) { + if (sa[s.charAt(start)] > ta[s.charAt(start)]) { + sa[s.charAt(start)]--; + } + start++; + } + if (end - start + 1 < min) { + minwin = s.substring(start, end + 1); + min = end - start + 1; + } + } + end++; + } + return minwin; + } + +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/17.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/17.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..1c52dbee4300fbcfe8b5b0ec84a33aee1489e549 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/17.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-7fb82955b7854dad9e920ca9e1490e8f", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/17.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/17.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..e1e868ca3882678f217f0e823233e427dde8a75c --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/17.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "03a34a061bfc46239fd7b8ba4d2bc551" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/17.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/2.java/17.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..2204bc5952f50e25de6fd7c5b6ec476f6393391c --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/17.exercises/solution.md" @@ -0,0 +1,95 @@ +# 合并K个升序链表 + +

            给你一个链表数组,每个链表都已经按升序排列。

            请你将所有链表合并到一个升序链表中,返回合并后的链表。

             

            示例 1:

            输入:lists = [[1,4,5],[1,3,4],[2,6]]
            输出:
            [1,1,2,3,4,4,5,6]
            解释:
            链表数组如下:[ 1->4->5, 1->3->4, 2->6]将它们合并到一个有序链表中得到。1->1->2->3->4->4->5->6

            示例 2:

            输入:lists = []
            输出:
            []

            示例 3:

            输入:lists = [[]]
            输出:
            []

             

            提示:

            • k == lists.length
            • 0 <= k <= 10^4
            • 0 <= lists[i].length <= 500
            • -10^4 <= lists[i][j] <= 10^4
            • lists[i]升序 排列
            • lists[i].length 的总和不超过 10^4
            + +## template + +```java + +public class ListNode { + int val; + ListNode next; + + ListNode() { + } + + ListNode(int val) { + this.val = val; + } + + ListNode(int val, ListNode next) { + this.val = val; + this.next = next; + } +} + +class Solution { + public ListNode mergeKLists(ListNode[] lists) { + if (lists.length == 0) + return null; + + return merge(lists, 0, lists.length - 1); + } + + public ListNode merge(ListNode[] lists, int low, int high) { + if (high - low == 0) + return lists[low]; + else if (high - low == 1) + return mergeTwoLists(lists[low], lists[high]); + else { + int mid = (low + high) / 2; + ListNode tmp1 = merge(lists, low, mid); + ListNode tmp2 = merge(lists, mid + 1, high); + return mergeTwoLists(tmp1, tmp2); + } + } + + public ListNode mergeTwoLists(ListNode l1, ListNode l2) { + ListNode head = new ListNode(); + ListNode p = head; + while (l1 != null && l2 != null) { + if (l1.val > l2.val) { + p.next = l2; + l2 = l2.next; + p = p.next; + } else { + p.next = l1; + l1 = l1.next; + p = p.next; + } + } + if (l1 != null) + p.next = l1; + if (l2 != null) + p.next = l2; + return head.next; + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/18.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/18.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..bb9eb89f3186724bc9c970dcc1df80f1a5cc735d --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/18.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-c40fd6978f0942f6bfec5aa1a1860068", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/18.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/18.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..236ff9928b119c2dab9c543f2ff1a14b582a1434 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/18.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "ae60e1c1cfac4b958e3de51bc52958bb" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/18.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/2.java/18.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..13a1e6b6feb076f9423903751efe65909c4914de --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/18.exercises/solution.md" @@ -0,0 +1,55 @@ +# 排列序列 + +

            给出集合 [1,2,3,...,n],其所有元素共有 n! 种排列。

            按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下:

            1. "123"
            2. "132"
            3. "213"
            4. "231"
            5. "312"
            6. "321"

            给定 n 和 k,返回第 k 个排列。

             

            示例 1:

            输入:n = 3, k = 3
            输出:
            "213"

            示例 2:

            输入:n = 4, k = 9
            输出:
            "2314"

            示例 3:

            输入:n = 3, k = 1
            输出:
            "123"

             

            提示:

            • 1 <= n <= 9
            • 1 <= k <= n!
            + +## template + +```java +class Solution { + public String getPermutation(int n, int k) { + StringBuilder sb = new StringBuilder(); + List candidates = new ArrayList<>(); + int[] factorials = new int[n + 1]; + factorials[0] = 1; + int fact = 1; + for (int i = 1; i <= n; ++i) { + candidates.add(i); + fact *= i; + factorials[i] = fact; + } + k -= 1; + for (int i = n - 1; i >= 0; --i) { + int index = k / factorials[i]; + sb.append(candidates.remove(index)); + k -= index * factorials[i]; + } + return sb.toString(); + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/19.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/19.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..cd4cf4b7da1aab65d01c91ae9ac85afbec223e7a --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/19.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-3b3a9cb9b6524693bef12ffd315b0478", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/19.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/19.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..5f005eb0a8490a83686c2d4fc1d68b29bd5edd2a --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/19.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "bf063df722ac4b17b3a2c6291c4ff647" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/19.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/2.java/19.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..a3d715f1d9e1ea4bf269ebdbfa33a2338e0e82d6 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/19.exercises/solution.md" @@ -0,0 +1,62 @@ +# 通配符匹配 + +

            给定一个字符串 (s) 和一个字符模式 (p) ,实现一个支持 '?' 和 '*' 的通配符匹配。

            '?' 可以匹配任何单个字符。'*' 可以匹配任意字符串(包括空字符串)。

            两个字符串完全匹配才算匹配成功。

            说明:

            • s 可能为空,且只包含从 a-z 的小写字母。
            • p 可能为空,且只包含从 a-z 的小写字母,以及字符 ? 和 *

            示例 1:

            输入:s = "aa"p = "a"
            输出:
            false
            解释:
            "a" 无法匹配 "aa" 整个字符串。

            示例 2:

            输入:s = "aa"p = "*"
            输出:
            true
            解释:
             '*' 可以匹配任意字符串。

            示例 3:

            输入:s = "cb"p = "?a"
            输出:
            false
            解释:
             '?' 可以匹配 'c', 但第二个 'a' 无法匹配 'b'。

            示例 4:

            输入:s = "adceb"p = "*a*b"
            输出:
            true
            解释:
             第一个 '*' 可以匹配空字符串, 第二个 '*' 可以匹配字符串 "dce".

            示例 5:

            输入:s = "acdcb"p = "a*c?b"
            输出:
            false
            + +## template + +```java +class Solution { + public boolean isMatch(String s, String p) { + boolean[][] value = new boolean[p.length() + 1][s.length() + 1]; + value[0][0] = true; + for (int i = 1; i <= s.length(); i++) { + value[0][i] = false; + } + for (int i = 1; i <= p.length(); i++) { + if (p.charAt(i - 1) == '*') { + value[i][0] = value[i - 1][0]; + for (int j = 1; j <= s.length(); j++) { + value[i][j] = (value[i][j - 1] || value[i - 1][j]); + } + } else if (p.charAt(i - 1) == '?') { + value[i][0] = false; + for (int j = 1; j <= s.length(); j++) { + value[i][j] = value[i - 1][j - 1]; + } + } else { + value[i][0] = false; + for (int j = 1; j <= s.length(); j++) { + value[i][j] = s.charAt(j - 1) == p.charAt(i - 1) && value[i - 1][j - 1]; + } + } + } + return value[p.length()][s.length()]; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/2.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/2.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..a679de1992f88b79425993eb7648f8984cc7889d --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/2.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-894c432c931e42588ae1c5ed59bdce46", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/2.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/2.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..97b73a161282aa1e0c05c9ae01b4bd1dcd951ffe --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/2.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "bfca7419ebbf440d80aad4599155e032" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/2.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/2.java/2.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..a2c7645eabd0e2a7520aca43258534c72f7a43aa --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/2.exercises/solution.md" @@ -0,0 +1,62 @@ +# 接雨水 + +

            给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。

             

            示例 1:

            输入:height = [0,1,0,2,1,0,1,3,2,1,2,1]
            输出:
            6
            解释:
            上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况下,可以接 6 个单位的雨水(蓝色部分表示雨水)。

            示例 2:

            输入:height = [4,2,0,3,2,5]
            输出:
            9

             

            提示:

            • n == height.length
            • 0 <= n <= 3 * 104
            • 0 <= height[i] <= 105
            + +## template + +```java +class Solution { + public int trap(int[] height) { + if (height == null) + return 0; + int len = height.length; + if (len == 0) + return 0; + int res = 0; + int[] left_max = new int[len]; + int[] right_max = new int[len]; + left_max[0] = height[0]; + + for (int i = 1; i < len; i++) { + left_max[i] = Math.max(height[i], left_max[i - 1]); + } + right_max[len - 1] = height[len - 1]; + + for (int i = len - 2; i >= 0; i--) { + right_max[i] = Math.max(height[i], right_max[i + 1]); + } + + for (int i = 1; i < len - 1; i++) { + res += Math.min(left_max[i], right_max[i]) - height[i]; + } + return res; + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/20.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/20.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..79acc1f027ef14555fa7dc44b9cac2aace3f2cc6 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/20.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-4828e4f9a4b94cea9b605f1a9aeecf74", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/20.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/20.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..c3cf97cb9712e35e8f559c512cd9f08511f8b210 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/20.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "917245efee6c4029866e04956600c992" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/20.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/2.java/20.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..c60dc378724513ac0e631a8b2c662a5834034dc5 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/20.exercises/solution.md" @@ -0,0 +1,46 @@ +# 正则表达式匹配 + +

            给你一个字符串 s 和一个字符规律 p,请你来实现一个支持 '.' 和 '*' 的正则表达式匹配。

            • '.' 匹配任意单个字符
            • '*' 匹配零个或多个前面的那一个元素

            所谓匹配,是要涵盖 整个 字符串 s的,而不是部分字符串。

            示例 1:

            输入:s = "aa" p = "a"
            输出:
            false
            解释:
            "a" 无法匹配 "aa" 整个字符串。

            示例 2:

            输入:s = "aa" p = "a*"
            输出:
            true
            解释:
            因为 '*' 代表可以匹配零个或多个前面的那一个元素, 在这里前面的元素就是 'a'。因此,字符串 "aa" 可被视为 'a' 重复了一次。

            示例 3:

            输入:s = "ab" p = ".*"
            输出:
            true
            解释:
            ".*" 表示可匹配零个或多个('*')任意字符('.')。

            示例 4:

            输入:s = "aab" p = "c*a*b"
            输出:
            true
            解释:
            因为 '*' 表示零个或多个,这里 'c' 为 0 个, 'a' 被重复一次。因此可以匹配字符串 "aab"。

            示例 5:

            输入:s = "mississippi" p = "mis*is*p*."
            输出:
            false

             

            提示:

            • 0 <= s.length <= 20
            • 0 <= p.length <= 30
            • s 可能为空,且只包含从 a-z 的小写字母。
            • p 可能为空,且只包含从 a-z 的小写字母,以及字符 . 和 *
            • 保证每次出现字符 * 时,前面都匹配到有效的字符
            + +## template + +```java +class Solution { + public boolean isMatch(String s, String p) { + if (p.length() == 0) + return s.length() == 0; + boolean head_match = s.length() > 0 && (s.charAt(0) == p.charAt(0) || p.charAt(0) == '.'); + if (p.length() > 1 && p.charAt(1) == '*') { + return (head_match && isMatch(s.substring(1), p)) || isMatch(s, p.substring(2)); + } else { + return head_match && isMatch(s.substring(1), p.substring(1)); + } + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/3.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/3.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..7dddc998d3cf6d2e4eaaf8fda1be2a23c21a0a73 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/3.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-4a4dd604116147869465de2d8743bcbd", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/3.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/3.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..2f4b6ee73b7c2bb8c04df4ad2f8a95a93e16f615 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/3.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "93877dc3327f4c06af563267b8f7c079" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/3.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/2.java/3.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..6f7e1e4dae6560718ec8f3ed374cdda6194cd49e --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/3.exercises/solution.md" @@ -0,0 +1,59 @@ +# 柱状图中最大的矩形 + +

            给定 n 个非负整数,用来表示柱状图中各个柱子的高度。每个柱子彼此相邻,且宽度为 1 。

            求在该柱状图中,能够勾勒出来的矩形的最大面积。

             

            以上是柱状图的示例,其中每个柱子的宽度为 1,给定的高度为 [2,1,5,6,2,3]

             

            图中阴影部分为所能勾勒出的最大矩形面积,其面积为 10 个单位。

             

            示例:

            输入: [2,1,5,6,2,3]
            输出:
            10
            + +## template + +```java +class Solution { + public int largestRectangleArea(int[] heights) { + int length = heights.length; + if (length == 0) { + return 0; + } + int maxSize = 0; + for (int i = 0; i < length; i++) { + int nowHeight = heights[i]; + int nowWidth = 0; + for (int j = i; j < length; j++) { + if (heights[j] < nowHeight) { + nowHeight = heights[j]; + } + nowWidth++; + if (maxSize < nowHeight * nowWidth) { + maxSize = nowHeight * nowWidth; + } + } + + } + return maxSize; + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/4.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/4.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..d34c7d52ac929a199bcf87358d162f68cdfcd1d4 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/4.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-43d3625add4e4caf8c76f756b2607d50", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/4.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/4.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..be07fed5c9565dd9d9add30114fbf159777da47f --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/4.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "d86a7aba9d344c4291f118bfd6936243" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/4.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/2.java/4.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..6eef76dd725521f69907a2c059ab0495afd38f93 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/4.exercises/solution.md" @@ -0,0 +1,64 @@ +# 缺失的第一个正数 + +

            给你一个未排序的整数数组 nums ,请你找出其中没有出现的最小的正整数。

             

            进阶:你可以实现时间复杂度为 O(n) 并且只使用常数级别额外空间的解决方案吗?

             

            示例 1:

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

            示例 2:

            输入:nums = [3,4,-1,1]
            输出:
            2

            示例 3:

            输入:nums = [7,8,9,11,12]
            输出:
            1

             

            提示:

            • 0 <= nums.length <= 300
            • -231 <= nums[i] <= 231 - 1
            + +## template + +```java +class Solution { + public int firstMissingPositive(int[] nums) { + int n = nums.length; + int contains = 0; + for (int i = 0; i < n; i++) { + if (nums[i] == 1) { + contains++; + break; + } + } + if (contains == 0) { + return 1; + } + for (int i = 0; i < n; i++) { + if ((nums[i] <= 0) || (nums[i] > n)) { + nums[i] = 1; + } + } + for (int i = 0; i < n; i++) { + int a = Math.abs(nums[i]); + nums[a - 1] = -Math.abs(nums[a - 1]); + } + for (int i = 0; i < n; i++) { + if (nums[i] > 0) { + return i + 1; + } + } + return n + 1; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/5.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/5.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..2fc893aaec6e600517b09fdfe850c4d6e5d118a0 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/5.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-e5b51393877949059971bac7fe2a126e", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/5.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/5.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..4a58f4b8b06b67a17e8dfa571e57296c98933e35 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/5.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "aaee36dc0a264959bbc9198e547025d1" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/5.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/2.java/5.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..61804cb004e7d86dc855203baab68d2c73b1d946 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/5.exercises/solution.md" @@ -0,0 +1,96 @@ +# N皇后 II + +
            +

            n 皇后问题 研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击。

            + +

            给你一个整数 n ,返回 n 皇后问题 不同的解决方案的数量。

            + +

             

            + +
            +
            +

            示例 1:

            + +
            输入:n = 4
            +输出:2
            +解释:如上图所示,4 皇后问题存在两个不同的解法。
            +    
            + +

            示例 2:

            + +
            输入:n = 1
            +输出:1
            +    
            + +

             

            + +

            提示:

            + +
              +
            • 1 <= n <= 9
            • +
            • 皇后彼此不能相互攻击,也就是说:任何两个皇后都不能处于同一条横行、纵行或斜线上。
            • +
            +
            +
            +
            + +## template + +```java +class Solution { + private boolean col[]; + private boolean dia1[]; + private boolean dia2[]; + public int totalNQueens(int n) { + col = new boolean[n]; + dia1 = new boolean[2 * n - 1]; + dia2 = new boolean[2 * n - 1]; + return putQueen(n, 0); + } + private int putQueen(int n, int index) { + int res = 0; + if (index == n) { + return 1; + } + for (int i = 0; i < n; i++) { + if (!col[i] && !dia1[i - index + n - 1] && !dia2[i + index]) { + col[i] = true; + dia1[i - index + n - 1] = true; + dia2[i + index] = true; + res += putQueen(n, index + 1); + col[i] = false; + dia1[i - index + n - 1] = false; + dia2[i + index] = false; + } + } + return res; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/6.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/6.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..ff0ca2e8158bddf8955fea98db3ef6d1530ce4d4 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/6.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-22fd4b1faf8f45d8ba7683fbcd996752", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/6.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/6.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..410100db32b1d6adbe1d95b9a0fd59d91ec1a163 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/6.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "ed4ebf1e352d47b5b9dcc0f05fc7a3cc" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/6.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/2.java/6.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..28a27040adc465922396f4b27d97d04eccecd8dd --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/6.exercises/solution.md" @@ -0,0 +1,79 @@ +# 最大矩形 + +

            给定一个仅包含 01 、大小为 rows x cols 的二维二进制矩阵,找出只包含 1 的最大矩形,并返回其面积。

             

            示例 1:

            输入:matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]
            输出:
            6
            解释:
            最大矩形如上图所示。

            示例 2:

            输入:matrix = []
            输出:
            0

            示例 3:

            输入:matrix = [["0"]]
            输出:
            0

            示例 4:

            输入:matrix = [["1"]]
            输出:
            1

            示例 5:

            输入:matrix = [["0","0"]]
            输出:
            0

             

            提示:

            • rows == matrix.length
            • cols == matrix[0].length
            • 0 <= row, cols <= 200
            • matrix[i][j]'0''1'
            + +## template + +```java +class Solution { + public int maximalRectangle(char[][] matrix) { + if (matrix == null || matrix.length == 0) + return 0; + int m = matrix.length; + int n = matrix[0].length; + int[] left = new int[n]; + int[] right = new int[n]; + int[] height = new int[n]; + Arrays.fill(right, n); + int cur_left = 0; + int cur_right = n; + int res = 0; + for (int i = 0; i < m; i++) { + cur_left = 0; + cur_right = n; + for (int j = 0; j < n; j++) { + if (matrix[i][j] == '1') + height[j]++; + else + height[j] = 0; + } + for (int j = 0; j < n; j++) { + if (matrix[i][j] == '1') { + left[j] = Math.max(left[j], cur_left); + } else { + left[j] = 0; + cur_left = j + 1; + } + } + for (int j = n - 1; j >= 0; j--) { + if (matrix[i][j] == '1') { + right[j] = Math.min(right[j], cur_right); + } else { + right[j] = n; + cur_right = j; + } + } + for (int j = 0; j < n; j++) + res = Math.max(res, (right[j] - left[j]) * height[j]); + } + return res; + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/7.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/7.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..5a3908cb481b47e9d80a03618243372047b8861d --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/7.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-9db5c2c258e748b5b3bd42b205f0464e", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/7.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/7.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..6d1cdf48d7ad0fc76bb2211dd5331fae3c10863e --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/7.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "f10c4b1e71424d5db17ca09695a88634" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/7.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/2.java/7.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..0927dc2263e5532abfdc38e0af4b91927466dfde --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/7.exercises/solution.md" @@ -0,0 +1,80 @@ +# 编辑距离 + +

            给你两个单词 word1 和 word2,请你计算出将 word1 转换成 word2 所使用的最少操作数 。

            你可以对一个单词进行如下三种操作:

            • 插入一个字符
            • 删除一个字符
            • 替换一个字符

             

            示例 1:

            输入:word1 = "horse", word2 = "ros"
            输出:
            3
            解释:
            horse -> rorse (将 'h' 替换为 'r')rorse -> rose (删除 'r')rose -> ros (删除 'e')

            示例 2:

            输入:word1 = "intention", word2 = "execution"
            输出:
            5
            解释:
            intention -> inention (删除 't')inention -> enention (将 'i' 替换为 'e')enention -> exention (将 'n' 替换为 'x')exention -> exection (将 'n' 替换为 'c')exection -> execution (插入 'u')

             

            提示:

            • 0 <= word1.length, word2.length <= 500
            • word1word2 由小写英文字母组成
            + +## template + +```java +class Solution { + public int minDistance(String word1, String word2) { + int len1 = word1.length(); + int len2 = word2.length(); + + if (len1 * len2 == 0) + return len1 + len2; + String longerStr = len1 > len2 ? word1 : word2; + String shorterStr = len1 > len2 ? word2 : word1; + int shorterOne = Math.min(len1, len2); + + int[] dp = new int[shorterOne + 1]; + + for (int i = 0; i < shorterOne + 1; i++) { + dp[i] = i; + } + + for (int j = 1; j <= longerStr.length(); j++) { + + int left = j; + + for (int i = 1; i <= shorterStr.length(); i++) { + int updateDown = dp[i] + 1; + int updateLeft = left + 1; + int updateLeftDown = dp[i - 1]; + + if (longerStr.charAt(j - 1) != shorterStr.charAt(i - 1)) { + updateLeftDown++; + } + + int min = Math.min(updateLeft, Math.min(updateDown, updateLeftDown)); + + dp[i - 1] = left; + + if (i == dp.length - 1) { + dp[i] = min; + } else { + + left = min; + } + } + } + return dp[shorterOne]; + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/8.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/8.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..7b171d1c7ca59d7f1ce92008d6b920b5fa20ab6c --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/8.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-787c0280ffcc44bdbfc0e233f6d19775", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/8.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/8.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..31bc9e383e9d621a651f8df45f5160e2581efb86 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/8.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "2dfeb26e6a8f43cbb80600eab2bd5544" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/8.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/2.java/8.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..76d69491f91c47a2239a16ccbad4509a5570eb96 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/8.exercises/solution.md" @@ -0,0 +1,141 @@ +# 文本左右对齐 + +
            +

            给定一个单词数组和一个长度 maxWidth,重新排版单词,使其成为每行恰好有 maxWidth 个字符,且左右两端对齐的文本。

            + +

            你应该使用“贪心算法”来放置给定的单词;也就是说,尽可能多地往每行中放置单词。必要时可用空格 ' ' 填充,使得每行恰好有 maxWidth 个字符。 +

            + +

            要求尽可能均匀分配单词间的空格数量。如果某一行单词间的空格不能均匀分配,则左侧放置的空格数要多于右侧的空格数。

            + +

            文本的最后一行应为左对齐,且单词之间不插入额外的空格。

            + +

            说明:

            + +
              +
            • 单词是指由非空格字符组成的字符序列。
            • +
            • 每个单词的长度大于 0,小于等于 maxWidth
            • +
            • 输入单词数组 words 至少包含一个单词。
            • +
            + +

            示例:

            + +
            输入:
            +    words = ["This", "is", "an", "example", "of", "text", "justification."]
            +    maxWidth = 16
            +输出:
            +    [
            +       "This    is    an",
            +       "example  of text",
            +       "justification.  "
            +    ]
            +    
            + +

            示例 2:

            + +
            输入:
            +    words = ["What","must","be","acknowledgment","shall","be"]
            +    maxWidth = 16
            +输出:
            +    [
            +      "What   must   be",
            +      "acknowledgment  ",
            +      "shall be        "
            +    ]
            +解释: 注意最后一行的格式应为 "shall be    " 而不是 "shall     be"
            +    因为最后一行应为左对齐,而不是左右两端对齐,第二行同样为左对齐,这是因为这行只包含一个单词。
            +    
            + +

            示例 3:

            + +
            输入:
            +    words = ["Science","is","what","we","understand","well","enough","to","explain",
            +             "to","a","computer.","Art","is","everything","else","we","do"]
            +    maxWidth = 20
            +输出:
            +    [
            +      "Science  is  what we",
            +      "understand      well",
            +      "enough to explain to",
            +      "a  computer.  Art is",
            +      "everything  else  we",
            +      "do                  "
            +    ]
            +    
            +
            + +## template + +```java +class Solution { + public List fullJustify(String[] words, int maxWidth) { + List ret = new ArrayList<>(); + int index = 0; + while (index < words.length) { + int cur = index, len = 0; + while (cur < words.length && len + words[cur].length() + cur - index <= maxWidth) { + len = len + words[cur++].length(); + } + cur--; + StringBuilder sb = new StringBuilder(); + if (cur == words.length - 1) { + for (int i = index; i <= cur; i++) { + sb.append(words[i]); + if (i < cur) { + sb.append(' '); + } + } + } else { + int base = cur > index ? (maxWidth - len) / (cur - index) : (maxWidth - len); + String baseStr = genSpace(base); + int left = cur > index ? (maxWidth - len) % (cur - index) : 0; + String leftStr = genSpace(base + 1); + for (int i = index; i <= cur; i++) { + sb.append(words[i]); + if (i < cur) { + sb.append(left > 0 ? leftStr : baseStr); + left--; + } + } + } + if (sb.length() < maxWidth) { + sb.append(genSpace(maxWidth - sb.length())); + } + ret.add(sb.toString()); + index = cur + 1; + } + return ret; + } + private String genSpace(int n) { + char[] cs = new char[n]; + Arrays.fill(cs, ' '); + return new String(cs); + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/9.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/9.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..42ca448ce7a574ce7a70d4ab8dd0da572e1fe453 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/9.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-3bf822254625411daaa0b2b036abc32c", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/9.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/9.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..30652f4ff84cea0bcbc43e3d7a8ae71f3ed7608d --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/9.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "20c748d431504de69be93cfe278e75fb" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/9.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/2.java/9.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..99863e962d1d0cf065df4bb5a1a20eb331a6e388 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/9.exercises/solution.md" @@ -0,0 +1,65 @@ +# 最长有效括号 + +

            给你一个只包含 '(' 和 ')' 的字符串,找出最长有效(格式正确且连续)括号子串的长度。

             

            示例 1:

            输入:s = "(()"
            输出:
            2
            解释:
            最长有效括号子串是 "()"

            示例 2:

            输入:s = ")()())"
            输出:
            4
            解释:
            最长有效括号子串是 "()()"

            示例 3:

            输入:s = ""
            输出:
            0

             

            提示:

            • 0 <= s.length <= 3 * 104
            • s[i]'('')'
            + +## template + +```java +import java.util.*; + +class Solution { + public int longestValidParentheses(String s) { + int left = 0, right = 0, max = 0; + for (int i = 0; i < s.length(); i++) { + if (s.charAt(i) == '(') + left++; + else + right++; + if (left == right) + max = Math.max(max, left * 2); + if (right > left) + left = right = 0; + } + left = 0; + right = 0; + for (int i = s.length() - 1; i >= 0; i--) { + if (s.charAt(i) == '(') + left++; + else + right++; + if (left == right) + max = Math.max(max, left * 2); + if (right < left) + left = right = 0; + } + return max; + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/3.python/10.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/3.python/10.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..f5ad4383f497e39a726fdf6dd6d1c01bf8a404a4 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/3.python/10.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-dbe9e50524574e9eb2e133750871b387", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/3.python/10.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/3.python/10.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..7ef2b97aa8f5d8e758fd0e32cab5471b22ac55a4 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/3.python/10.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "fa3246e15948481f8be7c835a6ba40ac" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/3.python/10.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/3.python/10.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..3e841be80a11284326dfa6a4b9fc6bd106b270a7 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/3.python/10.exercises/solution.md" @@ -0,0 +1,59 @@ +# 最长有效括号 + +

            给你一个只包含 '(' 和 ')' 的字符串,找出最长有效(格式正确且连续)括号子串的长度。

             

            示例 1:

            输入:s = "(()"
            输出:
            2
            解释:
            最长有效括号子串是 "()"

            示例 2:

            输入:s = ")()())"
            输出:
            4
            解释:
            最长有效括号子串是 "()()"

            示例 3:

            输入:s = ""
            输出:
            0

             

            提示:

            • 0 <= s.length <= 3 * 104
            • s[i]'('')'
            + +## template + +```python +import pdb +class Solution(object): + def longestValidParentheses(self, s): + ls = len(s) + stack = [] + data = [0] * ls + for i in range(ls): + curr = s[i] + if curr == '(': + stack.append(i) + else: + if len(stack) > 0: + data[i] = 1 + data[stack.pop(-1)] = 1 + tep, res = 0, 0 + for t in data: + if t == 1: + tep += 1 + else: + res = max(tep, res) + tep = 0 + return max(tep, res) +if __name__ == '__main__': + s = Solution() + print(s.longestValidParentheses(')()())')) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/3.python/11.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/3.python/11.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..426c1669192c1624fe787ec7df3210d620ab520b --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/3.python/11.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-970d72a6bd674165abd0240d25cdd7bc", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/3.python/11.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/3.python/11.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..9e990fd636a39a1d135158649fa945799a9ae43d --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/3.python/11.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "8d0a2beec7e44e4a875c24f7fcf2d2a8" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/3.python/11.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/3.python/11.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..5dbf2283274852aaf34a02c0a27b5e8b9c3ee983 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/3.python/11.exercises/solution.md" @@ -0,0 +1,67 @@ +# N 皇后 + +

            n 皇后问题 研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击。

            给你一个整数 n ,返回所有不同的 n 皇后问题 的解决方案。

            每一种解法包含一个不同的 n 皇后问题 的棋子放置方案,该方案中 'Q''.' 分别代表了皇后和空位。

             

            示例 1:

            输入:n = 4
            输出:
            [[".Q..","...Q","Q...","..Q."],["..Q.","Q...","...Q",".Q.."]]
            解释:
            如上图所示,4 皇后问题存在两个不同的解法。

            示例 2:

            输入:n = 1
            输出:
            [["Q"]]

             

            提示:

            • 1 <= n <= 9
            • 皇后彼此不能相互攻击,也就是说:任何两个皇后都不能处于同一条横行、纵行或斜线上。
            + +## template + +```python +class Solution(object): + def solveNQueens(self, n): + if n == 0: + return 0 + res = [] + board = [['.'] * n for t in range(n)] + self.do_solveNQueens(res, board, n) + return res + def do_solveNQueens(self, res, board, num): + if num == 0: + res.append([''.join(t) for t in board]) + return + ls = len(board) + pos = ls - num + check = [True] * ls + for i in range(pos): + for j in range(ls): + if board[i][j] == 'Q': + check[j] = False + step = pos - i + if j + step < ls: + check[j + step] = False + if j - step >= 0: + check[j - step] = False + break + for j in range(ls): + if check[j]: + board[pos][j] = 'Q' + self.do_solveNQueens(res, board, num - 1) + board[pos][j] = '.' +if __name__ == '__main__': + s = Solution() + print (s.solveNQueens(4)) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/3.python/12.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/3.python/12.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..8db01207a5fbcc86d06be417a29282d62cab5c01 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/3.python/12.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-cc8c0b02e2f0412fbf191dbd11982605", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/3.python/12.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/3.python/12.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..81b5c5e7e8f3993cdee31913ffa8148701d876d7 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/3.python/12.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "c0f6a5bad4674d0b9e35cedb71d068fe" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/3.python/12.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/3.python/12.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..5434889ae7617c2824b9855dfe6f9aeda9983084 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/3.python/12.exercises/solution.md" @@ -0,0 +1,115 @@ +# 扰乱字符串 + +
            使用下面描述的算法可以扰乱字符串 s 得到字符串 t : +
              +
            1. 如果字符串的长度为 1 ,算法停止
            2. +
            3. 如果字符串的长度 > 1 ,执行下述步骤: +
                +
              • 在一个随机下标处将字符串分割成两个非空的子字符串。即,如果已知字符串 s ,则可以将其分成两个子字符串 xy + ,且满足 s = x + y
              • +
              • 随机 决定是要「交换两个子字符串」还是要「保持这两个子字符串的顺序不变」。即,在执行这一步骤之后,s 可能是 + s = x + y 或者 s = y + x 。 +
              • +
              • xy 这两个子字符串上继续从步骤 1 开始递归执行此算法。
              • +
              +
            4. +
            + +

            给你两个 长度相等 的字符串 s1 + 和 s2,判断 s2 是否是 s1 的扰乱字符串。如果是,返回 + true ;否则,返回 false 。 +

            + +

             

            + +

            示例 1:

            + +
            输入:s1 = "great", s2 = "rgeat"
            +输出:true
            +解释:s1 上可能发生的一种情形是:
            +"great" --> "gr/eat" // 在一个随机下标处分割得到两个子字符串
            +"gr/eat" --> "gr/eat" // 随机决定:「保持这两个子字符串的顺序不变」
            +"gr/eat" --> "g/r / e/at" // 在子字符串上递归执行此算法。两个子字符串分别在随机下标处进行一轮分割
            +"g/r / e/at" --> "r/g / e/at" // 随机决定:第一组「交换两个子字符串」,第二组「保持这两个子字符串的顺序不变」
            +"r/g / e/at" --> "r/g / e/ a/t" // 继续递归执行此算法,将 "at" 分割得到 "a/t"
            +"r/g / e/ a/t" --> "r/g / e/ a/t" // 随机决定:「保持这两个子字符串的顺序不变」
            +算法终止,结果字符串和 s2 相同,都是 "rgeat"
            +这是一种能够扰乱 s1 得到 s2 的情形,可以认为 s2 是 s1 的扰乱字符串,返回 true
            +
            + +

            示例 2:

            + +
            输入:s1 = "abcde", s2 = "caebd"
            +输出:false
            +
            + +

            示例 3:

            + +
            输入:s1 = "a", s2 = "a"
            +输出:true
            +
            + +

             

            + +

            提示:

            + +
              +
            • s1.length == s2.length
            • +
            • 1 <= s1.length <= 30
            • +
            • s1s2 由小写英文字母组成
            • +
            +
            + +## template + +```python +class Solution(object): + def isScramble(self, s1, s2, memo={}): + if len(s1) != len(s2) or sorted(s1) != sorted(s2): + return False + if len(s1) <= len(s2) <= 1: + return s1 == s2 + if s1 == s2: + return True + if (s1, s2) in memo: + return memo[s1, s2] + n = len(s1) + for i in range(1, n): + a = self.isScramble(s1[:i], s2[:i], memo) and self.isScramble(s1[i:], s2[i:], memo) + if not a: + b = self.isScramble(s1[:i], s2[-i:], memo) and self.isScramble(s1[i:], s2[:-i], memo) + if a or b: + memo[s1, s2] = True + return True + memo[s1, s2] = False + return False +# %% +s = Solution() +print(s.isScramble(s1 = "great", s2 = "rgeat")) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/3.python/13.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/3.python/13.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..bca6bbe63730d8035ef02c2c015dbbdc0e95a664 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/3.python/13.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-ef57e1d2a91a40dfa99e8f4ad06f1680", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/3.python/13.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/3.python/13.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..2806c97594a0ad27ec893f71aacdfecac0ad81bb --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/3.python/13.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "03af526f0d6042c68f55c8efc4c4b0b7" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/3.python/13.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/3.python/13.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..128e07a7c74750ac6576d21699d73308e6d289ad --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/3.python/13.exercises/solution.md" @@ -0,0 +1,95 @@ +# K 个一组翻转链表 + +

            给你一个链表,每 个节点一组进行翻转,请你返回翻转后的链表。

            是一个正整数,它的值小于或等于链表的长度。

            如果节点总数不是 的整数倍,那么请将最后剩余的节点保持原有顺序。

            进阶:

            • 你可以设计一个只使用常数额外空间的算法来解决此问题吗?
            • 你不能只是单纯的改变节点内部的值,而是需要实际进行节点交换。

             

            示例 1:

            输入:head = [1,2,3,4,5], k = 2
            输出:
            [2,1,4,3,5]

            示例 2:

            输入:head = [1,2,3,4,5], k = 3
            输出:
            [3,2,1,4,5]

            示例 3:

            输入:head = [1,2,3,4,5], k = 1
            输出:
            [1,2,3,4,5]

            示例 4:

            输入:head = [1], k = 1
            输出:
            [1]

              提示:

              • 列表中节点的数量在范围 sz
              • 1 <= sz <= 5000
              • 0 <= Node.val <= 1000
              • 1 <= k <= sz
              + +## template + +```python +class ListNode(object): + def __init__(self, x): + self.val = x + self.next = None +class LinkList: + def __init__(self): + self.head=None + def initList(self, data): + self.head = ListNode(data[0]) + r=self.head + p = self.head + for i in data[1:]: + node = ListNode(i) + p.next = node + p = p.next + return r + def convert_list(self,head): + ret = [] + if head == None: + return + node = head + while node != None: + ret.append(node.val) + node = node.next + return ret +class Solution(object): + def reverseKGroup(self, head, k): + if head is None: + return None + index = 0 + lead, last = 0, 0 + pos = head + temp = ListNode(-1) + temp.next = head + head = temp + start = head + while pos is not None: + if index % k == k - 1: + last = pos.next + start = self.reverseList(start, last) + pos = start + pos = pos.next + index += 1 + return head.next + def reverseList(self, head, end): + pos = head.next + last = end + next_start = pos + while pos != end: + head.next = pos + last_pos = pos + pos = pos.next + last_pos.next = last + last = last_pos + return next_start +# %% +l = LinkList() +head = [1,2,3,4, 5] +l1 = l.initList(head) +s = Solution() +print(l.convert_list(s.reverseKGroup(l1, k = 2))) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/3.python/14.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/3.python/14.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..50d333fcc2e07e75b4618f35c0cbe13c9e463c5f --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/3.python/14.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-3c5a1ffd711946f59479cad4589d1c33", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/3.python/14.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/3.python/14.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..eb914f8a4c6eef78bfa08de68657ad5838d061db --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/3.python/14.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "ece3f29c60f849519fa234fbc315a80b" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/3.python/14.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/3.python/14.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..8ea7d615b87ca4ebc78e525b0fb1d9ec765750d1 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/3.python/14.exercises/solution.md" @@ -0,0 +1,65 @@ +# 有效数字 + +

              有效数字(按顺序)可以分成以下几个部分:

              1. 一个 小数 或者 整数
              2. (可选)一个 'e''E' ,后面跟着一个 整数

              小数(按顺序)可以分成以下几个部分:

              1. (可选)一个符号字符('+''-'
              2. 下述格式之一:
                1. 至少一位数字,后面跟着一个点 '.'
                2. 至少一位数字,后面跟着一个点 '.' ,后面再跟着至少一位数字
                3. 一个点 '.' ,后面跟着至少一位数字

              整数(按顺序)可以分成以下几个部分:

              1. (可选)一个符号字符('+''-'
              2. 至少一位数字

              部分有效数字列举如下:

              • ["2", "0089", "-0.1", "+3.14", "4.", "-.9", "2e10", "-90E3", "3e+7", "+6e-1", "53.5e93", "-123.456e789"]

              部分无效数字列举如下:

              • ["abc", "1a", "1e", "e3", "99e2.5", "--6", "-+3", "95a54e53"]

              给你一个字符串 s ,如果 s 是一个 有效数字 ,请返回 true

               

              示例 1:

              输入:s = "0"
              输出:
              true

              示例 2:

              输入:s = "e"
              输出:
              false

              示例 3:

              输入:s = "."
              输出:
              false

              示例 4:

              输入:s = ".1"
              输出:
              true

               

              提示:

              • 1 <= s.length <= 20
              • s 仅含英文字母(大写和小写),数字(0-9),加号 '+' ,减号 '-' ,或者点 '.'
              + +## template + +```python +class Solution(object): + def isNumber(self, s): + s = s.strip() + ls, pos = len(s), 0 + if ls == 0: + return False + if s[pos] == '+' or s[pos] == '-': + pos += 1 + isNumeric = False + while pos < ls and s[pos].isdigit(): + pos += 1 + isNumeric = True + if pos < ls and s[pos] == '.': + pos += 1 + while pos < ls and s[pos].isdigit(): + pos += 1 + isNumeric = True + elif pos < ls and s[pos] == 'e' and isNumeric: + isNumeric = False + pos += 1 + if pos < ls and (s[pos] == '+' or s[pos] == '-'): + pos += 1 + while pos < ls and s[pos].isdigit(): + pos += 1 + isNumeric = True + if pos == ls and isNumeric: + return True + return False +# %% +s = Solution() +print(s.isNumber(s = "0")) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/3.python/15.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/3.python/15.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..95c9c106d833a08052d80821a7c51b4e1aa7efba --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/3.python/15.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-bfb84adb85a9445da24c239c5cc5cb48", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/3.python/15.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/3.python/15.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..1619302463425a63f6c8169c59df3231afa235ae --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/3.python/15.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "d0f9f2899ee04aca8fec068946cb766f" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/3.python/15.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/3.python/15.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..769475f59b42becfab3aa613b6de972bc299b3a7 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/3.python/15.exercises/solution.md" @@ -0,0 +1,73 @@ +# 串联所有单词的子串 + +

              给定一个字符串 和一些长度相同的单词 words。找出 s + 中恰好可以由 words 中所有单词串联形成的子串的起始位置。

              +

              注意子串要与 words 中的单词完全匹配,中间不能有其他字符,但不需要考虑 words 中单词串联的顺序。

              +

               

              +

              示例 1:

              +
              输入:  s = "barfoothefoobarman",  words = ["foo","bar"]
              输出:
              [0,9]
              解释:
              从索引 0 和 9 开始的子串分别是 "barfoo" 和 "foobar" 。输出的顺序不重要, [9,0] 也是有效答案。
              +

              示例 2:

              +
              输入:  s = "wordgoodgoodgoodbestword",  words = ["word","good","best","word"]
              输出:
              []
              + +## template + +```python +class Solution(object): + def findSubstring(self, s, words): + """ + :type s: str + :type words: List[str] + :rtype: List[int] + """ + ls = len(s) + word_ls = len(words[0]) + target_dict = {} + for word in words: + try: + target_dict[word] += 1 + except KeyError: + target_dict[word] = 1 + res = [] + for start in range(ls - word_ls * len(words) + 1): + curr_dict = target_dict.copy() + for pos in range(start, start + word_ls * len(words), word_ls): + curr = s[pos:pos + word_ls] + try: + curr_dict[curr] -= 1 + if curr_dict[curr] < 0: + break + except KeyError: + break + else: + res.append(start) + return res +if __name__ == '__main__': + s = Solution() + print(s.findSubstring('wordgoodgoodgoodbestword', ["word", "good", "best", "good"])) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/3.python/16.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/3.python/16.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..3d936d26606a55829f3d0ce49ba39fca2c11ee63 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/3.python/16.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-7d2edf9aa044428c88ebd5f39dbbef0a", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/3.python/16.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/3.python/16.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..0f79ab89e7f3fae7a93c338c64da5ba49aa61e3f --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/3.python/16.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "ee055db7de8548779f21dfae8d06bf07" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/3.python/16.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/3.python/16.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..29e7db933e9836a88df272342a08f316e295524c --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/3.python/16.exercises/solution.md" @@ -0,0 +1,69 @@ +# 寻找两个正序数组的中位数 + +

              给定两个大小分别为 mn 的正序(从小到大)数组 nums1 和 nums2。请你找出并返回这两个正序数组的 中位数

               

              示例 1:

              输入:nums1 = [1,3], nums2 = [2]
              输出:
              2.00000
              解释:
              合并数组 = [1,2,3] ,中位数 2

              示例 2:

              输入:nums1 = [1,2], nums2 = [3,4]
              输出:
              2.50000
              解释:
              合并数组 = [1,2,3,4] ,中位数 (2 + 3) / 2 = 2.5

              示例 3:

              输入:nums1 = [0,0], nums2 = [0,0]
              输出:
              0.00000

              示例 4:

              输入:nums1 = [], nums2 = [1]
              输出:
              1.00000

              示例 5:

              输入:nums1 = [2], nums2 = []
              输出:
              2.00000

               

              提示:

              • nums1.length == m
              • nums2.length == n
              • 0 <= m <= 1000
              • 0 <= n <= 1000
              • 1 <= m + n <= 2000
              • -106 <= nums1[i], nums2[i] <= 106

               

              进阶:你能设计一个时间复杂度为 O(log (m+n)) 的算法解决此问题吗?

              + +## template + +```python +import math +from typing import List +class Solution: + def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: + nums1Size = len(nums1) + nums2Size = len(nums2) + na = nums1Size + nums2Size + ns = [] + i = 0 + j = 0 + m = int(math.floor(na / 2 + 1)) + while len(ns) < m: + n = None + if i < nums1Size and j < nums2Size: + if nums1[i] < nums2[j]: + n = nums1[i] + i += 1 + else: + n = nums2[j] + j += 1 + elif i < nums1Size: + n = nums1[i] + i += 1 + elif j < nums2Size: + n = nums2[j] + j += 1 + ns.append(n) + d = len(ns) + if na % 2 == 1: + return ns[d - 1] + else: + return (ns[d - 1] + ns[d - 2]) / 2.0 +# %% +s = Solution() +print(s.findMedianSortedArrays([1,3], [2])) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/3.python/17.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/3.python/17.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..a1de958e5e6bdc025c2e8103b73cbcf8151d629c --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/3.python/17.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-9ca3846c01d347ecaed312e10920c3d8", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/3.python/17.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/3.python/17.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..4f4e2b3bfa863b314d26d33a3fea111c21f0c203 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/3.python/17.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "7003d1a084b947fbbe163fd0b0e41dcf" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/3.python/17.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/3.python/17.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..8e634ef8aac667bbfb89bedf4e160586dcd122c4 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/3.python/17.exercises/solution.md" @@ -0,0 +1,71 @@ +# 最小覆盖子串 + +

              给你一个字符串 s 、一个字符串 t 。返回 s 中涵盖 t 所有字符的最小子串。如果 s 中不存在涵盖 t 所有字符的子串,则返回空字符串 ""

              注意:如果 s 中存在这样的子串,我们保证它是唯一的答案。

               

              示例 1:

              输入:s = "ADOBECODEBANC", t = "ABC"
              输出:
              "BANC"

              示例 2:

              输入:s = "a", t = "a"
              输出:
              "a"

               

              提示:

              • 1 <= s.length, t.length <= 105
              • st 由英文字母组成

               

              进阶:你能设计一个在 o(n) 时间内解决此问题的算法吗? + +## template + +```python +class Solution(object): + def minWindow(self, s, t): + ls_s, ls_t = len(s), len(t) + need_to_find = [0] * 256 + has_found = [0] * 256 + min_begin, min_end = 0, -1 + min_window = 100000000000000 + for index in range(ls_t): + need_to_find[ord(t[index])] += 1 + count, begin = 0, 0 + for end in range(ls_s): + end_index = ord(s[end]) + if need_to_find[end_index] == 0: + continue + has_found[end_index] += 1 + if has_found[end_index] <= need_to_find[end_index]: + count += 1 + if count == ls_t: + begin_index = ord(s[begin]) + while need_to_find[begin_index] == 0 or\ + has_found[begin_index] > need_to_find[begin_index]: + if has_found[begin_index] > need_to_find[begin_index]: + has_found[begin_index] -= 1 + begin += 1 + begin_index = ord(s[begin]) + window_ls = end - begin + 1 + if window_ls < min_window: + min_begin = begin + min_end = end + min_window = window_ls + if count == ls_t: + return s[min_begin: min_end + 1] + else: + return '' +if __name__ == '__main__': + s = Solution() + print (s.minWindow('a', 'a')) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/3.python/18.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/3.python/18.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..0d6e86b027b2122cfdc2ae864104079f9bba4ae7 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/3.python/18.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-d909102a85674a31845837c64fb7d51c", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/3.python/18.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/3.python/18.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..ce9979b55eb98d397eb2d462577435795da6063c --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/3.python/18.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "91abee8362554025b75b582c7c82ef23" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/3.python/18.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/3.python/18.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..0011ffee543a49a88fbd4d6b9da8557c0a8d604c --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/3.python/18.exercises/solution.md" @@ -0,0 +1,103 @@ +# 合并K个升序链表 + +

              给你一个链表数组,每个链表都已经按升序排列。

              请你将所有链表合并到一个升序链表中,返回合并后的链表。

               

              示例 1:

              输入:lists = [[1,4,5],[1,3,4],[2,6]]
              输出:
              [1,1,2,3,4,4,5,6]
              解释:
              链表数组如下:[ 1->4->5, 1->3->4, 2->6]将它们合并到一个有序链表中得到。1->1->2->3->4->4->5->6

              示例 2:

              输入:lists = []
              输出:
              []

              示例 3:

              输入:lists = [[]]
              输出:
              []

               

              提示:

              • k == lists.length
              • 0 <= k <= 10^4
              • 0 <= lists[i].length <= 500
              • -10^4 <= lists[i][j] <= 10^4
              • lists[i]升序 排列
              • lists[i].length 的总和不超过 10^4
              + +## template + +```python +from typing import List +class ListNode(object): + def __init__(self, x): + self.val = x + self.next = None +class LinkList: + def __init__(self): + self.head=None + def initList(self, data): + self.head = ListNode(data[0]) + r=self.head + p = self.head + for i in data[1:]: + node = ListNode(i) + p.next = node + p = p.next + return r + def convert_list(self,head): + ret = [] + if head == None: + return + node = head + while node != None: + ret.append(node.val) + node = node.next + return ret +class Solution(object): + def mergeKLists(self, lists): + if lists is None: + return None + elif len(lists) == 0: + return None + return self.mergeK(lists, 0, len(lists) - 1) + def mergeK(self, lists, low, high): + if low == high: + return lists[int(low)] + elif low + 1 == high: + return self.mergeTwolists(lists[int(low)], lists[int(high)]) + mid = (low + high) / 2 + return self.mergeTwolists(self.mergeK(lists, low, mid), self.mergeK(lists, mid + 1, high)) + def mergeTwolists(self, l1, l2): + l = LinkList() + if type(l1) == list: + l1 = l.initList(l1) + if type(l2) == list: + l2 = l.initList(l2) + if l1 is None: + return l2 + if l2 is None: + return l1 + head = curr = ListNode(-1) + while l1 is not None and l2 is not None: + if l1.val <= l2.val: + curr.next = l1 + l1 = l1.next + else: + curr.next = l2 + l2 = l2.next + curr = curr.next + if l1 is not None: + curr.next = l1 + if l2 is not None: + curr.next = l2 + return head.next +# %% +l = LinkList() +list1 = [[1,4,5],[1,3,4],[2,6]] +s = Solution() +print(l.convert_list(s.mergeKLists(list1))) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/3.python/19.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/3.python/19.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..1b436efad679faca1ab6dc65aa8905a2572b4130 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/3.python/19.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-a586184078754c0fbaf4327d93c556c8", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/3.python/19.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/3.python/19.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..c4f830791cdff2e250b381ca4008ccdef6e9a0f5 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/3.python/19.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "088062a41b454721998377719f521b69" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/3.python/19.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/3.python/19.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..0e4b26122bdc05404519ca3610d0273f54c34a94 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/3.python/19.exercises/solution.md" @@ -0,0 +1,69 @@ +# 排列序列 + +

              给出集合 [1,2,3,...,n],其所有元素共有 n! 种排列。

              按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下:

              1. "123"
              2. "132"
              3. "213"
              4. "231"
              5. "312"
              6. "321"

              给定 n 和 k,返回第 k 个排列。

               

              示例 1:

              输入:n = 3, k = 3
              输出:
              "213"

              示例 2:

              输入:n = 4, k = 9
              输出:
              "2314"

              示例 3:

              输入:n = 3, k = 1
              输出:
              "123"

               

              提示:

              • 1 <= n <= 9
              • 1 <= k <= n!
              + +## template + +```python +class Solution(object): + def getPermutation(self, n, k): + """ + :type n: int + :type k: int + :rtype: str + """ + import math + res=[""] + def generate(s,k): + n=len(s) + if n<=2: + if k==2: + res[0]+=s[::-1] + else: + res[0]+=s + return + step = math.factorial(n-1) + yu=k%step + if yu==0: + yu=step + c=k//step-1 + else: + c=k//step + res[0]+=s[c] + generate(s[:c]+s[c+1:],yu) + return + s="" + for i in range(1,n+1): + s+=str(i) + generate(s,k) + return res[0] +if __name__ == '__main__': + s = Solution() + print (s.getPermutation(3, 2)) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/3.python/2.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/3.python/2.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..dbec1c064e3590843bedf2764d57bdaf8b6c5093 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/3.python/2.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-d551cbe0a59e4448ac4a8a9e58421814", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/3.python/2.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/3.python/2.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..d36c354ab871790d3398c26d21ec86bc6de98609 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/3.python/2.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "b26192f40a314aa6a87f2fc69f99a981" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/3.python/2.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/3.python/2.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..29d0a1f4385ca4ed0ad07a65c5367203fe1447de --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/3.python/2.exercises/solution.md" @@ -0,0 +1,116 @@ +# 解数独 + +

              编写一个程序,通过填充空格来解决数独问题。

              +

              数独的解法需 遵循如下规则

              +
                +
              1. 数字 1-9 在每一行只能出现一次。
              2. +
              3. 数字 1-9 在每一列只能出现一次。
              4. +
              5. 数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。(请参考示例图)
              6. +
              +

              数独部分空格内已填入了数字,空白格用 '.' 表示。

              +

               

              +
              +
              +
              +

              示例:

              +
              输入:board = 
              +    [["5","3",".",".","7",".",".",".","."],
              +    ["6",".",".","1","9","5",".",".","."],
              +    [".","9","8",".",".",".",".","6","."],
              +    ["8",".",".",".","6",".",".",".","3"],
              +    ["4",".",".","8",".","3",".",".","1"],
              +    ["7",".",".",".","2",".",".",".","6"],
              +    [".","6",".",".",".",".","2","8","."],
              +    [".",".",".","4","1","9",".",".","5"],
              +    [".",".",".",".","8",".",".","7","9"]]
              +输出:
              +    [["5","3","4","6","7","8","9","1","2"],
              +    ["6","7","2","1","9","5","3","4","8"],
              +    ["1","9","8","3","4","2","5","6","7"],
              +    ["8","5","9","7","6","1","4","2","3"],
              +    ["4","2","6","8","5","3","7","9","1"],
              +    ["7","1","3","9","2","4","8","5","6"],
              +    ["9","6","1","5","3","7","2","8","4"],
              +    ["2","8","7","4","1","9","6","3","5"],
              +    ["3","4","5","2","8","6","1","7","9"]]
              +解释:输入的数独如上图所示,唯一有效的解决方案如下所示:
              +

               

              +
              + + +

               

              +

              提示:

              +
                +
              • board.length == 9
              • +
              • board[i].length == 9
              • +
              • board[i][j] 是一位数字或者 '.'
              • +
              • 题目数据 保证 输入数独仅有一个解
              • +
              +
              +
              +
              + +## template + +```python +class Solution(object): + def solveSudoku(self, board): + def isvaild(i,j): + for m in range(9): + if m!=i and board[m][j]==board[i][j]: + return False + for n in range(9): + if n!=j and board[i][n]==board[i][j]: + return False + for m in range(i//3*3,i//3*3+3): + for n in range(j//3*3,j//3*3+3): + if m!=i and n!=j and board[m][n]==board[i][j]: + return False + return True + def f(a,b,board): + for i in range(a,9): + for j in range(b,9): + if board[i][j]=='.': + for c in '123456789': + board[i][j]=c + if isvaild(i,j): + if f(a,b,board):return True + else: board[i][j] = '.' + else:board[i][j] = '.' + return False + return True + f(0,0,board) + return board +# %% +s = Solution() +board = [["5","3",".",".","7",".",".",".","."],["6",".",".","1","9","5",".",".","."],[".","9","8",".",".",".",".","6","."],["8",".",".",".","6",".",".",".","3"],["4",".",".","8",".","3",".",".","1"],["7",".",".",".","2",".",".",".","6"],[".","6",".",".",".",".","2","8","."],[".",".",".","4","1","9",".",".","5"],[".",".",".",".","8",".",".","7","9"]] +print(s.solveSudoku(board)) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/3.python/20.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/3.python/20.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..fb331ae5ee8418b0b9834748381cc0962b49805f --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/3.python/20.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-7fb41cd8c4a340c6a2bb716a75f4e960", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/3.python/20.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/3.python/20.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..01fd99e599495aa3fe5a4b9e7df407616822e6d2 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/3.python/20.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "b94e673aee5c4c0284ea5d8c785412c8" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/3.python/20.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/3.python/20.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..4cbb9f3bb5e9864b539f812853ea989f207bb928 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/3.python/20.exercises/solution.md" @@ -0,0 +1,64 @@ +# 通配符匹配 + +

              给定一个字符串 (s) 和一个字符模式 (p) ,实现一个支持 '?' 和 '*' 的通配符匹配。

              '?' 可以匹配任何单个字符。'*' 可以匹配任意字符串(包括空字符串)。

              两个字符串完全匹配才算匹配成功。

              说明:

              • s 可能为空,且只包含从 a-z 的小写字母。
              • p 可能为空,且只包含从 a-z 的小写字母,以及字符 ? 和 *

              示例 1:

              输入:s = "aa"p = "a"
              输出:
              false
              解释:
              "a" 无法匹配 "aa" 整个字符串。

              示例 2:

              输入:s = "aa"p = "*"
              输出:
              true
              解释:
               '*' 可以匹配任意字符串。

              示例 3:

              输入:s = "cb"p = "?a"
              输出:
              false
              解释:
               '?' 可以匹配 'c', 但第二个 'a' 无法匹配 'b'。

              示例 4:

              输入:s = "adceb"p = "*a*b"
              输出:
              true
              解释:
               第一个 '*' 可以匹配空字符串, 第二个 '*' 可以匹配字符串 "dce".

              示例 5:

              输入:s = "acdcb"p = "a*c?b"
              输出:
              false
              + +## template + +```python +class Solution(object): + def isMatch(self, s, p): + """ + :type s: str + :type p: str + :rtype: bool + """ + s_index, p_index = 0, 0 + star, s_star = -1, 0 + s_len, p_len = len(s), len(p) + while s_index < s_len: + if p_index < p_len and (s[s_index] == p[p_index] or p[p_index] == '?'): + s_index += 1 + p_index += 1 + elif p_index < p_len and p[p_index] == '*': + star = p_index + s_star = s_index + p_index += 1 + elif star != -1: + p_index = star + 1 + s_star += 1 + s_index = s_star + else: + return False + while p_index < p_len and p[p_index] == '*': + p_index += 1 + return p_index == p_len +if __name__ == '__main__': + s = Solution() + print (s.isMatch(s = "aa",p = "a")) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/3.python/21.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/3.python/21.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..73ccd2c06b260c5793d38978bfad80f7389a0dde --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/3.python/21.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-0883e73913494bbc9332d28ac2c81dae", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/3.python/21.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/3.python/21.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..a869998af304aca184b6549d7b581e4f98e46d8c --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/3.python/21.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "csdn.net", + "source": "solution.md", + "exercise_id": "cc174c751cea4aba82bdb1b3de87253a" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/3.python/21.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/3.python/21.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..12de7bf83288e9e059ee8d9691fce6f95277f91b --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/3.python/21.exercises/solution.md" @@ -0,0 +1,50 @@ +# 正则表达式匹配 + +

              给你一个字符串 s 和一个字符规律 p,请你来实现一个支持 '.' 和 '*' 的正则表达式匹配。

              • '.' 匹配任意单个字符
              • '*' 匹配零个或多个前面的那一个元素

              所谓匹配,是要涵盖 整个 字符串 s的,而不是部分字符串。

              示例 1:

              输入:s = "aa" p = "a"
              输出:
              false
              解释:
              "a" 无法匹配 "aa" 整个字符串。

              示例 2:

              输入:s = "aa" p = "a*"
              输出:
              true
              解释:
              因为 '*' 代表可以匹配零个或多个前面的那一个元素, 在这里前面的元素就是 'a'。因此,字符串 "aa" 可被视为 'a' 重复了一次。

              示例 3:

              输入:s = "ab" p = ".*"
              输出:
              true
              解释:
              ".*" 表示可匹配零个或多个('*')任意字符('.')。

              示例 4:

              输入:s = "aab" p = "c*a*b"
              输出:
              true
              解释:
              因为 '*' 表示零个或多个,这里 'c' 为 0 个, 'a' 被重复一次。因此可以匹配字符串 "aab"。

              示例 5:

              输入:s = "mississippi" p = "mis*is*p*."
              输出:
              false

               

              提示:

              • 0 <= s.length <= 20
              • 0 <= p.length <= 30
              • s 可能为空,且只包含从 a-z 的小写字母。
              • p 可能为空,且只包含从 a-z 的小写字母,以及字符 . 和 *
              • 保证每次出现字符 * 时,前面都匹配到有效的字符
              + +## template + +```python +class Solution: + def isMatch(self, s: str, p: str) -> bool: + if len(p) == 0: + return len(s) == 0 + head_match = len(s) > 0 and (s[0] == p[0] or p[0] == '.') + if len(p) > 1 and p[1] == '*': + if head_match and self.isMatch(s[1:], p): + return True + return self.isMatch(s, p[2:]) + else: + if not head_match: + return False + return self.isMatch(s[1:], p[1:]) +# %% +s = Solution() +print(s.isMatch(s = "aa" , p = "a")) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/3.python/3.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/3.python/3.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..f0ae2ea194247c4b4f406467d300a4626d4578de --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/3.python/3.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-6ba5e12282af432f9ff3877c00ed8583", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/3.python/3.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/3.python/3.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..ed942be941dfac66040ff613247b5a559049207a --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/3.python/3.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "e7215c7aa6db4ea598fed7a9e6b7572f" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/3.python/3.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/3.python/3.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..cafeb5a2e1b2fc4555a86fbd207fb71728fa50df --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/3.python/3.exercises/solution.md" @@ -0,0 +1,73 @@ +# 接雨水 + +

              给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。

               

              示例 1:

              输入:height = [0,1,0,2,1,0,1,3,2,1,2,1]
              输出:
              6
              解释:
              上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况下,可以接 6 个单位的雨水(蓝色部分表示雨水)。

              示例 2:

              输入:height = [4,2,0,3,2,5]
              输出:
              9

               

              提示:

              • n == height.length
              • 0 <= n <= 3 * 104
              • 0 <= height[i] <= 105
              + +## template + +```python +class Solution(object): + def trap(self, height): + ls = len(height) + if ls == 0: + return 0 + res, left = 0, 0 + while left < ls and height[left] == 0: + left += 1 + pos = left + 1 + while pos < ls: + if height[pos] >= height[left]: + res += self.rain_water(height, left, pos) + left = pos + pos += 1 + elif pos == ls - 1: + max_value, max_index = 0, pos + for index in range(left + 1, ls): + if height[index] > max_value: + max_value = height[index] + max_index = index + res += self.rain_water(height, left, max_index) + left = max_index + pos = left + 1 + else: + pos += 1 + return res + def rain_water(self, height, start, end): + if end - start <= 1: + return 0 + min_m = min(height[start], height[end]) + res = min_m * (end - start - 1) + step = 0 + for index in range(start + 1, end): + if height[index] > 0: + step += height[index] + return res - step +if __name__ == '__main__': + s = Solution() + print (s.trap([2,6,3,8,2,7,2,5,0])) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/3.python/4.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/3.python/4.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..b3fe7a73408331f7d9f7f7d2cfca625ac380f227 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/3.python/4.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-8a87def60cd541e588dd2686d4d670c0", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/3.python/4.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/3.python/4.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..786b49512e2d2bbc366dedbcbc6875bb070c5329 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/3.python/4.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "2304e2d4cff5467caed48f09cf3caa6c" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/3.python/4.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/3.python/4.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..bfa7b23252b4071247c0089f345125669edd2b5f --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/3.python/4.exercises/solution.md" @@ -0,0 +1,58 @@ +# 柱状图中最大的矩形 + +

              给定 n 个非负整数,用来表示柱状图中各个柱子的高度。每个柱子彼此相邻,且宽度为 1 。

              求在该柱状图中,能够勾勒出来的矩形的最大面积。

               

              以上是柱状图的示例,其中每个柱子的宽度为 1,给定的高度为 [2,1,5,6,2,3]

               

              图中阴影部分为所能勾勒出的最大矩形面积,其面积为 10 个单位。

               

              示例:

              输入: [2,1,5,6,2,3]
              输出:
              10
              + +## template + +```python +class Solution(object): + def largestRectangleArea(self, heights): + """ + :type heights: List[int] + :rtype: int + """ + largest_rectangle = 0 + ls = len(heights) + stack = [-1] + top, pos = 0, 0 + for pos in range(ls): + while top > 0 and heights[stack[top]] > heights[pos]: + largest_rectangle = max(largest_rectangle, heights[stack[top]] * (pos - stack[top - 1] - 1)) + top -= 1 + stack.pop() + stack.append(pos) + top += 1 + while top > 0: + largest_rectangle = max(largest_rectangle, heights[stack[top]] * (ls - stack[top - 1] - 1)) + top -= 1 + return largest_rectangle +if __name__ == "__main__": + s = Solution() + print (s.largestRectangleArea([2,1,5,6,2,3])) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/3.python/5.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/3.python/5.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..06600714ea67d630d87f76d20c304953080af865 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/3.python/5.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-935c5d307b5d421cac928efb7bd7c69b", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/3.python/5.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/3.python/5.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..4ea9a587d86859eef8019b5dfb78367fa7a816b7 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/3.python/5.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "c08ab8b52f484ceea093ef0cb3c9730c" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/3.python/5.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/3.python/5.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..aad7a29ff921ee8dfface4a81cf0f58ce08cb93e --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/3.python/5.exercises/solution.md" @@ -0,0 +1,51 @@ +# 缺失的第一个正数 + +

              给你一个未排序的整数数组 nums ,请你找出其中没有出现的最小的正整数。

               

              进阶:你可以实现时间复杂度为 O(n) 并且只使用常数级别额外空间的解决方案吗?

               

              示例 1:

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

              示例 2:

              输入:nums = [3,4,-1,1]
              输出:
              2

              示例 3:

              输入:nums = [7,8,9,11,12]
              输出:
              1

               

              提示:

              • 0 <= nums.length <= 300
              • -231 <= nums[i] <= 231 - 1
              + +## template + +```python +class Solution(object): + def firstMissingPositive(self, nums): + ls = len(nums) + index = 0 + while index < ls: + if nums[index] <= 0 or nums[index] > ls or nums[nums[index] - 1] == nums[index]: + index += 1 + else: + pos = nums[index] - 1 + nums[index], nums[pos] = nums[pos], nums[index] + res = 0 + while res < ls and nums[res] == res + 1: + res += 1 + return res + 1 +# %% +s = Solution() +print(s.firstMissingPositive(nums = [1,2,0])) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/3.python/6.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/3.python/6.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..de6c70738ce823cc6a8d6fffcec44c8288bff009 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/3.python/6.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-0e1e1fce6ef2411aafa9dc4a737875d8", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/3.python/6.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/3.python/6.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..11d8dea5851a21f0b70ad378d759c326aa76493a --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/3.python/6.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "eb6903b01dab4ea69a408634ceeb3b5d" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/3.python/6.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/3.python/6.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..b97907f144a7277c3cc2322365c2b002020422ed --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/3.python/6.exercises/solution.md" @@ -0,0 +1,88 @@ +# N皇后 II + +
              +

              n 皇后问题 研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击。

              + +

              给你一个整数 n ,返回 n 皇后问题 不同的解决方案的数量。

              + +

               

              + +
              +
              +

              示例 1:

              + +
              输入:n = 4
              +输出:2
              +解释:如上图所示,4 皇后问题存在两个不同的解法。
              +    
              + +

              示例 2:

              + +
              输入:n = 1
              +输出:1
              +    
              + +

               

              + +

              提示:

              + +
                +
              • 1 <= n <= 9
              • +
              • 皇后彼此不能相互攻击,也就是说:任何两个皇后都不能处于同一条横行、纵行或斜线上。
              • +
              +
              +
              +
              + +## template + +```python +class Solution(object): + def __init__(self): + self.count = 0 + def totalNQueens(self, n): + self.dfs(0, n, 0, 0, 0) + return self.count + def dfs(self, row, n, column, diag, antiDiag): + if row == n: + self.count += 1 + return + for index in range(n): + isColSafe = (1 << index) & column == 0 + isDigSafe = (1 << (n - 1 + row - index)) & diag == 0 + isAntiDiagSafe = (1 << (row + index)) & antiDiag == 0 + if isAntiDiagSafe and isColSafe and isDigSafe: + self.dfs(row + 1, n, (1 << index) | column, + (1 << (n - 1 + row - index)) | diag, + (1 << (row + index)) | antiDiag) +if __name__ == '__main__': + s = Solution() + print (s.totalNQueens(4)) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/3.python/7.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/3.python/7.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..18ad361900ff6d077d7bde793226ff9f14ab4d5f --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/3.python/7.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-9fd592933a484fa483bd23135870804f", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/3.python/7.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/3.python/7.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..961571d8a19f2f2f505cd5f3f96af6bcd135c119 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/3.python/7.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "5175e82af4484b52bb076a49cde80f49" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/3.python/7.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/3.python/7.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..00dcc4e7190232cf8cb7725018c3eb42eeca51aa --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/3.python/7.exercises/solution.md" @@ -0,0 +1,69 @@ +# 最大矩形 + +

              给定一个仅包含 01 、大小为 rows x cols 的二维二进制矩阵,找出只包含 1 的最大矩形,并返回其面积。

               

              示例 1:

              输入:matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]
              输出:
              6
              解释:
              最大矩形如上图所示。

              示例 2:

              输入:matrix = []
              输出:
              0

              示例 3:

              输入:matrix = [["0"]]
              输出:
              0

              示例 4:

              输入:matrix = [["1"]]
              输出:
              1

              示例 5:

              输入:matrix = [["0","0"]]
              输出:
              0

               

              提示:

              • rows == matrix.length
              • cols == matrix[0].length
              • 0 <= row, cols <= 200
              • matrix[i][j]'0''1'
              + +## template + +```python +class Solution(object): + def maximalRectangle(self, matrix): + """ + :type matrix: List[List[str]] + :rtype: int + """ + if matrix is None or len(matrix) == 0: + return 0 + ls_row, ls_col = len(matrix), len(matrix[0]) + left, right, height = [0] * ls_col, [ls_col] * ls_col, [0] * ls_col + maxA = 0 + for i in range(ls_row): + curr_left, curr_right = 0, ls_col + for j in range(ls_col): + if matrix[i][j] == '1': + height[j] += 1 + else: + height[j] = 0 + for j in range(ls_col): + if matrix[i][j] == '1': + left[j] = max(left[j], curr_left) + else: + left[j], curr_left = 0, j + 1 + for j in range(ls_col - 1, -1, -1): + if matrix[i][j] == '1': + right[j] = min(right[j], curr_right) + else: + right[j], curr_right = ls_col, j + for j in range(ls_col): + maxA = max(maxA, (right[j] - left[j]) * height[j]) + return maxA +# %% +s = Solution() +matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]] +print(s.maximalRectangle(matrix)) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/3.python/8.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/3.python/8.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..81bdf36b6f0bca1e4ba3c5e67494e26031936b17 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/3.python/8.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-1bfb061c537f487ca482fde1a2ea603e", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/3.python/8.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/3.python/8.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..bc9df95ff6701f4e85f12f2f0fda7591388e64df --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/3.python/8.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "550331a822414cfb96a82836ef5ec6cf" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/3.python/8.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/3.python/8.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..71fbbf262ae3333b5525508f01bbe68612641f1f --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/3.python/8.exercises/solution.md" @@ -0,0 +1,53 @@ +# 编辑距离 + +

              给你两个单词 word1 和 word2,请你计算出将 word1 转换成 word2 所使用的最少操作数 。

              你可以对一个单词进行如下三种操作:

              • 插入一个字符
              • 删除一个字符
              • 替换一个字符

               

              示例 1:

              输入:word1 = "horse", word2 = "ros"
              输出:
              3
              解释:
              horse -> rorse (将 'h' 替换为 'r')rorse -> rose (删除 'r')rose -> ros (删除 'e')

              示例 2:

              输入:word1 = "intention", word2 = "execution"
              输出:
              5
              解释:
              intention -> inention (删除 't')inention -> enention (将 'i' 替换为 'e')enention -> exention (将 'n' 替换为 'x')exention -> exection (将 'n' 替换为 'c')exection -> execution (插入 'u')

               

              提示:

              • 0 <= word1.length, word2.length <= 500
              • word1word2 由小写英文字母组成
              + +## template + +```python +class Solution(object): + def minDistance(self, word1, word2): + ls_1, ls_2 = len(word1), len(word2) + dp = list(range(ls_1 + 1)) + for j in range(1, ls_2 + 1): + pre = dp[0] + dp[0] = j + for i in range(1, ls_1 + 1): + temp = dp[i] + if word1[i - 1] == word2[j - 1]: + dp[i] = pre + else: + dp[i] = min(pre + 1, dp[i] + 1, dp[i - 1] + 1) + pre = temp + return dp[ls_1] +if __name__ == '__main__': + s = Solution() + print (s.minDistance("horse","ros")) + print (s.minDistance("intention","execution")) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/3.python/9.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/3.python/9.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..430b820fede71a899c8d0ab603bdc172cfef116f --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/3.python/9.exercises/config.json" @@ -0,0 +1,8 @@ +{ + "node_id": "dailycode-3e9bd22ff8ab4a81b013fe179ac9b1ba", + "keywords": [], + "children": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/3.python/9.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/3.python/9.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..3ebe37fb66b0dd64169840aa28af276d262dc9c9 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/3.python/9.exercises/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "https://github.com/qiyuangong/leetcode", + "source": "solution.md", + "exercise_id": "ba89ac477fbe4b90a077f38bf95c1a9f" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/3.python/9.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/3.python/9.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..3bdbb7f3f47cb5dd67a8094cd7e6a64d61873336 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/3.python/9.exercises/solution.md" @@ -0,0 +1,143 @@ +# 文本左右对齐 + +
              +

              给定一个单词数组和一个长度 maxWidth,重新排版单词,使其成为每行恰好有 maxWidth 个字符,且左右两端对齐的文本。

              + +

              你应该使用“贪心算法”来放置给定的单词;也就是说,尽可能多地往每行中放置单词。必要时可用空格 ' ' 填充,使得每行恰好有 maxWidth 个字符。 +

              + +

              要求尽可能均匀分配单词间的空格数量。如果某一行单词间的空格不能均匀分配,则左侧放置的空格数要多于右侧的空格数。

              + +

              文本的最后一行应为左对齐,且单词之间不插入额外的空格。

              + +

              说明:

              + +
                +
              • 单词是指由非空格字符组成的字符序列。
              • +
              • 每个单词的长度大于 0,小于等于 maxWidth
              • +
              • 输入单词数组 words 至少包含一个单词。
              • +
              + +

              示例:

              + +
              输入:
              +    words = ["This", "is", "an", "example", "of", "text", "justification."]
              +    maxWidth = 16
              +输出:
              +    [
              +       "This    is    an",
              +       "example  of text",
              +       "justification.  "
              +    ]
              +    
              + +

              示例 2:

              + +
              输入:
              +    words = ["What","must","be","acknowledgment","shall","be"]
              +    maxWidth = 16
              +输出:
              +    [
              +      "What   must   be",
              +      "acknowledgment  ",
              +      "shall be        "
              +    ]
              +解释: 注意最后一行的格式应为 "shall be    " 而不是 "shall     be"
              +    因为最后一行应为左对齐,而不是左右两端对齐,第二行同样为左对齐,这是因为这行只包含一个单词。
              +    
              + +

              示例 3:

              + +
              输入:
              +    words = ["Science","is","what","we","understand","well","enough","to","explain",
              +             "to","a","computer.","Art","is","everything","else","we","do"]
              +    maxWidth = 20
              +输出:
              +    [
              +      "Science  is  what we",
              +      "understand      well",
              +      "enough to explain to",
              +      "a  computer.  Art is",
              +      "everything  else  we",
              +      "do                  "
              +    ]
              +    
              +
              + +## template + +```python +class Solution(object): + def fullJustify(self, words, maxWidth): + """ + :type words: List[str] + :type maxWidth: int + :rtype: List[str] + """ + res = [] + res_list = [] + curr = [] + count, pos = 0, 0 + while pos < len(words): + word = words[pos] + if len(word) > maxWidth: + pos += 1 + if len(word) + count + len(curr)<= maxWidth: + count += len(word) + curr.append(word) + pos += 1 + else: + res_list.append(curr) + curr = [] + count = 0 + if len(curr) > 0: + res_list.append(curr) + for index, curr in enumerate(res_list): + text = '' + remain = sum([len(t) for t in curr]) + if len(curr) == 1: + text = curr[0] + ' ' * (maxWidth - remain) + elif index == len(res_list) - 1: + text = ' '.join(curr) + text += ' ' * (maxWidth - remain - len(curr) + 1) + else: + step = (maxWidth - remain) / (len(curr) - 1 ) + extra = (maxWidth - remain) % (len(curr) - 1 ) + for index in range(len(curr) - 1): + text += curr[index] + ' ' * int(step) + if extra > 0: + text += ' ' + extra -= 1 + text += curr[-1] + res.append(text) + return res +if __name__ == '__main__': + s = Solution() + print (s.fullJustify(["Don't","go","around","saying","the","world","owes","you","a","living;","the","world","owes","you","nothing;","it","was","here","first."],30)) +``` + +## 答案 + +```python + +``` + +## 选项 + +### A + +```python + +``` + +### B + +```python + +``` + +### C + +```python + +``` \ No newline at end of file diff --git a/helper.py b/helper.py index 7d4dc8c9ffb220b06714fcc462da047f925cc44f..ba89d917993f7b24df0a99868b9e42a13e1da293 100644 --- a/helper.py +++ b/helper.py @@ -185,24 +185,22 @@ def classify_leetcode(): else: language_dir = '' sys.exit("语言类型异常") - dst_dir = os.path.join(root_dir, language_dir) - dir_list_ = os.listdir(dst_dir) + current_dst_dir = os.path.join(root_dir, language_dir) + print(current_dst_dir) + dir_list_ = os.listdir(current_dst_dir) dir_list = [] for i in dir_list_: - if os.path.isdir(i): - dir_list.append(i) + current_dst_dir_ = os.path.join(current_dst_dir, i) + if os.path.isdir(current_dst_dir_): + dir_list.append(current_dst_dir_) number = len(dir_list) + 1 - dst_dir = os.path.join(dst_dir, str(number) + '.exercises') + dst_dir = os.path.join(current_dst_dir, str(number) + '.exercises') + solution_json_path = os.path.join(dst_dir, 'solution.json') solution_md_path = os.path.join(dst_dir, 'solution.md') config_path = os.path.join(dst_dir, 'config.json') - print(dst_dir) - print(solution_json_path) - print(config_path) - print(solution_json_data) - print(solution_md_data) if not os.path.exists(dst_dir): os.mkdir(dst_dir) dump_json(solution_json_path, solution_json_data) @@ -227,7 +225,7 @@ def rename_dir(): # os.rename(exercises_dir, new_exercises_dir) -classify_exercises() +classify_leetcode()