From c1efa19bc75a60d33cf5e67fe34610251c9d186f Mon Sep 17 00:00:00 2001 From: qq_44193969 <2608882093@qq.com> Date: Fri, 31 Dec 2021 10:09:35 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A020=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 +- .../1.cpp/26.exercises/solution.md" | 101 +++++++++++- .../1.cpp/27.exercises/solution.md" | 58 ++++++- .../1.cpp/28.exercises/solution.md" | 155 +++++++++++++++++- .../1.cpp/29.exercises/solution.md" | 73 ++++++++- .../1.cpp/30.exercises/solution.md" | 68 +++++++- .../1.cpp/31.exercises/solution.md" | 99 ++++++++++- .../1.cpp/32.exercises/solution.md" | 86 +++++++++- .../1.cpp/33.exercises/solution.md" | 84 +++++++++- .../1.cpp/34.exercises/solution.md" | 59 ++++++- .../1.cpp/35.exercises/solution.md" | 48 +++++- .../1.cpp/36.exercises/solution.md" | 59 ++++++- .../1.cpp/37.exercises/solution.md" | 77 ++++++++- .../1.cpp/38.exercises/solution.md" | 81 ++++++++- .../1.cpp/39.exercises/solution.md" | 80 ++++++++- .../1.cpp/40.exercises/solution.md" | 27 ++- .../2.java/1.exercises/solution.md" | 73 ++++++++- .../2.java/10.exercises/solution.md" | 61 ++++++- .../2.java/11.exercises/solution.md" | 58 ++++++- .../2.java/12.exercises/solution.md" | 74 ++++++++- .../2.java/13.exercises/solution.md" | 145 +++++++++++++++- .../2.java/14.exercises/solution.md" | 61 ++++++- .../2.java/15.exercises/solution.md" | 56 ++++++- .../2.java/16.exercises/solution.md" | 65 +++++++- .../2.java/17.exercises/solution.md" | 77 ++++++++- .../2.java/18.exercises/solution.md" | 46 +++++- .../2.java/19.exercises/solution.md" | 41 ++++- .../2.java/2.exercises/solution.md" | 47 +++++- .../2.java/20.exercises/solution.md" | 37 ++++- .../2.java/3.exercises/solution.md" | 44 ++++- .../2.java/4.exercises/solution.md" | 49 +++++- .../2.java/5.exercises/solution.md" | 60 ++++++- .../2.java/6.exercises/solution.md" | 61 ++++++- .../2.java/7.exercises/solution.md" | 70 +++++++- .../2.java/8.exercises/solution.md" | 67 +++++++- .../2.java/9.exercises/solution.md" | 53 +++++- 36 files changed, 2262 insertions(+), 141 deletions(-) diff --git a/.gitignore b/.gitignore index bee05a179..e534948d5 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,5 @@ helper.py test.md data_source/dailycode test.cpp -test.py \ No newline at end of file +test.py +test.java \ 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" index e5570506a..c59fc9115 100644 --- "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" @@ -2,6 +2,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

 

提示:

+以下程序实现了这一功能,请你填补空白处的内容: + +```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--) + { + _________________________; + } + 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; +} +``` + ## template ```cpp @@ -83,7 +156,12 @@ int main(int argc, char **argv) ## 答案 ```cpp - +while (pos > 0 && heights[indexes[pos - 1]] >= heights[i]) +{ + pos--; +} +rhist[i] = pos == 0 ? size : indexes[pos - 1]; +indexes[pos++] = i; ``` ## 选项 @@ -91,17 +169,32 @@ int main(int argc, char **argv) ### A ```cpp - +while (pos > 0 && heights[indexes[pos - 1]] >= heights[i]) +{ + pos--; +} +rhist[i] = pos == 0 ? indexes[pos - 1] : size; +indexes[pos++] = i; ``` ### B ```cpp - +while (pos > 0 && heights[indexes[pos - 1]] >= heights[i]) +{ + pos--; +} +rhist[i] = pos == 0 ? size : indexes[pos + 1]; +indexes[pos++] = i; ``` ### C ```cpp - +while (pos > 0 && heights[indexes[pos - 1]] >= heights[i]) +{ + pos++; +} +rhist[i] = pos == 0 ? size : indexes[pos - 1]; +indexes[pos++] = i; ``` \ 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" index d5b70a04f..a9b8b5360 100644 --- "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" @@ -2,6 +2,44 @@

给你两个单词 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 由小写英文字母组成
+以下程序实现了这一功能,请你填补空白处的内容: + +```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]; + _________________________; + else + { + dp[j] = 1 + min(left_up, min(up, dp[j - 1])); + } + left_up = up; + } + } + return dp[l2]; + } +}; +``` + ## template ```cpp @@ -46,7 +84,10 @@ public: ## 答案 ```cpp - +if (word1[i - 1] == word2[j - 1]) +{ + dp[j] = left_up; +} ``` ## 选项 @@ -54,17 +95,26 @@ public: ### A ```cpp - +if (word1[i + 1] == word2[j + 1]) +{ + dp[j] = left_up; +} ``` ### B ```cpp - +if (word1[i] == word2[j]) +{ + dp[j] = left_up; +} ``` ### C ```cpp - +if (word1[i - 1] == word2[j - 1]) +{ + dp[j] = left_up + 1; +} ``` \ 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" index 6de7cc796..f4ea9f2e9 100644 --- "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" @@ -64,6 +64,121 @@ +以下程序实现了这一功能,请你填补空白处的内容: + +```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) + { + ___________________; + } + } + 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; +} +``` + ## template ```cpp @@ -190,7 +305,15 @@ int main(int argc, char **argv) ## 答案 ```cpp - +for (j = 0; j < even_spaces; j++) +{ + *p++ = ' '; +} +if (remain_spaces > 0) +{ + *p++ = ' '; + remain_spaces--; +} ``` ## 选项 @@ -198,17 +321,41 @@ int main(int argc, char **argv) ### A ```cpp - +for (j = 0; j < even_spaces; j++) +{ + *p++ = ' '; +} +if (remain_spaces > 0) +{ + *p++ = ' '; + remain_spaces++; +} ``` ### B ```cpp - +for (j = 0; j < even_spaces; j++) +{ + *p++ = ' '; +} +if (remain_spaces < 0) +{ + *p++ = ' '; + remain_spaces++; +} ``` ### C ```cpp - +for (j = 0; j < even_spaces; j++) +{ + *p++ = '\0'; +} +if (remain_spaces > 0) +{ + *p++ = '\0'; + remain_spaces--; +} ``` \ 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" index a980273e7..b44f12f00 100644 --- "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" @@ -2,6 +2,43 @@

给你一个只包含 '(' 和 ')' 的字符串,找出最长有效(格式正确且连续)括号子串的长度。

 

示例 1:

输入:s = "(()"
输出:
2
解释:
最长有效括号子串是 "()"

示例 2:

输入:s = ")()())"
输出:
4
解释:
最长有效括号子串是 "()()"

示例 3:

输入:s = ""
输出:
0

 

提示:

  • 0 <= s.length <= 3 * 104
  • s[i]'('')'
+以下程序实现了这一功能,请你填补空白处的内容: + +```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(); + ___________________; + } + } + } + return max_len; + } +}; +``` + ## template ```cpp @@ -49,7 +86,14 @@ public: ## 答案 ```cpp - +if (stk.empty()) +{ + max_len = max(i - invalid, max_len); +} +else +{ + max_len = max(i - stk.top(), max_len); +} ``` ## 选项 @@ -57,17 +101,38 @@ public: ### A ```cpp - +if (stk.empty()) +{ + max_len = max(i - invalid, max_len); +} +else +{ + max_len = max(stk.top(), max_len); +} ``` ### B ```cpp - +if (stk.empty()) +{ + max_len = max(invalid, max_len); +} +else +{ + max_len = max(i - stk.top(), max_len); +} ``` ### C ```cpp - +if (stk.empty()) +{ + max_len = max(invalid - i, max_len); +} +else +{ + max_len = max(i - stk.top(), max_len); +} ``` \ 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" index aae16c1e3..5748f4a9f 100644 --- "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" @@ -2,6 +2,54 @@

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
  • 皇后彼此不能相互攻击,也就是说:任何两个皇后都不能处于同一条横行、纵行或斜线上。
+以下程序实现了这一功能,请你填补空白处的内容: + +```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)) + { + __________________; + } + } + } + } + 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; + } +} +``` + ## template ```cpp @@ -56,7 +104,10 @@ private: ## 答案 ```cpp - +solution[row][i] = 'Q'; +stack[row] = i; +dfs(n, row + 1, stack, solution, res); +solution[row][i] = '.'; ``` ## 选项 @@ -64,17 +115,26 @@ private: ### A ```cpp - +solution[row][i] = 'Q'; +stack[row] = i; +dfs(n, row, stack, solution, res); +solution[row][i] = '.'; ``` ### B ```cpp - +solution[row][i] = '.'; +stack[row] = i; +dfs(n, row + 1, stack, solution, res); +solution[row][i] = 'Q'; ``` ### C ```cpp - +solution[row][i] = '.'; +stack[row] = i; +dfs(n, row, stack, solution, res); +solution[row][i] = 'Q'; ``` \ 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" index f304bde01..36081f9ea 100644 --- "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" @@ -60,6 +60,65 @@ +以下程序实现了这一功能,请你填补空白处的内容: + +```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++) + { + _________________________; + } + 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; +} +``` + ## template ```cpp @@ -131,7 +190,16 @@ int main(int argc, char **argv) ## 答案 ```cpp - +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; +} ``` ## 选项 @@ -139,17 +207,40 @@ int main(int argc, char **argv) ### A ```cpp - +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; +} ``` ### B ```cpp - +if (scramble(s1, low1, low1 + i - 1, s2, low2, low2 + i - 1) +{ + return true; +} +if (scramble(s1, low1 + i, high1, s2, low2, high2 - i)) +{ + return true; +} ``` ### C ```cpp - +if (scramble(s1, low1 + i, high1, s2, low2 + i, high2)) +{ + return true; +} +if (scramble(s1, low1 + i, high1, s2, low2, high2 - i)) +{ + return true; +} ``` \ 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" index 383310077..445edc9a6 100644 --- "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" @@ -2,6 +2,36 @@

给你一个链表,每 个节点一组进行翻转,请你返回翻转后的链表。

是一个正整数,它的值小于或等于链表的长度。

如果节点总数不是 的整数倍,那么请将最后剩余的节点保持原有顺序。

进阶:

  • 你可以设计一个只使用常数额外空间的算法来解决此问题吗?
  • 你不能只是单纯的改变节点内部的值,而是需要实际进行节点交换。

 

示例 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
    +以下程序实现了这一功能,请你填补空白处的内容: + +```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) + { + ____________________; + } + return dummy.next; + } +}; +``` + ## template ```cpp @@ -47,7 +77,19 @@ public: ## 答案 ```cpp - +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; +} ``` ## 选项 @@ -55,17 +97,53 @@ public: ### A ```cpp - +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; +} ``` ### B ```cpp - +if (++len % k == 0) +{ + struct ListNode *p = prev->next; + while (prev->next != head) + { + struct ListNode *q = p->next; + prev->next = q; + q->next = prev->next; + p->next = q->next; + } + prev = p; + head = p; +} ``` ### C ```cpp - +if (++len % k == 0) +{ + struct ListNode *p = prev->next; + while (prev->next != head) + { + struct ListNode *q = p->next; + prev->next = q; + p->next = q->next; + q->next = prev->next; + } + prev = p; + head = p; +} ``` \ 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" index c832568bd..25f5f5127 100644 --- "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" @@ -2,6 +2,52 @@

    有效数字(按顺序)可以分成以下几个部分:

    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),加号 '+' ,减号 '-' ,或者点 '.'
    +以下程序实现了这一功能,请你填补空白处的内容: + +```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') + { + ___________________; + } + 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; +} +``` + ## template ```cpp @@ -59,7 +105,15 @@ int main(int argc, char **argv) ## 答案 ```cpp - +++s; +if (*s == '+' || *s == '-') + ++s; +if_find_num = false; +while (isdigit(*s)) +{ + if_find_num = true; + ++s; +} ``` ## 选项 @@ -67,17 +121,39 @@ int main(int argc, char **argv) ### A ```cpp - +if (*s == '+' || *s == '-') + ++s; +if_find_num = false; +while (isdigit(*s)) +{ + if_find_num = true; + ++s; +} ``` ### B ```cpp - +if (*s == '+' || *s == '-') + ++s; +if_find_num = true; +while (isdigit(*s)) +{ + if_find_num = false; + ++s; +} ``` ### C ```cpp - +++s; +if (*s == '+' || *s == '-') + ++s; +if_find_num = true; +while (isdigit(*s)) +{ + if_find_num = false; + ++s; +} ``` \ 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" index 9b56524bc..d5aeb0dcc 100644 --- "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" @@ -9,6 +9,45 @@

    示例 2:

    输入:  s = "wordgoodgoodgoodbestword",  words = ["word","good","best","word"]
    输出:
    []
    +以下程序实现了这一功能,请你填补空白处的内容: + +```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 (j == words.size()) + { + res.push_back(i); + } + } + return res; + } +}; +``` + ## template ```cpp @@ -54,7 +93,10 @@ public: ## 答案 ```cpp - +if (++counting[word] > ht[word]) +{ + break; +} ``` ## 选项 @@ -62,17 +104,26 @@ public: ### A ```cpp - +if (++counting[word] < ht[word]) +{ + break; +} ``` ### B ```cpp - +if (++counting[word] <= ht[word]) +{ + continue; +} ``` ### C ```cpp - +if (++counting[word] <= ht[word]) +{ + break; +} ``` \ 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" index 4a39a47bd..4888ecc42 100644 --- "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" @@ -2,6 +2,46 @@

    给定两个大小分别为 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)) 的算法解决此问题吗?

    +以下程序实现了这一功能,请你填补空白处的内容: + +```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) + { + ________________________; + } + 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; + } +}; +``` + ## template ```cpp @@ -45,7 +85,7 @@ public: ## 答案 ```cpp - +n = (nums1[i] < nums2[j]) ? nums1[i++] : nums2[j++]; ``` ## 选项 @@ -53,17 +93,17 @@ public: ### A ```cpp - +n = (nums1[i] < nums2[j]) ? nums1[j++] : nums2[i++]; ``` ### B ```cpp - +n = (nums1[i] > nums2[j]) ? nums1[i++] : nums2[j++]; ``` ### C ```cpp - +n = (nums1[i] >= nums2[j]) ? nums1[i++] : nums2[j++]; ``` \ 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" index ac79f74a6..e9da8e590 100644 --- "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" @@ -2,6 +2,45 @@

    给你一个字符串 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) 时间内解决此问题的算法吗? +以下程序实现了这一功能,请你填补空白处的内容: + +```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; + } + ___________________; + } + } + return min_len == INT_MAX ? "" : s.substr(start, min_len); + } +}; +``` + ## template ```cpp @@ -47,7 +86,10 @@ public: ## 答案 ```cpp - +if (++count[s[l++]] > 0) +{ + need_to_meet++; +} ``` ## 选项 @@ -55,17 +97,26 @@ public: ### A ```cpp - +if (count[s[l++]] > 0) +{ + need_to_meet++; +} ``` ### B ```cpp - +if (count[s[l++]] >= 0) +{ + need_to_meet++; +} ``` ### C ```cpp - +if (++count[s[l++]++] > 0) +{ + need_to_meet++; +} ``` \ 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" index 5ea8efca9..66001e7ec 100644 --- "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" @@ -2,6 +2,47 @@

    给你一个链表数组,每个链表都已经按升序排列。

    请你将所有链表合并到一个升序链表中,返回合并后的链表。

     

    示例 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
    +以下程序实现了这一功能,请你填补空白处的内容: + +```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()) + { + _________________; + } + return dummy.next; + } +}; +``` + ## template ```cpp @@ -53,7 +94,14 @@ public: ## 答案 ```cpp - +ListNode *node = queue.top(); +queue.pop(); +p->next = node; +p = node; +if (node->next != nullptr) +{ + queue.push(node->next); +} ``` ## 选项 @@ -61,17 +109,38 @@ public: ### A ```cpp - +ListNode *node = queue.top(); +queue.pop(); +p->next = node; +p = node; +if (node->next != nullptr) +{ + queue.pop(node->next); +} ``` ### B ```cpp - +ListNode *node = queue.top(); +queue.pop(); +p = node; +p->next = node; +if (node->next != nullptr) +{ + queue.push(node->next); +} ``` ### C ```cpp - +ListNode *node = queue.top(); +queue.pop(); +p = node; +p->next = node; +if (node->next != nullptr) +{ + queue.pop(node->next); +} ``` \ 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" index 1f6e0d5c5..efc2055e0 100644 --- "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" @@ -2,6 +2,51 @@

    给出集合 [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!
    +以下程序实现了这一功能,请你填补空白处的内容: + +```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; + ____________________; + 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; +} +``` + ## template ```cpp @@ -57,7 +102,14 @@ int main(int argc, char **argv) ## 答案 ```cpp - +while (count <= gid) +{ + x = (x + 1) % n; + if (!used[x]) + { + count++; + } +} ``` ## 选项 @@ -65,17 +117,38 @@ int main(int argc, char **argv) ### A ```cpp - +while (count <= gid) +{ + x = (x + 1) % n; + if (used[x]) + { + count++; + } +} ``` ### B ```cpp - +while (count < gid) +{ + x = (x + 1) % n; + if (used[x]) + { + count++; + } +} ``` ### C ```cpp - +while (count <= gid) +{ + x = x % n; + if (used[x]) + { + count++; + } +} ``` \ 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" index 947907ffd..65d0fa842 100644 --- "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" @@ -2,6 +2,52 @@

    给定一个字符串 (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
    +以下程序实现了这一功能,请你填补空白处的内容: + +```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 + { + 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; +} +``` + ## template ```cpp @@ -60,7 +106,16 @@ int main(int argc, char **argv) ## 答案 ```cpp - +else if (*p == '?' || *s == *p) +{ + s++; + p++; +} +else if (last_s != NULL) +{ + p = last_p; + s = ++last_s; +} ``` ## 选项 @@ -68,17 +123,34 @@ int main(int argc, char **argv) ### A ```cpp - +else if (last_s != NULL) +{ + p = last_p; + s = ++last_s; +} ``` ### B ```cpp - +else if (*p == '?' || *s == *p) +{ + s++; + p++; +} ``` ### C ```cpp - +else if (*p == '?' && *s == *p) +{ + s++; + p++; +} +else if (last_s != NULL) +{ + p = last_p; + s = ++last_s; +} ``` \ 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" index b10bdcf16..a129c19f5 100644 --- "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" @@ -2,6 +2,25 @@

    给你一个字符串 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 的小写字母,以及字符 . 和 *
    • 保证每次出现字符 * 时,前面都匹配到有效的字符
    +以下程序实现了这一功能,请你填补空白处的内容: + +```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] == '*') + { + _____________________; + } + else + { + return head_match && isMatch(s + 1, p + 1); + } +} +``` + ## template ```cpp @@ -24,7 +43,7 @@ bool isMatch(char *s, char *p) ## 答案 ```cpp - +return (head_match && isMatch(s + 1, p)) || isMatch(s, p + 2); ``` ## 选项 @@ -32,17 +51,17 @@ bool isMatch(char *s, char *p) ### A ```cpp - +return (head_match && isMatch(s + 1, p)) && isMatch(s, p + 2); ``` ### B ```cpp - +return (head_match || isMatch(s + 1, p)) || isMatch(s, p + 2); ``` ### C ```cpp - +return (head_match || isMatch(s + 1, p)) && isMatch(s, p + 2); ``` \ 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" index a28cda881..412bbd577 100644 --- "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" @@ -52,6 +52,67 @@ +以下程序实现了这一功能,请你填补空白处内容: + +```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; + + ____________________; + + 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; + } +} + +``` + + ## template ```java @@ -116,7 +177,8 @@ class Solution { ## 答案 ```java - +if (board[x][y] != '.') + return dfs(board, x, y + 1); ``` ## 选项 @@ -124,17 +186,20 @@ class Solution { ### A ```java - +if (board[x][y] != '.') + return dfs(board, x, y); ``` ### B ```java - +if (board[x][y] == '.') + return dfs(board, x, y); ``` ### C ```java - +if (board[x][y] == '.') + return dfs(board, x, y + 1); ``` \ 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" index ba7c713f6..3f00ef107 100644 --- "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" @@ -2,6 +2,59 @@

    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
    • 皇后彼此不能相互攻击,也就是说:任何两个皇后都不能处于同一条横行、纵行或斜线上。
    +以下程序实现了这一功能,请你填补空白处内容: + +```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; + ________________________; + } + } + } + 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; + } +} +``` + ## template ```java @@ -58,7 +111,7 @@ public class Solution { ## 答案 ```java - +placeQueen(queenList, row + 1, n, res); ``` ## 选项 @@ -66,17 +119,17 @@ public class Solution { ### A ```java - +placeQueen(queenList, row, n, res); ``` ### B ```java - +placeQueen(queenList, row + 1, n + 1, res); ``` ### C ```java - +placeQueen(queenList, row, n + 1, res); ``` \ 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" index 4a5f27cdf..dd24dddf3 100644 --- "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" @@ -60,6 +60,44 @@ +以下程序实现了这一功能,请你填补空白处内容: + +```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++) { + ____________________; + } + 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; + } +} +``` + ## template ```java @@ -104,7 +142,10 @@ class Solution { ## 答案 ```java - +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; ``` ## 选项 @@ -112,17 +153,26 @@ class Solution { ### A ```java - +if (dfs(s1, s2, start1, start2, i)) + return true; +if (dfs(s1, s2, start1, start2 + len - i, i)) + return true; ``` ### B ```java - +if (dfs(s1, s2, start1 + i, start2 + i, len - i)) + return true; +if (dfs(s1, s2, start1 + i, start2, len - i)) + return true; ``` ### C ```java - +if (dfs(s1, s2, start1 + i, start2 + i, len - i)) + return true; +if (dfs(s1, s2, start1, start2 + len - i, i)) + return true; ``` \ 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" index 298d54b3f..9295f87d9 100644 --- "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" @@ -2,6 +2,60 @@

    给你一个链表,每 个节点一组进行翻转,请你返回翻转后的链表。

    是一个正整数,它的值小于或等于链表的长度。

    如果节点总数不是 的整数倍,那么请将最后剩余的节点保持原有顺序。

    进阶:

    • 你可以设计一个只使用常数额外空间的算法来解决此问题吗?
    • 你不能只是单纯的改变节点内部的值,而是需要实际进行节点交换。

     

    示例 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
      +以下程序实现了这一功能,请你填补空白处内容: + +```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) { + __________________; + } + return pre; + } +} +``` + ## template ```java @@ -62,7 +116,10 @@ class Solution { ## 答案 ```java - +nxt = cur.next; +cur.next = pre; +pre = cur; +cur = nxt; ``` ## 选项 @@ -70,17 +127,26 @@ class Solution { ### A ```java - +cur.next = pre; +nxt = cur.next; +pre = cur; +cur = nxt; ``` ### B ```java - +cur.next = pre; +nxt = cur.next; +cur = nxt; +pre = cur; ``` ### C ```java - +nxt = cur.next; +cur.next = pre; +cur = nxt; +pre = cur; ``` \ 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" index e1b3cf519..da17ffea5 100644 --- "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" @@ -2,6 +2,79 @@

      有效数字(按顺序)可以分成以下几个部分:

      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),加号 '+' ,减号 '-' ,或者点 '.'
      +以下程序实现了这一功能,请你填补空白处内容: + +```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 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; + } +} +``` + ## template ```java @@ -94,7 +167,23 @@ class Solution { ## 答案 ```java - +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; +} ``` ## 选项 @@ -102,17 +191,65 @@ class Solution { ### A ```java - +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 true; + } + } + return false; +} ``` ### B ```java - +public boolean pre(int i, int length) { + if (i >= length) { + return false; + } + if (chars[i] == '+' || chars[i] == '-') { + i++; + } + if (i == length && !point) { + return true; + } + for (; i < length; i++) { + if (chars[i] < '0' || chars[i] > '9') { + return true; + } + } + return false; +} ``` ### C ```java - +public boolean pre(int i, int length) { + if (i >= length) { + return false; + } + if (chars[i] == '+' || chars[i] == '-') { + i++; + } + if (i == length && !point) { + return true; + } + for (; i < length; i++) { + if (chars[i] < '0' || chars[i] > '9') { + return false; + } + } + return true; +} ``` \ 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" index ec7486c3c..e69f2dbee 100644 --- "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" @@ -9,6 +9,47 @@

      示例 2:

      输入:  s = "wordgoodgoodgoodbestword",  words = ["word","good","best","word"]
      输出:
      []
      +以下程序实现了这一功能,请你填补空白处内容: + +```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)) { + ______________________; + } + if (count == word_num) + res.add(left); + } + } + } + return res; + } +} +``` + ## template ```java @@ -56,7 +97,10 @@ class Solution { ## 答案 ```java - +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; ``` ## 选项 @@ -64,17 +108,26 @@ class Solution { ### A ```java - +String t_w = s.substring(left, left + one_word); +count--; +tmp_map.put(t_w, tmp_map.getOrDefault(t_w, 0)); +left += one_word; ``` ### B ```java - +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; ``` ### C ```java - +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; ``` \ 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" index 99233bb0b..d5f527678 100644 --- "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" @@ -2,6 +2,30 @@

      给定两个大小分别为 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)) 的算法解决此问题吗?

      +以下程序实现了这一功能,请你填补空白处内容: + +```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; + _________________; + ns[d++] = n; + } + if (na % 2 == 1) { + return ns[d - 1]; + } + return (ns[d - 1] + ns[d - 2]) / 2.0; + } +} +``` + ## template ```java @@ -35,7 +59,13 @@ class Solution { ## 答案 ```java - +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++]; +} ``` ## 选项 @@ -43,17 +73,35 @@ class Solution { ### A ```java - +if (i < nums1Size && j < nums2Size) { + n = (nums1[j] < nums2[i]) ? nums2[i++] : nums1[j++]; +} else if (i < nums1Size) { + n = nums1[j++]; +} else if (j < nums2Size) { + n = nums2[i++]; +} ``` ### B ```java - +if (i < nums1Size && j < nums2Size) { + n = (nums1[j] > nums2[i]) ? nums2[i++] : nums1[j++]; +} else if (i > nums1Size) { + n = nums1[j++]; +} else if (j > nums2Size) { + n = nums2[i++]; +} ``` ### C ```java - +if (i < nums1Size && j > nums2Size) { + n = (nums1[j] < nums2[i]) ? nums2[i++] : nums1[j++]; +} else if (i < nums1Size) { + n = nums1[j++]; +} else if (j < nums2Size) { + n = nums2[i++]; +} ``` \ 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" index 1a81f546b..0261e52a8 100644 --- "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" @@ -2,6 +2,43 @@

      给你一个字符串 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) 时间内解决此问题的算法吗? +以下程序实现了这一功能,请你填补空白处内容: + +```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()) { + _________________; + if (end - start + 1 < min) { + minwin = s.substring(start, end + 1); + min = end - start + 1; + } + } + end++; + } + return minwin; + } + +} +``` + ## template ```java @@ -47,7 +84,12 @@ public class Min_Win_Sub { ## 答案 ```java - +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++; +} ``` ## 选项 @@ -55,17 +97,32 @@ public class Min_Win_Sub { ### A ```java - +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++; +} ``` ### B ```java - +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++; +} ``` ### C ```java - +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++; +} ``` \ 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" index 2204bc595..1256e2b69 100644 --- "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" @@ -2,6 +2,71 @@

      给你一个链表数组,每个链表都已经按升序排列。

      请你将所有链表合并到一个升序链表中,返回合并后的链表。

       

      示例 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
      +以下程序实现了这一功能,请你填补空白处内容: + +```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; + _____________________________; + 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; + } +} + +``` + ## template ```java @@ -71,7 +136,8 @@ class Solution { ## 答案 ```java - +ListNode tmp1 = merge(lists, low, mid); +ListNode tmp2 = merge(lists, mid + 1, high); ``` ## 选项 @@ -79,17 +145,20 @@ class Solution { ### A ```java - +ListNode tmp1 = merge(lists, low, mid); +ListNode tmp2 = merge(lists, mid, high); ``` ### B ```java - +ListNode tmp1 = merge(lists, low, mid + 1); +ListNode tmp2 = merge(lists, mid + 1, high); ``` ### C ```java - +ListNode tmp1 = merge(lists, low, mid + 1); +ListNode tmp2 = merge(lists, mid - 1, high); ``` \ 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" index 13a1e6b6f..0960fb9a4 100644 --- "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" @@ -2,6 +2,28 @@

      给出集合 [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!
      +以下程序实现了这一功能,请你填补空白处内容: + +```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; + ____________________; + return sb.toString(); + } +} +``` + ## template ```java @@ -31,7 +53,11 @@ class Solution { ## 答案 ```java - +for (int i = n - 1; i >= 0; --i) { + int index = k / factorials[i]; + sb.append(candidates.remove(index)); + k -= index * factorials[i]; +} ``` ## 选项 @@ -39,17 +65,29 @@ class Solution { ### A ```java - +for (int i = n - 1; i >= 0; --i) { + int index = k / factorials[i]; + sb.append(candidates.remove(index)); + k = index * factorials[i]; +} ``` ### B ```java - +for (int i = n - 1; i >= 0; --i) { + int index = k / factorials[i]; + sb.append(candidates.remove(index)); + k += index * factorials[i]; +} ``` ### C ```java - +for (int i = n; i >= 0; --i) { + int index = k / factorials[i - 1]; + sb.append(candidates.remove(index)); + k -= index * factorials[i]; +} ``` \ 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" index a3d715f1d..eef6bde9e 100644 --- "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" @@ -2,6 +2,39 @@

      给定一个字符串 (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
      +以下程序实现了这一功能,请你填补空白处内容: + +```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++) { + ____________________; + } + } + } + return value[p.length()][s.length()]; + } +} +``` + ## template ```java @@ -38,7 +71,7 @@ class Solution { ## 答案 ```java - +value[i][j] = s.charAt(j - 1) == p.charAt(i - 1) && value[i - 1][j - 1]; ``` ## 选项 @@ -46,17 +79,17 @@ class Solution { ### A ```java - +value[i][j] = s.charAt(j - 1) == p.charAt(i - 1) || value[i - 1][j - 1]; ``` ### B ```java - +value[i][j] = s.charAt(j - 1) == p.charAt(i) && value[i - 1][j]; ``` ### C ```java - +value[i][j] = s.charAt(j - 1) == p.charAt(i - 1) && value[i][j - 1]; ``` \ 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" index a2c7645ea..4b3ada41e 100644 --- "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" @@ -2,6 +2,37 @@

      给定 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
      +以下程序实现了这一功能,请你填补空白处内容: + +```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 = 1; i < len - 1; i++) { + res += Math.min(left_max[i], right_max[i]) - height[i]; + } + return res; + } +} + +``` + ## template ```java @@ -38,7 +69,9 @@ class Solution { ## 答案 ```java - +for (int i = len - 2; i >= 0; i--) { + right_max[i] = Math.max(height[i], right_max[i + 1]); +} ``` ## 选项 @@ -46,17 +79,23 @@ class Solution { ### A ```java - +for (int i = len - 2; i >= 0; i--) { + right_max[i] = Math.max(height[i], right_max[i]); +} ``` ### B ```java - +for (int i = len; i >= 0; i--) { + right_max[i] = Math.max(height[i], right_max[i]); +} ``` ### C ```java - +for (int i = len; i >= 0; i--) { + right_max[i] = Math.max(height[i], right_max[i + 1]); +} ``` \ 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" index c60dc3787..c695a50ac 100644 --- "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" @@ -2,6 +2,19 @@

      给你一个字符串 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 的小写字母,以及字符 . 和 *
      • 保证每次出现字符 * 时,前面都匹配到有效的字符
      +以下程序实现了这一功能,请你填补空白处内容: + +```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) == '.'); + ________________; + } +} +``` + ## template ```java @@ -22,7 +35,11 @@ class Solution { ## 答案 ```java - +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)); +} ``` ## 选项 @@ -30,17 +47,29 @@ class Solution { ### A ```java - +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(2)); +} ``` ### B ```java - +if (p.length() > 1 && p.charAt(1) == '*') { + return (head_match && isMatch(s.substring(1), p)) || isMatch(s, p.substring(2)); +} else { + return isMatch(s.substring(1), p.substring(2)); +} ``` ### C ```java - +if (p.length() > 1 && p.charAt(1) == '*') { + return isMatch(s.substring(1), p)) || isMatch(s, p.substring(2); +} else { + return isMatch(s.substring(1), p.substring(2)); +} ``` \ 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" index 6f7e1e4da..763c64572 100644 --- "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" @@ -2,6 +2,34 @@

      给定 n 个非负整数,用来表示柱状图中各个柱子的高度。每个柱子彼此相邻,且宽度为 1 。

      求在该柱状图中,能够勾勒出来的矩形的最大面积。

       

      以上是柱状图的示例,其中每个柱子的宽度为 1,给定的高度为 [2,1,5,6,2,3]

       

      图中阴影部分为所能勾勒出的最大矩形面积,其面积为 10 个单位。

       

      示例:

      输入: [2,1,5,6,2,3]
      输出:
      10
      +以下程序实现了这一功能,请你填补空白处内容: + +```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++) { + ___________________; + nowWidth++; + if (maxSize < nowHeight * nowWidth) { + maxSize = nowHeight * nowWidth; + } + } + + } + return maxSize; + } +} + +``` + ## template ```java @@ -35,7 +63,9 @@ class Solution { ## 答案 ```java - +if (heights[j] < nowHeight) { + nowHeight = heights[j]; +} ``` ## 选项 @@ -43,17 +73,23 @@ class Solution { ### A ```java - +if (heights[j] > nowHeight) { + nowHeight = heights[j]; +} ``` ### B ```java - +if (heights[j] >= nowHeight) { + nowHeight = heights[j] + 1; +} ``` ### C ```java - +if (heights[j] < nowHeight) { + nowHeight = heights[j] + 1; +} ``` \ 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" index 6eef76dd7..a491f21b0 100644 --- "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" @@ -2,6 +2,39 @@

      给你一个未排序的整数数组 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
      +以下程序实现了这一功能,请你填补空白处内容: + +```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++) { + _________________; + } + 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; + } +} +``` + ## template ```java @@ -40,7 +73,9 @@ class Solution { ## 答案 ```java - +if ((nums[i] <= 0) || (nums[i] > n)) { + nums[i] = 1; +} ``` ## 选项 @@ -48,17 +83,23 @@ class Solution { ### A ```java - +if (nums[i] <= 0) { + nums[i] = 1; +} ``` ### B ```java - +if ((nums[i] <= 0) || (nums[i] > n)) { + nums[i] = 0; +} ``` ### C ```java - +if (nums[i] > n) { + nums[i] = 1; +} ``` \ 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" index 61804cb00..9fa979638 100644 --- "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" @@ -35,6 +35,34 @@ +以下程序实现了这一功能,请你填补空白处内容: + +```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]) { + ________________________; + } + } + return res; + } +} +``` + ## template ```java @@ -72,7 +100,13 @@ class Solution { ## 答案 ```java - +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; ``` ## 选项 @@ -80,17 +114,35 @@ class Solution { ### A ```java - +col[i] = false; +dia1[i - index + n - 1] = false; +dia2[i + index] = false; +res += putQueen(n, index + 1); +col[i] = true; +dia1[i - index + n - 1] = true; +dia2[i + index] = true; ``` ### B ```java - +col[i] = false; +dia1[i - index + n - 1] = false; +dia2[i + index] = false; +res += putQueen(n, index); +col[i] = true; +dia1[i - index + n - 1] = true; +dia2[i + index] = true; ``` ### C ```java - +col[i] = true; +dia1[i - index + n - 1] = true; +dia2[i + index] = true; +res += putQueen(n, index); +col[i] = false; +dia1[i - index + n - 1] = false; +dia2[i + index] = false; ``` \ 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" index 28a27040a..602d6ca59 100644 --- "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" @@ -2,6 +2,55 @@

      给定一个仅包含 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'
      +以下程序实现了这一功能,请你填补空白处内容: + +```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; + } + } + ______________________; + } + return res; + } +} + +``` + ## template ```java @@ -55,7 +104,8 @@ class Solution { ## 答案 ```java - +for (int j = 0; j < n; j++) + res = Math.max(res, (right[j] - left[j]) * height[j]); ``` ## 选项 @@ -63,17 +113,20 @@ class Solution { ### A ```java - +for (int j = 0; j < n; j++) + res = Math.max(res, (right[j] + left[j]) * height[j]); ``` ### B ```java - +for (int j = 0; j < n; j++) + res = Math.max(res, (right[j] + left[j]) / 2 * height[j]); ``` ### C ```java - +for (int j = 0; j < n; j++) + res = Math.max(res, (right[j] - left[j]) / 2 * height[j]); ``` \ 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" index 0927dc226..6c326e183 100644 --- "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" @@ -2,6 +2,52 @@

      给你两个单词 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 由小写英文字母组成
      +以下程序实现了这一功能,请你填补空白处内容: + +```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; + + _____________________; + } + } + return dp[shorterOne]; + } +} + +``` + ## template ```java @@ -56,7 +102,11 @@ class Solution { ## 答案 ```java - +if (i == dp.length - 1) { + dp[i] = min; +} else { + left = min; +} ``` ## 选项 @@ -64,17 +114,29 @@ class Solution { ### A ```java - +if (i == dp.length) { + dp[i] = min; +} else { + left = min; +} ``` ### B ```java - +if (i == dp.length + 1) { + dp[i] = min; +} else { + left = min; +} ``` ### C ```java - +if (i == dp.length - 1) { + left = min; +} else { + dp[i] = min; +} ``` \ 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" index 76d69491f..d616b0917 100644 --- "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" @@ -64,6 +64,53 @@ +以下程序实现了这一功能,请你填补空白处内容: + +```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 (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); + } +} +``` + ## template ```java @@ -117,7 +164,10 @@ class Solution { ## 答案 ```java - +if (i < cur) { + sb.append(left > 0 ? leftStr : baseStr); + left--; +} ``` ## 选项 @@ -125,17 +175,26 @@ class Solution { ### A ```java - +if (i < cur) { + sb.append(left > 0 ? baseStr : leftStr); + left--; +} ``` ### B ```java - +if (i < cur) { + sb.append(left > 0 ? baseStr : leftStr); + left++; +} ``` ### C ```java - +if (i < cur) { + sb.append(left >= 0 ? leftStr : baseStr); + left--; +} ``` \ 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" index 99863e962..d93cb439c 100644 --- "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" @@ -2,6 +2,39 @@

      给你一个只包含 '(' 和 ')' 的字符串,找出最长有效(格式正确且连续)括号子串的长度。

       

      示例 1:

      输入:s = "(()"
      输出:
      2
      解释:
      最长有效括号子串是 "()"

      示例 2:

      输入:s = ")()())"
      输出:
      4
      解释:
      最长有效括号子串是 "()()"

      示例 3:

      输入:s = ""
      输出:
      0

       

      提示:

      • 0 <= s.length <= 3 * 104
      • s[i]'('')'
      +以下程序实现了这一功能,请你填补空白处内容: + +```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 (left == right) + max = Math.max(max, left * 2); + if (right < left) + left = right = 0; + } + return max; + } +} + +``` + ## template ```java @@ -41,7 +74,10 @@ class Solution { ## 答案 ```java - +if (s.charAt(i) == '(') + left++; +else + right++; ``` ## 选项 @@ -49,17 +85,26 @@ class Solution { ### A ```java - +if (s.charAt(i) == ')') + left++; +else + right++; ``` ### B ```java - +if (s.charAt(i) == '(') + right++; +else + left++; ``` ### C ```java - +if (s.charAt(i) == ')') + right++; +else + left++; ``` \ No newline at end of file -- GitLab