提交 c1efa19b 编写于 作者: ToTensor's avatar ToTensor

增加20题

上级 37497e85
...@@ -9,4 +9,5 @@ helper.py ...@@ -9,4 +9,5 @@ helper.py
test.md test.md
data_source/dailycode data_source/dailycode
test.cpp test.cpp
test.py test.py
\ No newline at end of file test.java
\ No newline at end of file
...@@ -2,6 +2,79 @@ ...@@ -2,6 +2,79 @@
<p>给定一个仅包含 <code>0</code> 和 <code>1</code> 、大小为 <code>rows x cols</code> 的二维二进制矩阵,找出只包含 <code>1</code> 的最大矩形,并返回其面积。</p><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0085.Maximal%20Rectangle/images/maximal.jpg" style="width: 402px; height: 322px;" /><pre><strong>输入:</strong>matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]<strong><br />输出:</strong>6<strong><br />解释:</strong>最大矩形如上图所示。</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>matrix = []<strong><br />输出:</strong>0</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>matrix = [["0"]]<strong><br />输出:</strong>0</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>matrix = [["1"]]<strong><br />输出:</strong>1</pre><p><strong>示例 5:</strong></p><pre><strong>输入:</strong>matrix = [["0","0"]]<strong><br />输出:</strong>0</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>rows == matrix.length</code></li> <li><code>cols == matrix[0].length</code></li> <li><code>0 <= row, cols <= 200</code></li> <li><code>matrix[i][j]</code> 为 <code>'0'</code> 或 <code>'1'</code></li></ul> <p>给定一个仅包含 <code>0</code> 和 <code>1</code> 、大小为 <code>rows x cols</code> 的二维二进制矩阵,找出只包含 <code>1</code> 的最大矩形,并返回其面积。</p><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0085.Maximal%20Rectangle/images/maximal.jpg" style="width: 402px; height: 322px;" /><pre><strong>输入:</strong>matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]<strong><br />输出:</strong>6<strong><br />解释:</strong>最大矩形如上图所示。</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>matrix = []<strong><br />输出:</strong>0</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>matrix = [["0"]]<strong><br />输出:</strong>0</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>matrix = [["1"]]<strong><br />输出:</strong>1</pre><p><strong>示例 5:</strong></p><pre><strong>输入:</strong>matrix = [["0","0"]]<strong><br />输出:</strong>0</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>rows == matrix.length</code></li> <li><code>cols == matrix[0].length</code></li> <li><code>0 <= row, cols <= 200</code></li> <li><code>matrix[i][j]</code> 为 <code>'0'</code> 或 <code>'1'</code></li></ul>
以下程序实现了这一功能,请你填补空白处的内容:
```cpp
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
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 ## template
```cpp ```cpp
...@@ -83,7 +156,12 @@ int main(int argc, char **argv) ...@@ -83,7 +156,12 @@ int main(int argc, char **argv)
## 答案 ## 答案
```cpp ```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) ...@@ -91,17 +169,32 @@ int main(int argc, char **argv)
### A ### A
```cpp ```cpp
while (pos > 0 && heights[indexes[pos - 1]] >= heights[i])
{
pos--;
}
rhist[i] = pos == 0 ? indexes[pos - 1] : size;
indexes[pos++] = i;
``` ```
### B ### B
```cpp ```cpp
while (pos > 0 && heights[indexes[pos - 1]] >= heights[i])
{
pos--;
}
rhist[i] = pos == 0 ? size : indexes[pos + 1];
indexes[pos++] = i;
``` ```
### C ### C
```cpp ```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
...@@ -2,6 +2,44 @@ ...@@ -2,6 +2,44 @@
<p>给你两个单词 <code>word1</code> 和 <code>word2</code>,请你计算出将 <code>word1</code> 转换成 <code>word2</code><em> </em>所使用的最少操作数 。</p><p>你可以对一个单词进行如下三种操作:</p><ul> <li>插入一个字符</li> <li>删除一个字符</li> <li>替换一个字符</li></ul><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>word1 = "horse", word2 = "ros"<strong><br />输出:</strong>3<strong><br />解释:</strong>horse -> rorse (将 'h' 替换为 'r')rorse -> rose (删除 'r')rose -> ros (删除 'e')</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>word1 = "intention", word2 = "execution"<strong><br />输出:</strong>5<strong><br />解释:</strong>intention -> inention (删除 't')inention -> enention (将 'i' 替换为 'e')enention -> exention (将 'n' 替换为 'x')exention -> exection (将 'n' 替换为 'c')exection -> execution (插入 'u')</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>0 <= word1.length, word2.length <= 500</code></li> <li><code>word1</code><code>word2</code> 由小写英文字母组成</li></ul> <p>给你两个单词 <code>word1</code> 和 <code>word2</code>,请你计算出将 <code>word1</code> 转换成 <code>word2</code><em> </em>所使用的最少操作数 。</p><p>你可以对一个单词进行如下三种操作:</p><ul> <li>插入一个字符</li> <li>删除一个字符</li> <li>替换一个字符</li></ul><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>word1 = "horse", word2 = "ros"<strong><br />输出:</strong>3<strong><br />解释:</strong>horse -> rorse (将 'h' 替换为 'r')rorse -> rose (删除 'r')rose -> ros (删除 'e')</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>word1 = "intention", word2 = "execution"<strong><br />输出:</strong>5<strong><br />解释:</strong>intention -> inention (删除 't')inention -> enention (将 'i' 替换为 'e')enention -> exention (将 'n' 替换为 'x')exention -> exection (将 'n' 替换为 'c')exection -> execution (插入 'u')</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>0 <= word1.length, word2.length <= 500</code></li> <li><code>word1</code><code>word2</code> 由小写英文字母组成</li></ul>
以下程序实现了这一功能,请你填补空白处的内容:
```cpp
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
int minDistance(string word1, string word2)
{
int l1 = word1.length();
int l2 = word2.length();
vector<int> 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 ## template
```cpp ```cpp
...@@ -46,7 +84,10 @@ public: ...@@ -46,7 +84,10 @@ public:
## 答案 ## 答案
```cpp ```cpp
if (word1[i - 1] == word2[j - 1])
{
dp[j] = left_up;
}
``` ```
## 选项 ## 选项
...@@ -54,17 +95,26 @@ public: ...@@ -54,17 +95,26 @@ public:
### A ### A
```cpp ```cpp
if (word1[i + 1] == word2[j + 1])
{
dp[j] = left_up;
}
``` ```
### B ### B
```cpp ```cpp
if (word1[i] == word2[j])
{
dp[j] = left_up;
}
``` ```
### C ### C
```cpp ```cpp
if (word1[i - 1] == word2[j - 1])
{
dp[j] = left_up + 1;
}
``` ```
\ No newline at end of file
...@@ -64,6 +64,121 @@ ...@@ -64,6 +64,121 @@
</pre> </pre>
</div> </div>
以下程序实现了这一功能,请你填补空白处的内容:
```cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
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 ## template
```cpp ```cpp
...@@ -190,7 +305,15 @@ int main(int argc, char **argv) ...@@ -190,7 +305,15 @@ int main(int argc, char **argv)
## 答案 ## 答案
```cpp ```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) ...@@ -198,17 +321,41 @@ int main(int argc, char **argv)
### A ### A
```cpp ```cpp
for (j = 0; j < even_spaces; j++)
{
*p++ = ' ';
}
if (remain_spaces > 0)
{
*p++ = ' ';
remain_spaces++;
}
``` ```
### B ### B
```cpp ```cpp
for (j = 0; j < even_spaces; j++)
{
*p++ = ' ';
}
if (remain_spaces < 0)
{
*p++ = ' ';
remain_spaces++;
}
``` ```
### C ### C
```cpp ```cpp
for (j = 0; j < even_spaces; j++)
{
*p++ = '\0';
}
if (remain_spaces > 0)
{
*p++ = '\0';
remain_spaces--;
}
``` ```
\ No newline at end of file
...@@ -2,6 +2,43 @@ ...@@ -2,6 +2,43 @@
<p>给你一个只包含 <code>'('</code> 和 <code>')'</code> 的字符串,找出最长有效(格式正确且连续)括号子串的长度。</p><p> </p><div class="original__bRMd"><div><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>s = "(()"<strong><br />输出:</strong>2<strong><br />解释:</strong>最长有效括号子串是 "()"</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>s = ")()())"<strong><br />输出:</strong>4<strong><br />解释:</strong>最长有效括号子串是 "()()"</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>s = ""<strong><br />输出:</strong>0</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>0 <= s.length <= 3 * 10<sup>4</sup></code></li> <li><code>s[i]</code><code>'('</code><code>')'</code></li></ul></div></div> <p>给你一个只包含 <code>'('</code> 和 <code>')'</code> 的字符串,找出最长有效(格式正确且连续)括号子串的长度。</p><p> </p><div class="original__bRMd"><div><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>s = "(()"<strong><br />输出:</strong>2<strong><br />解释:</strong>最长有效括号子串是 "()"</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>s = ")()())"<strong><br />输出:</strong>4<strong><br />解释:</strong>最长有效括号子串是 "()()"</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>s = ""<strong><br />输出:</strong>0</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>0 <= s.length <= 3 * 10<sup>4</sup></code></li> <li><code>s[i]</code><code>'('</code><code>')'</code></li></ul></div></div>
以下程序实现了这一功能,请你填补空白处的内容:
```cpp
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
int longestValidParentheses(string s)
{
stack<int> 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 ## template
```cpp ```cpp
...@@ -49,7 +86,14 @@ public: ...@@ -49,7 +86,14 @@ public:
## 答案 ## 答案
```cpp ```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: ...@@ -57,17 +101,38 @@ public:
### A ### A
```cpp ```cpp
if (stk.empty())
{
max_len = max(i - invalid, max_len);
}
else
{
max_len = max(stk.top(), max_len);
}
``` ```
### B ### B
```cpp ```cpp
if (stk.empty())
{
max_len = max(invalid, max_len);
}
else
{
max_len = max(i - stk.top(), max_len);
}
``` ```
### C ### C
```cpp ```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
...@@ -2,6 +2,54 @@ ...@@ -2,6 +2,54 @@
<p><strong>n 皇后问题</strong> 研究的是如何将 <code>n</code> 个皇后放置在 <code>n×n</code> 的棋盘上,并且使皇后彼此之间不能相互攻击。</p><p>给你一个整数 <code>n</code> ,返回所有不同的 <strong>n<em> </em>皇后问题</strong> 的解决方案。</p><div class="original__bRMd"><div><p>每一种解法包含一个不同的 <strong>n 皇后问题</strong> 的棋子放置方案,该方案中 <code>'Q'</code><code>'.'</code> 分别代表了皇后和空位。</p><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://assets.leetcode.com/uploads/2020/11/13/queens.jpg" style="width: 600px; height: 268px;" /><pre><strong>输入:</strong>n = 4<strong><br />输出:</strong>[[".Q..","...Q","Q...","..Q."],["..Q.","Q...","...Q",".Q.."]]<strong><br />解释:</strong>如上图所示,4 皇后问题存在两个不同的解法。</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>n = 1<strong><br />输出:</strong>[["Q"]]</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>1 <= n <= 9</code></li> <li>皇后彼此不能相互攻击,也就是说:任何两个皇后都不能处于同一条横行、纵行或斜线上。</li></ul></div></div> <p><strong>n 皇后问题</strong> 研究的是如何将 <code>n</code> 个皇后放置在 <code>n×n</code> 的棋盘上,并且使皇后彼此之间不能相互攻击。</p><p>给你一个整数 <code>n</code> ,返回所有不同的 <strong>n<em> </em>皇后问题</strong> 的解决方案。</p><div class="original__bRMd"><div><p>每一种解法包含一个不同的 <strong>n 皇后问题</strong> 的棋子放置方案,该方案中 <code>'Q'</code><code>'.'</code> 分别代表了皇后和空位。</p><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://assets.leetcode.com/uploads/2020/11/13/queens.jpg" style="width: 600px; height: 268px;" /><pre><strong>输入:</strong>n = 4<strong><br />输出:</strong>[[".Q..","...Q","Q...","..Q."],["..Q.","Q...","...Q",".Q.."]]<strong><br />解释:</strong>如上图所示,4 皇后问题存在两个不同的解法。</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>n = 1<strong><br />输出:</strong>[["Q"]]</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>1 <= n <= 9</code></li> <li>皇后彼此不能相互攻击,也就是说:任何两个皇后都不能处于同一条横行、纵行或斜线上。</li></ul></div></div>
以下程序实现了这一功能,请你填补空白处的内容:
```cpp
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
vector<vector<string>> solveNQueens(int n)
{
vector<vector<string>> res;
vector<int> stack(n);
vector<string> solution(n, string(n, '.'));
dfs(n, 0, stack, solution, res);
return res;
}
private:
void dfs(int n, int row, vector<int> &stack, vector<string> &solution, vector<vector<string>> &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<int> &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 ## template
```cpp ```cpp
...@@ -56,7 +104,10 @@ private: ...@@ -56,7 +104,10 @@ private:
## 答案 ## 答案
```cpp ```cpp
solution[row][i] = 'Q';
stack[row] = i;
dfs(n, row + 1, stack, solution, res);
solution[row][i] = '.';
``` ```
## 选项 ## 选项
...@@ -64,17 +115,26 @@ private: ...@@ -64,17 +115,26 @@ private:
### A ### A
```cpp ```cpp
solution[row][i] = 'Q';
stack[row] = i;
dfs(n, row, stack, solution, res);
solution[row][i] = '.';
``` ```
### B ### B
```cpp ```cpp
solution[row][i] = '.';
stack[row] = i;
dfs(n, row + 1, stack, solution, res);
solution[row][i] = 'Q';
``` ```
### C ### C
```cpp ```cpp
solution[row][i] = '.';
stack[row] = i;
dfs(n, row, stack, solution, res);
solution[row][i] = 'Q';
``` ```
\ No newline at end of file
...@@ -60,6 +60,65 @@ ...@@ -60,6 +60,65 @@
</ul> </ul>
</div> </div>
以下程序实现了这一功能,请你填补空白处的内容:
```cpp
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
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 ## template
```cpp ```cpp
...@@ -131,7 +190,16 @@ int main(int argc, char **argv) ...@@ -131,7 +190,16 @@ int main(int argc, char **argv)
## 答案 ## 答案
```cpp ```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) ...@@ -139,17 +207,40 @@ int main(int argc, char **argv)
### A ### A
```cpp ```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 ### B
```cpp ```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 ### C
```cpp ```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
...@@ -2,6 +2,36 @@ ...@@ -2,6 +2,36 @@
<p>给你一个链表,每 <em>k </em>个节点一组进行翻转,请你返回翻转后的链表。</p><p><em>k </em>是一个正整数,它的值小于或等于链表的长度。</p><p>如果节点总数不是 <em>k </em>的整数倍,那么请将最后剩余的节点保持原有顺序。</p><p><strong>进阶:</strong></p><ul> <li>你可以设计一个只使用常数额外空间的算法来解决此问题吗?</li> <li><strong>你不能只是单纯的改变节点内部的值</strong>,而是需要实际进行节点交换。</li></ul><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0025.Reverse%20Nodes%20in%20k-Group/images/reverse_ex1.jpg" style="width: 542px; height: 222px;" /><pre><strong>输入:</strong>head = [1,2,3,4,5], k = 2<strong><br />输出:</strong>[2,1,4,3,5]</pre><p><strong>示例 2:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0025.Reverse%20Nodes%20in%20k-Group/images/reverse_ex2.jpg" style="width: 542px; height: 222px;" /><pre><strong>输入:</strong>head = [1,2,3,4,5], k = 3<strong><br />输出:</strong>[3,2,1,4,5]</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>head = [1,2,3,4,5], k = 1<strong><br />输出:</strong>[1,2,3,4,5]</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>head = [1], k = 1<strong><br />输出:</strong>[1]</pre><ul></ul><p><strong>提示:</strong></p><ul> <li>列表中节点的数量在范围 <code>sz</code> 内</li> <li><code>1 <= sz <= 5000</code></li> <li><code>0 <= Node.val <= 1000</code></li> <li><code>1 <= k <= sz</code></li></ul> <p>给你一个链表,每 <em>k </em>个节点一组进行翻转,请你返回翻转后的链表。</p><p><em>k </em>是一个正整数,它的值小于或等于链表的长度。</p><p>如果节点总数不是 <em>k </em>的整数倍,那么请将最后剩余的节点保持原有顺序。</p><p><strong>进阶:</strong></p><ul> <li>你可以设计一个只使用常数额外空间的算法来解决此问题吗?</li> <li><strong>你不能只是单纯的改变节点内部的值</strong>,而是需要实际进行节点交换。</li></ul><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0025.Reverse%20Nodes%20in%20k-Group/images/reverse_ex1.jpg" style="width: 542px; height: 222px;" /><pre><strong>输入:</strong>head = [1,2,3,4,5], k = 2<strong><br />输出:</strong>[2,1,4,3,5]</pre><p><strong>示例 2:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0025.Reverse%20Nodes%20in%20k-Group/images/reverse_ex2.jpg" style="width: 542px; height: 222px;" /><pre><strong>输入:</strong>head = [1,2,3,4,5], k = 3<strong><br />输出:</strong>[3,2,1,4,5]</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>head = [1,2,3,4,5], k = 1<strong><br />输出:</strong>[1,2,3,4,5]</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>head = [1], k = 1<strong><br />输出:</strong>[1]</pre><ul></ul><p><strong>提示:</strong></p><ul> <li>列表中节点的数量在范围 <code>sz</code> 内</li> <li><code>1 <= sz <= 5000</code></li> <li><code>0 <= Node.val <= 1000</code></li> <li><code>1 <= k <= sz</code></li></ul>
以下程序实现了这一功能,请你填补空白处的内容:
```cpp
#include <bits/stdc++.h>
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 ## template
```cpp ```cpp
...@@ -47,7 +77,19 @@ public: ...@@ -47,7 +77,19 @@ public:
## 答案 ## 答案
```cpp ```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: ...@@ -55,17 +97,53 @@ public:
### A ### A
```cpp ```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 ### B
```cpp ```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 ### C
```cpp ```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
...@@ -2,6 +2,52 @@ ...@@ -2,6 +2,52 @@
<p><strong>有效数字</strong>(按顺序)可以分成以下几个部分:</p><ol> <li>一个 <strong>小数</strong> 或者 <strong>整数</strong></li> <li>(可选)一个 <code>'e'</code><code>'E'</code> ,后面跟着一个 <strong>整数</strong></li></ol><p><strong>小数</strong>(按顺序)可以分成以下几个部分:</p><ol> <li>(可选)一个符号字符(<code>'+'</code><code>'-'</code></li> <li>下述格式之一: <ol> <li>至少一位数字,后面跟着一个点 <code>'.'</code></li> <li>至少一位数字,后面跟着一个点 <code>'.'</code> ,后面再跟着至少一位数字</li> <li>一个点 <code>'.'</code> ,后面跟着至少一位数字</li> </ol> </li></ol><p><strong>整数</strong>(按顺序)可以分成以下几个部分:</p><ol> <li>(可选)一个符号字符(<code>'+'</code><code>'-'</code></li> <li>至少一位数字</li></ol><p>部分有效数字列举如下:</p><ul> <li><code>["2", "0089", "-0.1", "+3.14", "4.", "-.9", "2e10", "-90E3", "3e+7", "+6e-1", "53.5e93", "-123.456e789"]</code></li></ul><p>部分无效数字列举如下:</p><ul> <li><code>["abc", "1a", "1e", "e3", "99e2.5", "--6", "-+3", "95a54e53"]</code></li></ul><p>给你一个字符串 <code>s</code> ,如果 <code>s</code> 是一个 <strong>有效数字</strong> ,请返回 <code>true</code></p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>s = "0"<strong><br />输出:</strong>true</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>s = "e"<strong><br />输出:</strong>false</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>s = "."<strong><br />输出:</strong>false</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>s = ".1"<strong><br />输出:</strong>true</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>1 <= s.length <= 20</code></li> <li><code>s</code> 仅含英文字母(大写和小写),数字(<code>0-9</code>),加号 <code>'+'</code> ,减号 <code>'-'</code> ,或者点 <code>'.'</code></li></ul> <p><strong>有效数字</strong>(按顺序)可以分成以下几个部分:</p><ol> <li>一个 <strong>小数</strong> 或者 <strong>整数</strong></li> <li>(可选)一个 <code>'e'</code><code>'E'</code> ,后面跟着一个 <strong>整数</strong></li></ol><p><strong>小数</strong>(按顺序)可以分成以下几个部分:</p><ol> <li>(可选)一个符号字符(<code>'+'</code><code>'-'</code></li> <li>下述格式之一: <ol> <li>至少一位数字,后面跟着一个点 <code>'.'</code></li> <li>至少一位数字,后面跟着一个点 <code>'.'</code> ,后面再跟着至少一位数字</li> <li>一个点 <code>'.'</code> ,后面跟着至少一位数字</li> </ol> </li></ol><p><strong>整数</strong>(按顺序)可以分成以下几个部分:</p><ol> <li>(可选)一个符号字符(<code>'+'</code><code>'-'</code></li> <li>至少一位数字</li></ol><p>部分有效数字列举如下:</p><ul> <li><code>["2", "0089", "-0.1", "+3.14", "4.", "-.9", "2e10", "-90E3", "3e+7", "+6e-1", "53.5e93", "-123.456e789"]</code></li></ul><p>部分无效数字列举如下:</p><ul> <li><code>["abc", "1a", "1e", "e3", "99e2.5", "--6", "-+3", "95a54e53"]</code></li></ul><p>给你一个字符串 <code>s</code> ,如果 <code>s</code> 是一个 <strong>有效数字</strong> ,请返回 <code>true</code></p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>s = "0"<strong><br />输出:</strong>true</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>s = "e"<strong><br />输出:</strong>false</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>s = "."<strong><br />输出:</strong>false</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>s = ".1"<strong><br />输出:</strong>true</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>1 <= s.length <= 20</code></li> <li><code>s</code> 仅含英文字母(大写和小写),数字(<code>0-9</code>),加号 <code>'+'</code> ,减号 <code>'-'</code> ,或者点 <code>'.'</code></li></ul>
以下程序实现了这一功能,请你填补空白处的内容:
```cpp
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <ctype.h>
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 ## template
```cpp ```cpp
...@@ -59,7 +105,15 @@ int main(int argc, char **argv) ...@@ -59,7 +105,15 @@ int main(int argc, char **argv)
## 答案 ## 答案
```cpp ```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) ...@@ -67,17 +121,39 @@ int main(int argc, char **argv)
### A ### A
```cpp ```cpp
if (*s == '+' || *s == '-')
++s;
if_find_num = false;
while (isdigit(*s))
{
if_find_num = true;
++s;
}
``` ```
### B ### B
```cpp ```cpp
if (*s == '+' || *s == '-')
++s;
if_find_num = true;
while (isdigit(*s))
{
if_find_num = false;
++s;
}
``` ```
### C ### C
```cpp ```cpp
++s;
if (*s == '+' || *s == '-')
++s;
if_find_num = true;
while (isdigit(*s))
{
if_find_num = false;
++s;
}
``` ```
\ No newline at end of file
...@@ -9,6 +9,45 @@ ...@@ -9,6 +9,45 @@
<p><strong>示例 2:</strong></p> <p><strong>示例 2:</strong></p>
<pre><strong>输入: s =</strong> &quot;wordgoodgoodgoodbestword&quot;,<strong> words = </strong>[&quot;word&quot;,&quot;good&quot;,&quot;best&quot;,&quot;word&quot;]<strong><br />输出:</strong>[]</pre> <pre><strong>输入: s =</strong> &quot;wordgoodgoodgoodbestword&quot;,<strong> words = </strong>[&quot;word&quot;,&quot;good&quot;,&quot;best&quot;,&quot;word&quot;]<strong><br />输出:</strong>[]</pre>
以下程序实现了这一功能,请你填补空白处的内容:
```cpp
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
vector<int> findSubstring(string s, vector<string> &words)
{
vector<int> res;
if (s.empty() || words.empty())
{
return res;
}
unordered_map<string, int> 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<string, int> 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 ## template
```cpp ```cpp
...@@ -54,7 +93,10 @@ public: ...@@ -54,7 +93,10 @@ public:
## 答案 ## 答案
```cpp ```cpp
if (++counting[word] > ht[word])
{
break;
}
``` ```
## 选项 ## 选项
...@@ -62,17 +104,26 @@ public: ...@@ -62,17 +104,26 @@ public:
### A ### A
```cpp ```cpp
if (++counting[word] < ht[word])
{
break;
}
``` ```
### B ### B
```cpp ```cpp
if (++counting[word] <= ht[word])
{
continue;
}
``` ```
### C ### C
```cpp ```cpp
if (++counting[word] <= ht[word])
{
break;
}
``` ```
\ No newline at end of file
...@@ -2,6 +2,46 @@ ...@@ -2,6 +2,46 @@
<p>给定两个大小分别为 <code>m</code><code>n</code> 的正序(从小到大)数组 <code>nums1</code> 和 <code>nums2</code>。请你找出并返回这两个正序数组的 <strong>中位数</strong></p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>nums1 = [1,3], nums2 = [2]<strong><br />输出:</strong>2.00000<strong><br />解释:</strong>合并数组 = [1,2,3] ,中位数 2</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>nums1 = [1,2], nums2 = [3,4]<strong><br />输出:</strong>2.50000<strong><br />解释:</strong>合并数组 = [1,2,3,4] ,中位数 (2 + 3) / 2 = 2.5</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>nums1 = [0,0], nums2 = [0,0]<strong><br />输出:</strong>0.00000</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>nums1 = [], nums2 = [1]<strong><br />输出:</strong>1.00000</pre><p><strong>示例 5:</strong></p><pre><strong>输入:</strong>nums1 = [2], nums2 = []<strong><br />输出:</strong>2.00000</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>nums1.length == m</code></li> <li><code>nums2.length == n</code></li> <li><code>0 <= m <= 1000</code></li> <li><code>0 <= n <= 1000</code></li> <li><code>1 <= m + n <= 2000</code></li> <li><code>-10<sup>6</sup> <= nums1[i], nums2[i] <= 10<sup>6</sup></code></li></ul><p> </p><p><strong>进阶:</strong>你能设计一个时间复杂度为 <code>O(log (m+n))</code> 的算法解决此问题吗?</p> <p>给定两个大小分别为 <code>m</code><code>n</code> 的正序(从小到大)数组 <code>nums1</code> 和 <code>nums2</code>。请你找出并返回这两个正序数组的 <strong>中位数</strong></p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>nums1 = [1,3], nums2 = [2]<strong><br />输出:</strong>2.00000<strong><br />解释:</strong>合并数组 = [1,2,3] ,中位数 2</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>nums1 = [1,2], nums2 = [3,4]<strong><br />输出:</strong>2.50000<strong><br />解释:</strong>合并数组 = [1,2,3,4] ,中位数 (2 + 3) / 2 = 2.5</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>nums1 = [0,0], nums2 = [0,0]<strong><br />输出:</strong>0.00000</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>nums1 = [], nums2 = [1]<strong><br />输出:</strong>1.00000</pre><p><strong>示例 5:</strong></p><pre><strong>输入:</strong>nums1 = [2], nums2 = []<strong><br />输出:</strong>2.00000</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>nums1.length == m</code></li> <li><code>nums2.length == n</code></li> <li><code>0 <= m <= 1000</code></li> <li><code>0 <= n <= 1000</code></li> <li><code>1 <= m + n <= 2000</code></li> <li><code>-10<sup>6</sup> <= nums1[i], nums2[i] <= 10<sup>6</sup></code></li></ul><p> </p><p><strong>进阶:</strong>你能设计一个时间复杂度为 <code>O(log (m+n))</code> 的算法解决此问题吗?</p>
以下程序实现了这一功能,请你填补空白处的内容:
```cpp
class Solution
{
public:
double findMedianSortedArrays(vector<int> &nums1, vector<int> &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 ## template
```cpp ```cpp
...@@ -45,7 +85,7 @@ public: ...@@ -45,7 +85,7 @@ public:
## 答案 ## 答案
```cpp ```cpp
n = (nums1[i] < nums2[j]) ? nums1[i++] : nums2[j++];
``` ```
## 选项 ## 选项
...@@ -53,17 +93,17 @@ public: ...@@ -53,17 +93,17 @@ public:
### A ### A
```cpp ```cpp
n = (nums1[i] < nums2[j]) ? nums1[j++] : nums2[i++];
``` ```
### B ### B
```cpp ```cpp
n = (nums1[i] > nums2[j]) ? nums1[i++] : nums2[j++];
``` ```
### C ### C
```cpp ```cpp
n = (nums1[i] >= nums2[j]) ? nums1[i++] : nums2[j++];
``` ```
\ No newline at end of file
...@@ -2,6 +2,45 @@ ...@@ -2,6 +2,45 @@
<p>给你一个字符串 <code>s</code> 、一个字符串 <code>t</code> 。返回 <code>s</code> 中涵盖 <code>t</code> 所有字符的最小子串。如果 <code>s</code> 中不存在涵盖 <code>t</code> 所有字符的子串,则返回空字符串 <code>""</code></p><p><strong>注意:</strong>如果 <code>s</code> 中存在这样的子串,我们保证它是唯一的答案。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>s = "ADOBECODEBANC", t = "ABC"<strong><br />输出:</strong>"BANC"</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>s = "a", t = "a"<strong><br />输出:</strong>"a"</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>1 <= s.length, t.length <= 10<sup>5</sup></code></li> <li><code>s</code><code>t</code> 由英文字母组成</li></ul><p> </p><strong>进阶:</strong>你能设计一个在 <code>o(n)</code> 时间内解决此问题的算法吗? <p>给你一个字符串 <code>s</code> 、一个字符串 <code>t</code> 。返回 <code>s</code> 中涵盖 <code>t</code> 所有字符的最小子串。如果 <code>s</code> 中不存在涵盖 <code>t</code> 所有字符的子串,则返回空字符串 <code>""</code></p><p><strong>注意:</strong>如果 <code>s</code> 中存在这样的子串,我们保证它是唯一的答案。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>s = "ADOBECODEBANC", t = "ABC"<strong><br />输出:</strong>"BANC"</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>s = "a", t = "a"<strong><br />输出:</strong>"a"</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>1 <= s.length, t.length <= 10<sup>5</sup></code></li> <li><code>s</code><code>t</code> 由英文字母组成</li></ul><p> </p><strong>进阶:</strong>你能设计一个在 <code>o(n)</code> 时间内解决此问题的算法吗?
以下程序实现了这一功能,请你填补空白处的内容:
```cpp
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
string minWindow(string s, string t)
{
vector<int> 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 ## template
```cpp ```cpp
...@@ -47,7 +86,10 @@ public: ...@@ -47,7 +86,10 @@ public:
## 答案 ## 答案
```cpp ```cpp
if (++count[s[l++]] > 0)
{
need_to_meet++;
}
``` ```
## 选项 ## 选项
...@@ -55,17 +97,26 @@ public: ...@@ -55,17 +97,26 @@ public:
### A ### A
```cpp ```cpp
if (count[s[l++]] > 0)
{
need_to_meet++;
}
``` ```
### B ### B
```cpp ```cpp
if (count[s[l++]] >= 0)
{
need_to_meet++;
}
``` ```
### C ### C
```cpp ```cpp
if (++count[s[l++]++] > 0)
{
need_to_meet++;
}
``` ```
\ No newline at end of file
...@@ -2,6 +2,47 @@ ...@@ -2,6 +2,47 @@
<p>给你一个链表数组,每个链表都已经按升序排列。</p><p>请你将所有链表合并到一个升序链表中,返回合并后的链表。</p><p>&nbsp;</p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>lists = [[1,4,5],[1,3,4],[2,6]]<strong><br />输出:</strong>[1,1,2,3,4,4,5,6]<strong><br />解释:</strong>链表数组如下:[ 1-&gt;4-&gt;5, 1-&gt;3-&gt;4, 2-&gt;6]将它们合并到一个有序链表中得到。1-&gt;1-&gt;2-&gt;3-&gt;4-&gt;4-&gt;5-&gt;6</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>lists = []<strong><br />输出:</strong>[]</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>lists = [[]]<strong><br />输出:</strong>[]</pre><p>&nbsp;</p><p><strong>提示:</strong></p><ul> <li><code>k == lists.length</code></li> <li><code>0 &lt;= k &lt;= 10^4</code></li> <li><code>0 &lt;= lists[i].length &lt;= 500</code></li> <li><code>-10^4 &lt;= lists[i][j] &lt;= 10^4</code></li> <li><code>lists[i]</code><strong>升序</strong> 排列</li> <li><code>lists[i].length</code> 的总和不超过 <code>10^4</code></li></ul> <p>给你一个链表数组,每个链表都已经按升序排列。</p><p>请你将所有链表合并到一个升序链表中,返回合并后的链表。</p><p>&nbsp;</p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>lists = [[1,4,5],[1,3,4],[2,6]]<strong><br />输出:</strong>[1,1,2,3,4,4,5,6]<strong><br />解释:</strong>链表数组如下:[ 1-&gt;4-&gt;5, 1-&gt;3-&gt;4, 2-&gt;6]将它们合并到一个有序链表中得到。1-&gt;1-&gt;2-&gt;3-&gt;4-&gt;4-&gt;5-&gt;6</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>lists = []<strong><br />输出:</strong>[]</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>lists = [[]]<strong><br />输出:</strong>[]</pre><p>&nbsp;</p><p><strong>提示:</strong></p><ul> <li><code>k == lists.length</code></li> <li><code>0 &lt;= k &lt;= 10^4</code></li> <li><code>0 &lt;= lists[i].length &lt;= 500</code></li> <li><code>-10^4 &lt;= lists[i][j] &lt;= 10^4</code></li> <li><code>lists[i]</code><strong>升序</strong> 排列</li> <li><code>lists[i].length</code> 的总和不超过 <code>10^4</code></li></ul>
以下程序实现了这一功能,请你填补空白处的内容:
```cpp
#include <bits/stdc++.h>
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<ListNode *> &lists)
{
auto cmp = [](struct ListNode *n1, struct ListNode *n2)
{
return n1->val > n2->val;
} priority_queue<struct ListNode *, vector<struct ListNode *>, 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 ## template
```cpp ```cpp
...@@ -53,7 +94,14 @@ public: ...@@ -53,7 +94,14 @@ public:
## 答案 ## 答案
```cpp ```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: ...@@ -61,17 +109,38 @@ public:
### A ### A
```cpp ```cpp
ListNode *node = queue.top();
queue.pop();
p->next = node;
p = node;
if (node->next != nullptr)
{
queue.pop(node->next);
}
``` ```
### B ### B
```cpp ```cpp
ListNode *node = queue.top();
queue.pop();
p = node;
p->next = node;
if (node->next != nullptr)
{
queue.push(node->next);
}
``` ```
### C ### C
```cpp ```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
...@@ -2,6 +2,51 @@ ...@@ -2,6 +2,51 @@
<p>给出集合 <code>[1,2,3,...,n]</code>,其所有元素共有 <code>n!</code> 种排列。</p><p>按大小顺序列出所有排列情况,并一一标记,当 <code>n = 3</code> 时, 所有排列如下:</p><ol> <li><code>"123"</code></li> <li><code>"132"</code></li> <li><code>"213"</code></li> <li><code>"231"</code></li> <li><code>"312"</code></li> <li><code>"321"</code></li></ol><p>给定 <code>n</code> 和 <code>k</code>,返回第 <code>k</code> 个排列。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>n = 3, k = 3<strong><br />输出:</strong>"213"</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>n = 4, k = 9<strong><br />输出:</strong>"2314"</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>n = 3, k = 1<strong><br />输出:</strong>"123"</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>1 <= n <= 9</code></li> <li><code>1 <= k <= n!</code></li></ul> <p>给出集合 <code>[1,2,3,...,n]</code>,其所有元素共有 <code>n!</code> 种排列。</p><p>按大小顺序列出所有排列情况,并一一标记,当 <code>n = 3</code> 时, 所有排列如下:</p><ol> <li><code>"123"</code></li> <li><code>"132"</code></li> <li><code>"213"</code></li> <li><code>"231"</code></li> <li><code>"312"</code></li> <li><code>"321"</code></li></ol><p>给定 <code>n</code> 和 <code>k</code>,返回第 <code>k</code> 个排列。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>n = 3, k = 3<strong><br />输出:</strong>"213"</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>n = 4, k = 9<strong><br />输出:</strong>"2314"</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>n = 3, k = 1<strong><br />输出:</strong>"123"</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>1 <= n <= 9</code></li> <li><code>1 <= k <= n!</code></li></ul>
以下程序实现了这一功能,请你填补空白处的内容:
```cpp
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
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 ## template
```cpp ```cpp
...@@ -57,7 +102,14 @@ int main(int argc, char **argv) ...@@ -57,7 +102,14 @@ int main(int argc, char **argv)
## 答案 ## 答案
```cpp ```cpp
while (count <= gid)
{
x = (x + 1) % n;
if (!used[x])
{
count++;
}
}
``` ```
## 选项 ## 选项
...@@ -65,17 +117,38 @@ int main(int argc, char **argv) ...@@ -65,17 +117,38 @@ int main(int argc, char **argv)
### A ### A
```cpp ```cpp
while (count <= gid)
{
x = (x + 1) % n;
if (used[x])
{
count++;
}
}
``` ```
### B ### B
```cpp ```cpp
while (count < gid)
{
x = (x + 1) % n;
if (used[x])
{
count++;
}
}
``` ```
### C ### C
```cpp ```cpp
while (count <= gid)
{
x = x % n;
if (used[x])
{
count++;
}
}
``` ```
\ No newline at end of file
...@@ -2,6 +2,52 @@ ...@@ -2,6 +2,52 @@
<p>给定一个字符串&nbsp;(<code>s</code>) 和一个字符模式&nbsp;(<code>p</code>) ,实现一个支持&nbsp;<code>&#39;?&#39;</code>&nbsp;&nbsp;<code>&#39;*&#39;</code>&nbsp;的通配符匹配。</p><pre>&#39;?&#39; 可以匹配任何单个字符。&#39;*&#39; 可以匹配任意字符串(包括空字符串)。</pre><p>两个字符串<strong>完全匹配</strong>才算匹配成功。</p><p><strong>说明:</strong></p><ul> <li><code>s</code>&nbsp;可能为空,且只包含从&nbsp;<code>a-z</code>&nbsp;的小写字母。</li> <li><code>p</code>&nbsp;可能为空,且只包含从&nbsp;<code>a-z</code>&nbsp;的小写字母,以及字符&nbsp;<code>?</code>&nbsp;&nbsp;<code>*</code></li></ul><p><strong>示例&nbsp;1:</strong></p><pre><strong>输入:</strong>s = &quot;aa&quot;p = &quot;a&quot;<strong><br />输出:</strong> false<strong><br />解释:</strong> &quot;a&quot; 无法匹配 &quot;aa&quot; 整个字符串。</pre><p><strong>示例&nbsp;2:</strong></p><pre><strong>输入:</strong>s = &quot;aa&quot;p = &quot;*&quot;<strong><br />输出:</strong> true<strong><br />解释:</strong>&nbsp;&#39;*&#39; 可以匹配任意字符串。</pre><p><strong>示例&nbsp;3:</strong></p><pre><strong>输入:</strong>s = &quot;cb&quot;p = &quot;?a&quot;<strong><br />输出:</strong> false<strong><br />解释:</strong>&nbsp;&#39;?&#39; 可以匹配 &#39;c&#39;, 但第二个 &#39;a&#39; 无法匹配 &#39;b&#39;</pre><p><strong>示例&nbsp;4:</strong></p><pre><strong>输入:</strong>s = &quot;adceb&quot;p = &quot;*a*b&quot;<strong><br />输出:</strong> true<strong><br />解释:</strong>&nbsp;第一个 &#39;*&#39; 可以匹配空字符串, 第二个 &#39;*&#39; 可以匹配字符串 &quot;dce&quot;.</pre><p><strong>示例&nbsp;5:</strong></p><pre><strong>输入:</strong>s = &quot;acdcb&quot;p = &quot;a*c?b&quot;<strong><br />输出:</strong> false</pre> <p>给定一个字符串&nbsp;(<code>s</code>) 和一个字符模式&nbsp;(<code>p</code>) ,实现一个支持&nbsp;<code>&#39;?&#39;</code>&nbsp;&nbsp;<code>&#39;*&#39;</code>&nbsp;的通配符匹配。</p><pre>&#39;?&#39; 可以匹配任何单个字符。&#39;*&#39; 可以匹配任意字符串(包括空字符串)。</pre><p>两个字符串<strong>完全匹配</strong>才算匹配成功。</p><p><strong>说明:</strong></p><ul> <li><code>s</code>&nbsp;可能为空,且只包含从&nbsp;<code>a-z</code>&nbsp;的小写字母。</li> <li><code>p</code>&nbsp;可能为空,且只包含从&nbsp;<code>a-z</code>&nbsp;的小写字母,以及字符&nbsp;<code>?</code>&nbsp;&nbsp;<code>*</code></li></ul><p><strong>示例&nbsp;1:</strong></p><pre><strong>输入:</strong>s = &quot;aa&quot;p = &quot;a&quot;<strong><br />输出:</strong> false<strong><br />解释:</strong> &quot;a&quot; 无法匹配 &quot;aa&quot; 整个字符串。</pre><p><strong>示例&nbsp;2:</strong></p><pre><strong>输入:</strong>s = &quot;aa&quot;p = &quot;*&quot;<strong><br />输出:</strong> true<strong><br />解释:</strong>&nbsp;&#39;*&#39; 可以匹配任意字符串。</pre><p><strong>示例&nbsp;3:</strong></p><pre><strong>输入:</strong>s = &quot;cb&quot;p = &quot;?a&quot;<strong><br />输出:</strong> false<strong><br />解释:</strong>&nbsp;&#39;?&#39; 可以匹配 &#39;c&#39;, 但第二个 &#39;a&#39; 无法匹配 &#39;b&#39;</pre><p><strong>示例&nbsp;4:</strong></p><pre><strong>输入:</strong>s = &quot;adceb&quot;p = &quot;*a*b&quot;<strong><br />输出:</strong> true<strong><br />解释:</strong>&nbsp;第一个 &#39;*&#39; 可以匹配空字符串, 第二个 &#39;*&#39; 可以匹配字符串 &quot;dce&quot;.</pre><p><strong>示例&nbsp;5:</strong></p><pre><strong>输入:</strong>s = &quot;acdcb&quot;p = &quot;a*c?b&quot;<strong><br />输出:</strong> false</pre>
以下程序实现了这一功能,请你填补空白处的内容:
```cpp
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
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 ## template
```cpp ```cpp
...@@ -60,7 +106,16 @@ int main(int argc, char **argv) ...@@ -60,7 +106,16 @@ int main(int argc, char **argv)
## 答案 ## 答案
```cpp ```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) ...@@ -68,17 +123,34 @@ int main(int argc, char **argv)
### A ### A
```cpp ```cpp
else if (last_s != NULL)
{
p = last_p;
s = ++last_s;
}
``` ```
### B ### B
```cpp ```cpp
else if (*p == '?' || *s == *p)
{
s++;
p++;
}
``` ```
### C ### C
```cpp ```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
...@@ -2,6 +2,25 @@ ...@@ -2,6 +2,25 @@
<p>给你一个字符串 <code>s</code> 和一个字符规律 <code>p</code>,请你来实现一个支持 <code>'.'</code> 和 <code>'*'</code> 的正则表达式匹配。</p><ul> <li><code>'.'</code> 匹配任意单个字符</li> <li><code>'*'</code> 匹配零个或多个前面的那一个元素</li></ul><p>所谓匹配,是要涵盖 <strong>整个 </strong>字符串 <code>s</code>的,而不是部分字符串。</p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>s = "aa" p = "a"<strong><br />输出:</strong>false<strong><br />解释:</strong>"a" 无法匹配 "aa" 整个字符串。</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>s = "aa" p = "a*"<strong><br />输出:</strong>true<strong><br />解释:</strong>因为 '*' 代表可以匹配零个或多个前面的那一个元素, 在这里前面的元素就是 'a'。因此,字符串 "aa" 可被视为 'a' 重复了一次。</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>s = "ab" p = ".*"<strong><br />输出:</strong>true<strong><br />解释:</strong>".*" 表示可匹配零个或多个('*')任意字符('.')。</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>s = "aab" p = "c*a*b"<strong><br />输出:</strong>true<strong><br />解释:</strong>因为 '*' 表示零个或多个,这里 'c' 为 0 个, 'a' 被重复一次。因此可以匹配字符串 "aab"。</pre><p><strong>示例 5:</strong></p><pre><strong>输入:</strong>s = "mississippi" p = "mis*is*p*."<strong><br />输出:</strong>false</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>0 <= s.length <= 20</code></li> <li><code>0 <= p.length <= 30</code></li> <li><code>s</code> 可能为空,且只包含从 <code>a-z</code> 的小写字母。</li> <li><code>p</code> 可能为空,且只包含从 <code>a-z</code> 的小写字母,以及字符 <code>.</code> 和 <code>*</code></li> <li>保证每次出现字符 <code>*</code> 时,前面都匹配到有效的字符</li></ul> <p>给你一个字符串 <code>s</code> 和一个字符规律 <code>p</code>,请你来实现一个支持 <code>'.'</code> 和 <code>'*'</code> 的正则表达式匹配。</p><ul> <li><code>'.'</code> 匹配任意单个字符</li> <li><code>'*'</code> 匹配零个或多个前面的那一个元素</li></ul><p>所谓匹配,是要涵盖 <strong>整个 </strong>字符串 <code>s</code>的,而不是部分字符串。</p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>s = "aa" p = "a"<strong><br />输出:</strong>false<strong><br />解释:</strong>"a" 无法匹配 "aa" 整个字符串。</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>s = "aa" p = "a*"<strong><br />输出:</strong>true<strong><br />解释:</strong>因为 '*' 代表可以匹配零个或多个前面的那一个元素, 在这里前面的元素就是 'a'。因此,字符串 "aa" 可被视为 'a' 重复了一次。</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>s = "ab" p = ".*"<strong><br />输出:</strong>true<strong><br />解释:</strong>".*" 表示可匹配零个或多个('*')任意字符('.')。</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>s = "aab" p = "c*a*b"<strong><br />输出:</strong>true<strong><br />解释:</strong>因为 '*' 表示零个或多个,这里 'c' 为 0 个, 'a' 被重复一次。因此可以匹配字符串 "aab"。</pre><p><strong>示例 5:</strong></p><pre><strong>输入:</strong>s = "mississippi" p = "mis*is*p*."<strong><br />输出:</strong>false</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>0 <= s.length <= 20</code></li> <li><code>0 <= p.length <= 30</code></li> <li><code>s</code> 可能为空,且只包含从 <code>a-z</code> 的小写字母。</li> <li><code>p</code> 可能为空,且只包含从 <code>a-z</code> 的小写字母,以及字符 <code>.</code> 和 <code>*</code></li> <li>保证每次出现字符 <code>*</code> 时,前面都匹配到有效的字符</li></ul>
以下程序实现了这一功能,请你填补空白处的内容:
```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 ## template
```cpp ```cpp
...@@ -24,7 +43,7 @@ bool isMatch(char *s, char *p) ...@@ -24,7 +43,7 @@ bool isMatch(char *s, char *p)
## 答案 ## 答案
```cpp ```cpp
return (head_match && isMatch(s + 1, p)) || isMatch(s, p + 2);
``` ```
## 选项 ## 选项
...@@ -32,17 +51,17 @@ bool isMatch(char *s, char *p) ...@@ -32,17 +51,17 @@ bool isMatch(char *s, char *p)
### A ### A
```cpp ```cpp
return (head_match && isMatch(s + 1, p)) && isMatch(s, p + 2);
``` ```
### B ### B
```cpp ```cpp
return (head_match || isMatch(s + 1, p)) || isMatch(s, p + 2);
``` ```
### C ### C
```cpp ```cpp
return (head_match || isMatch(s + 1, p)) && isMatch(s, p + 2);
``` ```
\ No newline at end of file
...@@ -52,6 +52,67 @@ ...@@ -52,6 +52,67 @@
</div> </div>
</div> </div>
以下程序实现了这一功能,请你填补空白处内容:
```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 ## template
```java ```java
...@@ -116,7 +177,8 @@ class Solution { ...@@ -116,7 +177,8 @@ class Solution {
## 答案 ## 答案
```java ```java
if (board[x][y] != '.')
return dfs(board, x, y + 1);
``` ```
## 选项 ## 选项
...@@ -124,17 +186,20 @@ class Solution { ...@@ -124,17 +186,20 @@ class Solution {
### A ### A
```java ```java
if (board[x][y] != '.')
return dfs(board, x, y);
``` ```
### B ### B
```java ```java
if (board[x][y] == '.')
return dfs(board, x, y);
``` ```
### C ### C
```java ```java
if (board[x][y] == '.')
return dfs(board, x, y + 1);
``` ```
\ No newline at end of file
...@@ -2,6 +2,59 @@ ...@@ -2,6 +2,59 @@
<p><strong>n 皇后问题</strong> 研究的是如何将 <code>n</code> 个皇后放置在 <code>n×n</code> 的棋盘上,并且使皇后彼此之间不能相互攻击。</p><p>给你一个整数 <code>n</code> ,返回所有不同的 <strong>n<em> </em>皇后问题</strong> 的解决方案。</p><div class="original__bRMd"><div><p>每一种解法包含一个不同的 <strong>n 皇后问题</strong> 的棋子放置方案,该方案中 <code>'Q'</code><code>'.'</code> 分别代表了皇后和空位。</p><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://assets.leetcode.com/uploads/2020/11/13/queens.jpg" style="width: 600px; height: 268px;" /><pre><strong>输入:</strong>n = 4<strong><br />输出:</strong>[[".Q..","...Q","Q...","..Q."],["..Q.","Q...","...Q",".Q.."]]<strong><br />解释:</strong>如上图所示,4 皇后问题存在两个不同的解法。</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>n = 1<strong><br />输出:</strong>[["Q"]]</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>1 <= n <= 9</code></li> <li>皇后彼此不能相互攻击,也就是说:任何两个皇后都不能处于同一条横行、纵行或斜线上。</li></ul></div></div> <p><strong>n 皇后问题</strong> 研究的是如何将 <code>n</code> 个皇后放置在 <code>n×n</code> 的棋盘上,并且使皇后彼此之间不能相互攻击。</p><p>给你一个整数 <code>n</code> ,返回所有不同的 <strong>n<em> </em>皇后问题</strong> 的解决方案。</p><div class="original__bRMd"><div><p>每一种解法包含一个不同的 <strong>n 皇后问题</strong> 的棋子放置方案,该方案中 <code>'Q'</code><code>'.'</code> 分别代表了皇后和空位。</p><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://assets.leetcode.com/uploads/2020/11/13/queens.jpg" style="width: 600px; height: 268px;" /><pre><strong>输入:</strong>n = 4<strong><br />输出:</strong>[[".Q..","...Q","Q...","..Q."],["..Q.","Q...","...Q",".Q.."]]<strong><br />解释:</strong>如上图所示,4 皇后问题存在两个不同的解法。</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>n = 1<strong><br />输出:</strong>[["Q"]]</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>1 <= n <= 9</code></li> <li>皇后彼此不能相互攻击,也就是说:任何两个皇后都不能处于同一条横行、纵行或斜线上。</li></ul></div></div>
以下程序实现了这一功能,请你填补空白处内容:
```java
import java.util.List;
import java.util.ArrayList;
public class Solution {
public List<List<String>> solveNQueens(int n) {
List<List<String>> res = new ArrayList<List<String>>();
int[] queenList = new int[n];
placeQueen(queenList, 0, n, res);
return res;
}
private void placeQueen(int[] queenList, int row, int n, List<List<String>> res) {
if (row == n) {
ArrayList<String> list = new ArrayList<String>();
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 ## template
```java ```java
...@@ -58,7 +111,7 @@ public class Solution { ...@@ -58,7 +111,7 @@ public class Solution {
## 答案 ## 答案
```java ```java
placeQueen(queenList, row + 1, n, res);
``` ```
## 选项 ## 选项
...@@ -66,17 +119,17 @@ public class Solution { ...@@ -66,17 +119,17 @@ public class Solution {
### A ### A
```java ```java
placeQueen(queenList, row, n, res);
``` ```
### B ### B
```java ```java
placeQueen(queenList, row + 1, n + 1, res);
``` ```
### C ### C
```java ```java
placeQueen(queenList, row, n + 1, res);
``` ```
\ No newline at end of file
...@@ -60,6 +60,44 @@ ...@@ -60,6 +60,44 @@
</ul> </ul>
</div> </div>
以下程序实现了这一功能,请你填补空白处内容:
```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 ## template
```java ```java
...@@ -104,7 +142,10 @@ class Solution { ...@@ -104,7 +142,10 @@ class Solution {
## 答案 ## 答案
```java ```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 { ...@@ -112,17 +153,26 @@ class Solution {
### A ### A
```java ```java
if (dfs(s1, s2, start1, start2, i))
return true;
if (dfs(s1, s2, start1, start2 + len - i, i))
return true;
``` ```
### B ### B
```java ```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 ### C
```java ```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
...@@ -2,6 +2,60 @@ ...@@ -2,6 +2,60 @@
<p>给你一个链表,每 <em>k </em>个节点一组进行翻转,请你返回翻转后的链表。</p><p><em>k </em>是一个正整数,它的值小于或等于链表的长度。</p><p>如果节点总数不是 <em>k </em>的整数倍,那么请将最后剩余的节点保持原有顺序。</p><p><strong>进阶:</strong></p><ul> <li>你可以设计一个只使用常数额外空间的算法来解决此问题吗?</li> <li><strong>你不能只是单纯的改变节点内部的值</strong>,而是需要实际进行节点交换。</li></ul><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0025.Reverse%20Nodes%20in%20k-Group/images/reverse_ex1.jpg" style="width: 542px; height: 222px;" /><pre><strong>输入:</strong>head = [1,2,3,4,5], k = 2<strong><br />输出:</strong>[2,1,4,3,5]</pre><p><strong>示例 2:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0025.Reverse%20Nodes%20in%20k-Group/images/reverse_ex2.jpg" style="width: 542px; height: 222px;" /><pre><strong>输入:</strong>head = [1,2,3,4,5], k = 3<strong><br />输出:</strong>[3,2,1,4,5]</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>head = [1,2,3,4,5], k = 1<strong><br />输出:</strong>[1,2,3,4,5]</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>head = [1], k = 1<strong><br />输出:</strong>[1]</pre><ul></ul><p><strong>提示:</strong></p><ul> <li>列表中节点的数量在范围 <code>sz</code> 内</li> <li><code>1 <= sz <= 5000</code></li> <li><code>0 <= Node.val <= 1000</code></li> <li><code>1 <= k <= sz</code></li></ul> <p>给你一个链表,每 <em>k </em>个节点一组进行翻转,请你返回翻转后的链表。</p><p><em>k </em>是一个正整数,它的值小于或等于链表的长度。</p><p>如果节点总数不是 <em>k </em>的整数倍,那么请将最后剩余的节点保持原有顺序。</p><p><strong>进阶:</strong></p><ul> <li>你可以设计一个只使用常数额外空间的算法来解决此问题吗?</li> <li><strong>你不能只是单纯的改变节点内部的值</strong>,而是需要实际进行节点交换。</li></ul><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0025.Reverse%20Nodes%20in%20k-Group/images/reverse_ex1.jpg" style="width: 542px; height: 222px;" /><pre><strong>输入:</strong>head = [1,2,3,4,5], k = 2<strong><br />输出:</strong>[2,1,4,3,5]</pre><p><strong>示例 2:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0025.Reverse%20Nodes%20in%20k-Group/images/reverse_ex2.jpg" style="width: 542px; height: 222px;" /><pre><strong>输入:</strong>head = [1,2,3,4,5], k = 3<strong><br />输出:</strong>[3,2,1,4,5]</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>head = [1,2,3,4,5], k = 1<strong><br />输出:</strong>[1,2,3,4,5]</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>head = [1], k = 1<strong><br />输出:</strong>[1]</pre><ul></ul><p><strong>提示:</strong></p><ul> <li>列表中节点的数量在范围 <code>sz</code> 内</li> <li><code>1 <= sz <= 5000</code></li> <li><code>0 <= Node.val <= 1000</code></li> <li><code>1 <= k <= sz</code></li></ul>
以下程序实现了这一功能,请你填补空白处内容:
```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 ## template
```java ```java
...@@ -62,7 +116,10 @@ class Solution { ...@@ -62,7 +116,10 @@ class Solution {
## 答案 ## 答案
```java ```java
nxt = cur.next;
cur.next = pre;
pre = cur;
cur = nxt;
``` ```
## 选项 ## 选项
...@@ -70,17 +127,26 @@ class Solution { ...@@ -70,17 +127,26 @@ class Solution {
### A ### A
```java ```java
cur.next = pre;
nxt = cur.next;
pre = cur;
cur = nxt;
``` ```
### B ### B
```java ```java
cur.next = pre;
nxt = cur.next;
cur = nxt;
pre = cur;
``` ```
### C ### C
```java ```java
nxt = cur.next;
cur.next = pre;
cur = nxt;
pre = cur;
``` ```
\ No newline at end of file
...@@ -2,6 +2,79 @@ ...@@ -2,6 +2,79 @@
<p><strong>有效数字</strong>(按顺序)可以分成以下几个部分:</p><ol> <li>一个 <strong>小数</strong> 或者 <strong>整数</strong></li> <li>(可选)一个 <code>'e'</code><code>'E'</code> ,后面跟着一个 <strong>整数</strong></li></ol><p><strong>小数</strong>(按顺序)可以分成以下几个部分:</p><ol> <li>(可选)一个符号字符(<code>'+'</code><code>'-'</code></li> <li>下述格式之一: <ol> <li>至少一位数字,后面跟着一个点 <code>'.'</code></li> <li>至少一位数字,后面跟着一个点 <code>'.'</code> ,后面再跟着至少一位数字</li> <li>一个点 <code>'.'</code> ,后面跟着至少一位数字</li> </ol> </li></ol><p><strong>整数</strong>(按顺序)可以分成以下几个部分:</p><ol> <li>(可选)一个符号字符(<code>'+'</code><code>'-'</code></li> <li>至少一位数字</li></ol><p>部分有效数字列举如下:</p><ul> <li><code>["2", "0089", "-0.1", "+3.14", "4.", "-.9", "2e10", "-90E3", "3e+7", "+6e-1", "53.5e93", "-123.456e789"]</code></li></ul><p>部分无效数字列举如下:</p><ul> <li><code>["abc", "1a", "1e", "e3", "99e2.5", "--6", "-+3", "95a54e53"]</code></li></ul><p>给你一个字符串 <code>s</code> ,如果 <code>s</code> 是一个 <strong>有效数字</strong> ,请返回 <code>true</code></p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>s = "0"<strong><br />输出:</strong>true</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>s = "e"<strong><br />输出:</strong>false</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>s = "."<strong><br />输出:</strong>false</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>s = ".1"<strong><br />输出:</strong>true</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>1 <= s.length <= 20</code></li> <li><code>s</code> 仅含英文字母(大写和小写),数字(<code>0-9</code>),加号 <code>'+'</code> ,减号 <code>'-'</code> ,或者点 <code>'.'</code></li></ul> <p><strong>有效数字</strong>(按顺序)可以分成以下几个部分:</p><ol> <li>一个 <strong>小数</strong> 或者 <strong>整数</strong></li> <li>(可选)一个 <code>'e'</code><code>'E'</code> ,后面跟着一个 <strong>整数</strong></li></ol><p><strong>小数</strong>(按顺序)可以分成以下几个部分:</p><ol> <li>(可选)一个符号字符(<code>'+'</code><code>'-'</code></li> <li>下述格式之一: <ol> <li>至少一位数字,后面跟着一个点 <code>'.'</code></li> <li>至少一位数字,后面跟着一个点 <code>'.'</code> ,后面再跟着至少一位数字</li> <li>一个点 <code>'.'</code> ,后面跟着至少一位数字</li> </ol> </li></ol><p><strong>整数</strong>(按顺序)可以分成以下几个部分:</p><ol> <li>(可选)一个符号字符(<code>'+'</code><code>'-'</code></li> <li>至少一位数字</li></ol><p>部分有效数字列举如下:</p><ul> <li><code>["2", "0089", "-0.1", "+3.14", "4.", "-.9", "2e10", "-90E3", "3e+7", "+6e-1", "53.5e93", "-123.456e789"]</code></li></ul><p>部分无效数字列举如下:</p><ul> <li><code>["abc", "1a", "1e", "e3", "99e2.5", "--6", "-+3", "95a54e53"]</code></li></ul><p>给你一个字符串 <code>s</code> ,如果 <code>s</code> 是一个 <strong>有效数字</strong> ,请返回 <code>true</code></p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>s = "0"<strong><br />输出:</strong>true</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>s = "e"<strong><br />输出:</strong>false</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>s = "."<strong><br />输出:</strong>false</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>s = ".1"<strong><br />输出:</strong>true</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>1 <= s.length <= 20</code></li> <li><code>s</code> 仅含英文字母(大写和小写),数字(<code>0-9</code>),加号 <code>'+'</code> ,减号 <code>'-'</code> ,或者点 <code>'.'</code></li></ul>
以下程序实现了这一功能,请你填补空白处内容:
```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 ## template
```java ```java
...@@ -94,7 +167,23 @@ class Solution { ...@@ -94,7 +167,23 @@ class Solution {
## 答案 ## 答案
```java ```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 { ...@@ -102,17 +191,65 @@ class Solution {
### A ### A
```java ```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 ### B
```java ```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 ### C
```java ```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
...@@ -9,6 +9,47 @@ ...@@ -9,6 +9,47 @@
<p><strong>示例 2:</strong></p> <p><strong>示例 2:</strong></p>
<pre><strong>输入: s =</strong> &quot;wordgoodgoodgoodbestword&quot;,<strong> words = </strong>[&quot;word&quot;,&quot;good&quot;,&quot;best&quot;,&quot;word&quot;]<strong><br />输出:</strong>[]</pre> <pre><strong>输入: s =</strong> &quot;wordgoodgoodgoodbestword&quot;,<strong> words = </strong>[&quot;word&quot;,&quot;good&quot;,&quot;best&quot;,&quot;word&quot;]<strong><br />输出:</strong>[]</pre>
以下程序实现了这一功能,请你填补空白处内容:
```java
class Solution {
public List<Integer> findSubstring(String s, String[] words) {
List<Integer> res = new ArrayList<>();
if (s == null || s.length() == 0 || words == null || words.length == 0)
return res;
HashMap<String, Integer> 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<String, Integer> 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 ## template
```java ```java
...@@ -56,7 +97,10 @@ class Solution { ...@@ -56,7 +97,10 @@ class Solution {
## 答案 ## 答案
```java ```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 { ...@@ -64,17 +108,26 @@ class Solution {
### A ### A
```java ```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 ### B
```java ```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 ### C
```java ```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
...@@ -2,6 +2,30 @@ ...@@ -2,6 +2,30 @@
<p>给定两个大小分别为 <code>m</code><code>n</code> 的正序(从小到大)数组 <code>nums1</code> 和 <code>nums2</code>。请你找出并返回这两个正序数组的 <strong>中位数</strong></p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>nums1 = [1,3], nums2 = [2]<strong><br />输出:</strong>2.00000<strong><br />解释:</strong>合并数组 = [1,2,3] ,中位数 2</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>nums1 = [1,2], nums2 = [3,4]<strong><br />输出:</strong>2.50000<strong><br />解释:</strong>合并数组 = [1,2,3,4] ,中位数 (2 + 3) / 2 = 2.5</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>nums1 = [0,0], nums2 = [0,0]<strong><br />输出:</strong>0.00000</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>nums1 = [], nums2 = [1]<strong><br />输出:</strong>1.00000</pre><p><strong>示例 5:</strong></p><pre><strong>输入:</strong>nums1 = [2], nums2 = []<strong><br />输出:</strong>2.00000</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>nums1.length == m</code></li> <li><code>nums2.length == n</code></li> <li><code>0 <= m <= 1000</code></li> <li><code>0 <= n <= 1000</code></li> <li><code>1 <= m + n <= 2000</code></li> <li><code>-10<sup>6</sup> <= nums1[i], nums2[i] <= 10<sup>6</sup></code></li></ul><p> </p><p><strong>进阶:</strong>你能设计一个时间复杂度为 <code>O(log (m+n))</code> 的算法解决此问题吗?</p> <p>给定两个大小分别为 <code>m</code><code>n</code> 的正序(从小到大)数组 <code>nums1</code> 和 <code>nums2</code>。请你找出并返回这两个正序数组的 <strong>中位数</strong></p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>nums1 = [1,3], nums2 = [2]<strong><br />输出:</strong>2.00000<strong><br />解释:</strong>合并数组 = [1,2,3] ,中位数 2</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>nums1 = [1,2], nums2 = [3,4]<strong><br />输出:</strong>2.50000<strong><br />解释:</strong>合并数组 = [1,2,3,4] ,中位数 (2 + 3) / 2 = 2.5</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>nums1 = [0,0], nums2 = [0,0]<strong><br />输出:</strong>0.00000</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>nums1 = [], nums2 = [1]<strong><br />输出:</strong>1.00000</pre><p><strong>示例 5:</strong></p><pre><strong>输入:</strong>nums1 = [2], nums2 = []<strong><br />输出:</strong>2.00000</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>nums1.length == m</code></li> <li><code>nums2.length == n</code></li> <li><code>0 <= m <= 1000</code></li> <li><code>0 <= n <= 1000</code></li> <li><code>1 <= m + n <= 2000</code></li> <li><code>-10<sup>6</sup> <= nums1[i], nums2[i] <= 10<sup>6</sup></code></li></ul><p> </p><p><strong>进阶:</strong>你能设计一个时间复杂度为 <code>O(log (m+n))</code> 的算法解决此问题吗?</p>
以下程序实现了这一功能,请你填补空白处内容:
```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 ## template
```java ```java
...@@ -35,7 +59,13 @@ class Solution { ...@@ -35,7 +59,13 @@ class Solution {
## 答案 ## 答案
```java ```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 { ...@@ -43,17 +73,35 @@ class Solution {
### A ### A
```java ```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 ### B
```java ```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 ### C
```java ```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
...@@ -2,6 +2,43 @@ ...@@ -2,6 +2,43 @@
<p>给你一个字符串 <code>s</code> 、一个字符串 <code>t</code> 。返回 <code>s</code> 中涵盖 <code>t</code> 所有字符的最小子串。如果 <code>s</code> 中不存在涵盖 <code>t</code> 所有字符的子串,则返回空字符串 <code>""</code></p><p><strong>注意:</strong>如果 <code>s</code> 中存在这样的子串,我们保证它是唯一的答案。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>s = "ADOBECODEBANC", t = "ABC"<strong><br />输出:</strong>"BANC"</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>s = "a", t = "a"<strong><br />输出:</strong>"a"</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>1 <= s.length, t.length <= 10<sup>5</sup></code></li> <li><code>s</code><code>t</code> 由英文字母组成</li></ul><p> </p><strong>进阶:</strong>你能设计一个在 <code>o(n)</code> 时间内解决此问题的算法吗? <p>给你一个字符串 <code>s</code> 、一个字符串 <code>t</code> 。返回 <code>s</code> 中涵盖 <code>t</code> 所有字符的最小子串。如果 <code>s</code> 中不存在涵盖 <code>t</code> 所有字符的子串,则返回空字符串 <code>""</code></p><p><strong>注意:</strong>如果 <code>s</code> 中存在这样的子串,我们保证它是唯一的答案。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>s = "ADOBECODEBANC", t = "ABC"<strong><br />输出:</strong>"BANC"</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>s = "a", t = "a"<strong><br />输出:</strong>"a"</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>1 <= s.length, t.length <= 10<sup>5</sup></code></li> <li><code>s</code><code>t</code> 由英文字母组成</li></ul><p> </p><strong>进阶:</strong>你能设计一个在 <code>o(n)</code> 时间内解决此问题的算法吗?
以下程序实现了这一功能,请你填补空白处内容:
```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 ## template
```java ```java
...@@ -47,7 +84,12 @@ public class Min_Win_Sub { ...@@ -47,7 +84,12 @@ public class Min_Win_Sub {
## 答案 ## 答案
```java ```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 { ...@@ -55,17 +97,32 @@ public class Min_Win_Sub {
### A ### A
```java ```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 ### B
```java ```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 ### C
```java ```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
...@@ -2,6 +2,71 @@ ...@@ -2,6 +2,71 @@
<p>给你一个链表数组,每个链表都已经按升序排列。</p><p>请你将所有链表合并到一个升序链表中,返回合并后的链表。</p><p>&nbsp;</p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>lists = [[1,4,5],[1,3,4],[2,6]]<strong><br />输出:</strong>[1,1,2,3,4,4,5,6]<strong><br />解释:</strong>链表数组如下:[ 1-&gt;4-&gt;5, 1-&gt;3-&gt;4, 2-&gt;6]将它们合并到一个有序链表中得到。1-&gt;1-&gt;2-&gt;3-&gt;4-&gt;4-&gt;5-&gt;6</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>lists = []<strong><br />输出:</strong>[]</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>lists = [[]]<strong><br />输出:</strong>[]</pre><p>&nbsp;</p><p><strong>提示:</strong></p><ul> <li><code>k == lists.length</code></li> <li><code>0 &lt;= k &lt;= 10^4</code></li> <li><code>0 &lt;= lists[i].length &lt;= 500</code></li> <li><code>-10^4 &lt;= lists[i][j] &lt;= 10^4</code></li> <li><code>lists[i]</code><strong>升序</strong> 排列</li> <li><code>lists[i].length</code> 的总和不超过 <code>10^4</code></li></ul> <p>给你一个链表数组,每个链表都已经按升序排列。</p><p>请你将所有链表合并到一个升序链表中,返回合并后的链表。</p><p>&nbsp;</p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>lists = [[1,4,5],[1,3,4],[2,6]]<strong><br />输出:</strong>[1,1,2,3,4,4,5,6]<strong><br />解释:</strong>链表数组如下:[ 1-&gt;4-&gt;5, 1-&gt;3-&gt;4, 2-&gt;6]将它们合并到一个有序链表中得到。1-&gt;1-&gt;2-&gt;3-&gt;4-&gt;4-&gt;5-&gt;6</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>lists = []<strong><br />输出:</strong>[]</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>lists = [[]]<strong><br />输出:</strong>[]</pre><p>&nbsp;</p><p><strong>提示:</strong></p><ul> <li><code>k == lists.length</code></li> <li><code>0 &lt;= k &lt;= 10^4</code></li> <li><code>0 &lt;= lists[i].length &lt;= 500</code></li> <li><code>-10^4 &lt;= lists[i][j] &lt;= 10^4</code></li> <li><code>lists[i]</code><strong>升序</strong> 排列</li> <li><code>lists[i].length</code> 的总和不超过 <code>10^4</code></li></ul>
以下程序实现了这一功能,请你填补空白处内容:
```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 ## template
```java ```java
...@@ -71,7 +136,8 @@ class Solution { ...@@ -71,7 +136,8 @@ class Solution {
## 答案 ## 答案
```java ```java
ListNode tmp1 = merge(lists, low, mid);
ListNode tmp2 = merge(lists, mid + 1, high);
``` ```
## 选项 ## 选项
...@@ -79,17 +145,20 @@ class Solution { ...@@ -79,17 +145,20 @@ class Solution {
### A ### A
```java ```java
ListNode tmp1 = merge(lists, low, mid);
ListNode tmp2 = merge(lists, mid, high);
``` ```
### B ### B
```java ```java
ListNode tmp1 = merge(lists, low, mid + 1);
ListNode tmp2 = merge(lists, mid + 1, high);
``` ```
### C ### C
```java ```java
ListNode tmp1 = merge(lists, low, mid + 1);
ListNode tmp2 = merge(lists, mid - 1, high);
``` ```
\ No newline at end of file
...@@ -2,6 +2,28 @@ ...@@ -2,6 +2,28 @@
<p>给出集合 <code>[1,2,3,...,n]</code>,其所有元素共有 <code>n!</code> 种排列。</p><p>按大小顺序列出所有排列情况,并一一标记,当 <code>n = 3</code> 时, 所有排列如下:</p><ol> <li><code>"123"</code></li> <li><code>"132"</code></li> <li><code>"213"</code></li> <li><code>"231"</code></li> <li><code>"312"</code></li> <li><code>"321"</code></li></ol><p>给定 <code>n</code> 和 <code>k</code>,返回第 <code>k</code> 个排列。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>n = 3, k = 3<strong><br />输出:</strong>"213"</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>n = 4, k = 9<strong><br />输出:</strong>"2314"</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>n = 3, k = 1<strong><br />输出:</strong>"123"</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>1 <= n <= 9</code></li> <li><code>1 <= k <= n!</code></li></ul> <p>给出集合 <code>[1,2,3,...,n]</code>,其所有元素共有 <code>n!</code> 种排列。</p><p>按大小顺序列出所有排列情况,并一一标记,当 <code>n = 3</code> 时, 所有排列如下:</p><ol> <li><code>"123"</code></li> <li><code>"132"</code></li> <li><code>"213"</code></li> <li><code>"231"</code></li> <li><code>"312"</code></li> <li><code>"321"</code></li></ol><p>给定 <code>n</code> 和 <code>k</code>,返回第 <code>k</code> 个排列。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>n = 3, k = 3<strong><br />输出:</strong>"213"</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>n = 4, k = 9<strong><br />输出:</strong>"2314"</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>n = 3, k = 1<strong><br />输出:</strong>"123"</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>1 <= n <= 9</code></li> <li><code>1 <= k <= n!</code></li></ul>
以下程序实现了这一功能,请你填补空白处内容:
```java
class Solution {
public String getPermutation(int n, int k) {
StringBuilder sb = new StringBuilder();
List<Integer> 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 ## template
```java ```java
...@@ -31,7 +53,11 @@ class Solution { ...@@ -31,7 +53,11 @@ class Solution {
## 答案 ## 答案
```java ```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 { ...@@ -39,17 +65,29 @@ class Solution {
### A ### A
```java ```java
for (int i = n - 1; i >= 0; --i) {
int index = k / factorials[i];
sb.append(candidates.remove(index));
k = index * factorials[i];
}
``` ```
### B ### B
```java ```java
for (int i = n - 1; i >= 0; --i) {
int index = k / factorials[i];
sb.append(candidates.remove(index));
k += index * factorials[i];
}
``` ```
### C ### C
```java ```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
...@@ -2,6 +2,39 @@ ...@@ -2,6 +2,39 @@
<p>给定一个字符串&nbsp;(<code>s</code>) 和一个字符模式&nbsp;(<code>p</code>) ,实现一个支持&nbsp;<code>&#39;?&#39;</code>&nbsp;&nbsp;<code>&#39;*&#39;</code>&nbsp;的通配符匹配。</p><pre>&#39;?&#39; 可以匹配任何单个字符。&#39;*&#39; 可以匹配任意字符串(包括空字符串)。</pre><p>两个字符串<strong>完全匹配</strong>才算匹配成功。</p><p><strong>说明:</strong></p><ul> <li><code>s</code>&nbsp;可能为空,且只包含从&nbsp;<code>a-z</code>&nbsp;的小写字母。</li> <li><code>p</code>&nbsp;可能为空,且只包含从&nbsp;<code>a-z</code>&nbsp;的小写字母,以及字符&nbsp;<code>?</code>&nbsp;&nbsp;<code>*</code></li></ul><p><strong>示例&nbsp;1:</strong></p><pre><strong>输入:</strong>s = &quot;aa&quot;p = &quot;a&quot;<strong><br />输出:</strong> false<strong><br />解释:</strong> &quot;a&quot; 无法匹配 &quot;aa&quot; 整个字符串。</pre><p><strong>示例&nbsp;2:</strong></p><pre><strong>输入:</strong>s = &quot;aa&quot;p = &quot;*&quot;<strong><br />输出:</strong> true<strong><br />解释:</strong>&nbsp;&#39;*&#39; 可以匹配任意字符串。</pre><p><strong>示例&nbsp;3:</strong></p><pre><strong>输入:</strong>s = &quot;cb&quot;p = &quot;?a&quot;<strong><br />输出:</strong> false<strong><br />解释:</strong>&nbsp;&#39;?&#39; 可以匹配 &#39;c&#39;, 但第二个 &#39;a&#39; 无法匹配 &#39;b&#39;</pre><p><strong>示例&nbsp;4:</strong></p><pre><strong>输入:</strong>s = &quot;adceb&quot;p = &quot;*a*b&quot;<strong><br />输出:</strong> true<strong><br />解释:</strong>&nbsp;第一个 &#39;*&#39; 可以匹配空字符串, 第二个 &#39;*&#39; 可以匹配字符串 &quot;dce&quot;.</pre><p><strong>示例&nbsp;5:</strong></p><pre><strong>输入:</strong>s = &quot;acdcb&quot;p = &quot;a*c?b&quot;<strong><br />输出:</strong> false</pre> <p>给定一个字符串&nbsp;(<code>s</code>) 和一个字符模式&nbsp;(<code>p</code>) ,实现一个支持&nbsp;<code>&#39;?&#39;</code>&nbsp;&nbsp;<code>&#39;*&#39;</code>&nbsp;的通配符匹配。</p><pre>&#39;?&#39; 可以匹配任何单个字符。&#39;*&#39; 可以匹配任意字符串(包括空字符串)。</pre><p>两个字符串<strong>完全匹配</strong>才算匹配成功。</p><p><strong>说明:</strong></p><ul> <li><code>s</code>&nbsp;可能为空,且只包含从&nbsp;<code>a-z</code>&nbsp;的小写字母。</li> <li><code>p</code>&nbsp;可能为空,且只包含从&nbsp;<code>a-z</code>&nbsp;的小写字母,以及字符&nbsp;<code>?</code>&nbsp;&nbsp;<code>*</code></li></ul><p><strong>示例&nbsp;1:</strong></p><pre><strong>输入:</strong>s = &quot;aa&quot;p = &quot;a&quot;<strong><br />输出:</strong> false<strong><br />解释:</strong> &quot;a&quot; 无法匹配 &quot;aa&quot; 整个字符串。</pre><p><strong>示例&nbsp;2:</strong></p><pre><strong>输入:</strong>s = &quot;aa&quot;p = &quot;*&quot;<strong><br />输出:</strong> true<strong><br />解释:</strong>&nbsp;&#39;*&#39; 可以匹配任意字符串。</pre><p><strong>示例&nbsp;3:</strong></p><pre><strong>输入:</strong>s = &quot;cb&quot;p = &quot;?a&quot;<strong><br />输出:</strong> false<strong><br />解释:</strong>&nbsp;&#39;?&#39; 可以匹配 &#39;c&#39;, 但第二个 &#39;a&#39; 无法匹配 &#39;b&#39;</pre><p><strong>示例&nbsp;4:</strong></p><pre><strong>输入:</strong>s = &quot;adceb&quot;p = &quot;*a*b&quot;<strong><br />输出:</strong> true<strong><br />解释:</strong>&nbsp;第一个 &#39;*&#39; 可以匹配空字符串, 第二个 &#39;*&#39; 可以匹配字符串 &quot;dce&quot;.</pre><p><strong>示例&nbsp;5:</strong></p><pre><strong>输入:</strong>s = &quot;acdcb&quot;p = &quot;a*c?b&quot;<strong><br />输出:</strong> false</pre>
以下程序实现了这一功能,请你填补空白处内容:
```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 ## template
```java ```java
...@@ -38,7 +71,7 @@ class Solution { ...@@ -38,7 +71,7 @@ class Solution {
## 答案 ## 答案
```java ```java
value[i][j] = s.charAt(j - 1) == p.charAt(i - 1) && value[i - 1][j - 1];
``` ```
## 选项 ## 选项
...@@ -46,17 +79,17 @@ class Solution { ...@@ -46,17 +79,17 @@ class Solution {
### A ### A
```java ```java
value[i][j] = s.charAt(j - 1) == p.charAt(i - 1) || value[i - 1][j - 1];
``` ```
### B ### B
```java ```java
value[i][j] = s.charAt(j - 1) == p.charAt(i) && value[i - 1][j];
``` ```
### C ### C
```java ```java
value[i][j] = s.charAt(j - 1) == p.charAt(i - 1) && value[i][j - 1];
``` ```
\ No newline at end of file
...@@ -2,6 +2,37 @@ ...@@ -2,6 +2,37 @@
<p>给定 <em>n</em> 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。</p><p> </p><p><strong>示例 1:</strong></p><p><img src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0042.Trapping%20Rain%20Water/images/rainwatertrap.png" style="height: 161px; width: 412px;" /></p><pre><strong>输入:</strong>height = [0,1,0,2,1,0,1,3,2,1,2,1]<strong><br />输出:</strong>6<strong><br />解释:</strong>上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况下,可以接 6 个单位的雨水(蓝色部分表示雨水)。 </pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>height = [4,2,0,3,2,5]<strong><br />输出:</strong>9</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>n == height.length</code></li> <li><code>0 <= n <= 3 * 10<sup>4</sup></code></li> <li><code>0 <= height[i] <= 10<sup>5</sup></code></li></ul> <p>给定 <em>n</em> 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。</p><p> </p><p><strong>示例 1:</strong></p><p><img src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0042.Trapping%20Rain%20Water/images/rainwatertrap.png" style="height: 161px; width: 412px;" /></p><pre><strong>输入:</strong>height = [0,1,0,2,1,0,1,3,2,1,2,1]<strong><br />输出:</strong>6<strong><br />解释:</strong>上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况下,可以接 6 个单位的雨水(蓝色部分表示雨水)。 </pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>height = [4,2,0,3,2,5]<strong><br />输出:</strong>9</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>n == height.length</code></li> <li><code>0 <= n <= 3 * 10<sup>4</sup></code></li> <li><code>0 <= height[i] <= 10<sup>5</sup></code></li></ul>
以下程序实现了这一功能,请你填补空白处内容:
```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 ## template
```java ```java
...@@ -38,7 +69,9 @@ class Solution { ...@@ -38,7 +69,9 @@ class Solution {
## 答案 ## 答案
```java ```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 { ...@@ -46,17 +79,23 @@ class Solution {
### A ### A
```java ```java
for (int i = len - 2; i >= 0; i--) {
right_max[i] = Math.max(height[i], right_max[i]);
}
``` ```
### B ### B
```java ```java
for (int i = len; i >= 0; i--) {
right_max[i] = Math.max(height[i], right_max[i]);
}
``` ```
### C ### C
```java ```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
...@@ -2,6 +2,19 @@ ...@@ -2,6 +2,19 @@
<p>给你一个字符串 <code>s</code> 和一个字符规律 <code>p</code>,请你来实现一个支持 <code>'.'</code> 和 <code>'*'</code> 的正则表达式匹配。</p><ul> <li><code>'.'</code> 匹配任意单个字符</li> <li><code>'*'</code> 匹配零个或多个前面的那一个元素</li></ul><p>所谓匹配,是要涵盖 <strong>整个 </strong>字符串 <code>s</code>的,而不是部分字符串。</p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>s = "aa" p = "a"<strong><br />输出:</strong>false<strong><br />解释:</strong>"a" 无法匹配 "aa" 整个字符串。</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>s = "aa" p = "a*"<strong><br />输出:</strong>true<strong><br />解释:</strong>因为 '*' 代表可以匹配零个或多个前面的那一个元素, 在这里前面的元素就是 'a'。因此,字符串 "aa" 可被视为 'a' 重复了一次。</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>s = "ab" p = ".*"<strong><br />输出:</strong>true<strong><br />解释:</strong>".*" 表示可匹配零个或多个('*')任意字符('.')。</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>s = "aab" p = "c*a*b"<strong><br />输出:</strong>true<strong><br />解释:</strong>因为 '*' 表示零个或多个,这里 'c' 为 0 个, 'a' 被重复一次。因此可以匹配字符串 "aab"。</pre><p><strong>示例 5:</strong></p><pre><strong>输入:</strong>s = "mississippi" p = "mis*is*p*."<strong><br />输出:</strong>false</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>0 <= s.length <= 20</code></li> <li><code>0 <= p.length <= 30</code></li> <li><code>s</code> 可能为空,且只包含从 <code>a-z</code> 的小写字母。</li> <li><code>p</code> 可能为空,且只包含从 <code>a-z</code> 的小写字母,以及字符 <code>.</code> 和 <code>*</code></li> <li>保证每次出现字符 <code>*</code> 时,前面都匹配到有效的字符</li></ul> <p>给你一个字符串 <code>s</code> 和一个字符规律 <code>p</code>,请你来实现一个支持 <code>'.'</code> 和 <code>'*'</code> 的正则表达式匹配。</p><ul> <li><code>'.'</code> 匹配任意单个字符</li> <li><code>'*'</code> 匹配零个或多个前面的那一个元素</li></ul><p>所谓匹配,是要涵盖 <strong>整个 </strong>字符串 <code>s</code>的,而不是部分字符串。</p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>s = "aa" p = "a"<strong><br />输出:</strong>false<strong><br />解释:</strong>"a" 无法匹配 "aa" 整个字符串。</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>s = "aa" p = "a*"<strong><br />输出:</strong>true<strong><br />解释:</strong>因为 '*' 代表可以匹配零个或多个前面的那一个元素, 在这里前面的元素就是 'a'。因此,字符串 "aa" 可被视为 'a' 重复了一次。</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>s = "ab" p = ".*"<strong><br />输出:</strong>true<strong><br />解释:</strong>".*" 表示可匹配零个或多个('*')任意字符('.')。</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>s = "aab" p = "c*a*b"<strong><br />输出:</strong>true<strong><br />解释:</strong>因为 '*' 表示零个或多个,这里 'c' 为 0 个, 'a' 被重复一次。因此可以匹配字符串 "aab"。</pre><p><strong>示例 5:</strong></p><pre><strong>输入:</strong>s = "mississippi" p = "mis*is*p*."<strong><br />输出:</strong>false</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>0 <= s.length <= 20</code></li> <li><code>0 <= p.length <= 30</code></li> <li><code>s</code> 可能为空,且只包含从 <code>a-z</code> 的小写字母。</li> <li><code>p</code> 可能为空,且只包含从 <code>a-z</code> 的小写字母,以及字符 <code>.</code> 和 <code>*</code></li> <li>保证每次出现字符 <code>*</code> 时,前面都匹配到有效的字符</li></ul>
以下程序实现了这一功能,请你填补空白处内容:
```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 ## template
```java ```java
...@@ -22,7 +35,11 @@ class Solution { ...@@ -22,7 +35,11 @@ class Solution {
## 答案 ## 答案
```java ```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 { ...@@ -30,17 +47,29 @@ class Solution {
### A ### A
```java ```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 ### B
```java ```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 ### C
```java ```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
...@@ -2,6 +2,34 @@ ...@@ -2,6 +2,34 @@
<p>给定 <em>n</em> 个非负整数,用来表示柱状图中各个柱子的高度。每个柱子彼此相邻,且宽度为 1 。</p><p>求在该柱状图中,能够勾勒出来的矩形的最大面积。</p><p>&nbsp;</p><p><img src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0084.Largest%20Rectangle%20in%20Histogram/images/histogram.png"></p><p><small>以上是柱状图的示例,其中每个柱子的宽度为 1,给定的高度为&nbsp;<code>[2,1,5,6,2,3]</code>。</small></p><p>&nbsp;</p><p><img src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0084.Largest%20Rectangle%20in%20Histogram/images/histogram_area.png"></p><p><small>图中阴影部分为所能勾勒出的最大矩形面积,其面积为&nbsp;<code>10</code>&nbsp;个单位。</small></p><p>&nbsp;</p><p><strong>示例:</strong></p><pre><strong>输入:</strong> [2,1,5,6,2,3]<strong><br />输出:</strong> 10</pre> <p>给定 <em>n</em> 个非负整数,用来表示柱状图中各个柱子的高度。每个柱子彼此相邻,且宽度为 1 。</p><p>求在该柱状图中,能够勾勒出来的矩形的最大面积。</p><p>&nbsp;</p><p><img src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0084.Largest%20Rectangle%20in%20Histogram/images/histogram.png"></p><p><small>以上是柱状图的示例,其中每个柱子的宽度为 1,给定的高度为&nbsp;<code>[2,1,5,6,2,3]</code>。</small></p><p>&nbsp;</p><p><img src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0084.Largest%20Rectangle%20in%20Histogram/images/histogram_area.png"></p><p><small>图中阴影部分为所能勾勒出的最大矩形面积,其面积为&nbsp;<code>10</code>&nbsp;个单位。</small></p><p>&nbsp;</p><p><strong>示例:</strong></p><pre><strong>输入:</strong> [2,1,5,6,2,3]<strong><br />输出:</strong> 10</pre>
以下程序实现了这一功能,请你填补空白处内容:
```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 ## template
```java ```java
...@@ -35,7 +63,9 @@ class Solution { ...@@ -35,7 +63,9 @@ class Solution {
## 答案 ## 答案
```java ```java
if (heights[j] < nowHeight) {
nowHeight = heights[j];
}
``` ```
## 选项 ## 选项
...@@ -43,17 +73,23 @@ class Solution { ...@@ -43,17 +73,23 @@ class Solution {
### A ### A
```java ```java
if (heights[j] > nowHeight) {
nowHeight = heights[j];
}
``` ```
### B ### B
```java ```java
if (heights[j] >= nowHeight) {
nowHeight = heights[j] + 1;
}
``` ```
### C ### C
```java ```java
if (heights[j] < nowHeight) {
nowHeight = heights[j] + 1;
}
``` ```
\ No newline at end of file
...@@ -2,6 +2,39 @@ ...@@ -2,6 +2,39 @@
<p>给你一个未排序的整数数组 <code>nums</code> ,请你找出其中没有出现的最小的正整数。</p><p> </p><p><strong>进阶:</strong>你可以实现时间复杂度为 <code>O(n)</code> 并且只使用常数级别额外空间的解决方案吗?</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>nums = [1,2,0]<strong><br />输出:</strong>3</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>nums = [3,4,-1,1]<strong><br />输出:</strong>2</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>nums = [7,8,9,11,12]<strong><br />输出:</strong>1</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>0 <= nums.length <= 300</code></li> <li><code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code></li></ul> <p>给你一个未排序的整数数组 <code>nums</code> ,请你找出其中没有出现的最小的正整数。</p><p> </p><p><strong>进阶:</strong>你可以实现时间复杂度为 <code>O(n)</code> 并且只使用常数级别额外空间的解决方案吗?</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>nums = [1,2,0]<strong><br />输出:</strong>3</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>nums = [3,4,-1,1]<strong><br />输出:</strong>2</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>nums = [7,8,9,11,12]<strong><br />输出:</strong>1</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>0 <= nums.length <= 300</code></li> <li><code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code></li></ul>
以下程序实现了这一功能,请你填补空白处内容:
```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 ## template
```java ```java
...@@ -40,7 +73,9 @@ class Solution { ...@@ -40,7 +73,9 @@ class Solution {
## 答案 ## 答案
```java ```java
if ((nums[i] <= 0) || (nums[i] > n)) {
nums[i] = 1;
}
``` ```
## 选项 ## 选项
...@@ -48,17 +83,23 @@ class Solution { ...@@ -48,17 +83,23 @@ class Solution {
### A ### A
```java ```java
if (nums[i] <= 0) {
nums[i] = 1;
}
``` ```
### B ### B
```java ```java
if ((nums[i] <= 0) || (nums[i] > n)) {
nums[i] = 0;
}
``` ```
### C ### C
```java ```java
if (nums[i] > n) {
nums[i] = 1;
}
``` ```
\ No newline at end of file
...@@ -35,6 +35,34 @@ ...@@ -35,6 +35,34 @@
</div> </div>
</div> </div>
以下程序实现了这一功能,请你填补空白处内容:
```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 ## template
```java ```java
...@@ -72,7 +100,13 @@ class Solution { ...@@ -72,7 +100,13 @@ class Solution {
## 答案 ## 答案
```java ```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 { ...@@ -80,17 +114,35 @@ class Solution {
### A ### A
```java ```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 ### B
```java ```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 ### C
```java ```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
...@@ -2,6 +2,55 @@ ...@@ -2,6 +2,55 @@
<p>给定一个仅包含 <code>0</code> 和 <code>1</code> 、大小为 <code>rows x cols</code> 的二维二进制矩阵,找出只包含 <code>1</code> 的最大矩形,并返回其面积。</p><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0085.Maximal%20Rectangle/images/maximal.jpg" style="width: 402px; height: 322px;" /><pre><strong>输入:</strong>matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]<strong><br />输出:</strong>6<strong><br />解释:</strong>最大矩形如上图所示。</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>matrix = []<strong><br />输出:</strong>0</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>matrix = [["0"]]<strong><br />输出:</strong>0</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>matrix = [["1"]]<strong><br />输出:</strong>1</pre><p><strong>示例 5:</strong></p><pre><strong>输入:</strong>matrix = [["0","0"]]<strong><br />输出:</strong>0</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>rows == matrix.length</code></li> <li><code>cols == matrix[0].length</code></li> <li><code>0 <= row, cols <= 200</code></li> <li><code>matrix[i][j]</code> 为 <code>'0'</code> 或 <code>'1'</code></li></ul> <p>给定一个仅包含 <code>0</code> 和 <code>1</code> 、大小为 <code>rows x cols</code> 的二维二进制矩阵,找出只包含 <code>1</code> 的最大矩形,并返回其面积。</p><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0085.Maximal%20Rectangle/images/maximal.jpg" style="width: 402px; height: 322px;" /><pre><strong>输入:</strong>matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]<strong><br />输出:</strong>6<strong><br />解释:</strong>最大矩形如上图所示。</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>matrix = []<strong><br />输出:</strong>0</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>matrix = [["0"]]<strong><br />输出:</strong>0</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>matrix = [["1"]]<strong><br />输出:</strong>1</pre><p><strong>示例 5:</strong></p><pre><strong>输入:</strong>matrix = [["0","0"]]<strong><br />输出:</strong>0</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>rows == matrix.length</code></li> <li><code>cols == matrix[0].length</code></li> <li><code>0 <= row, cols <= 200</code></li> <li><code>matrix[i][j]</code> 为 <code>'0'</code> 或 <code>'1'</code></li></ul>
以下程序实现了这一功能,请你填补空白处内容:
```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 ## template
```java ```java
...@@ -55,7 +104,8 @@ class Solution { ...@@ -55,7 +104,8 @@ class Solution {
## 答案 ## 答案
```java ```java
for (int j = 0; j < n; j++)
res = Math.max(res, (right[j] - left[j]) * height[j]);
``` ```
## 选项 ## 选项
...@@ -63,17 +113,20 @@ class Solution { ...@@ -63,17 +113,20 @@ class Solution {
### A ### A
```java ```java
for (int j = 0; j < n; j++)
res = Math.max(res, (right[j] + left[j]) * height[j]);
``` ```
### B ### B
```java ```java
for (int j = 0; j < n; j++)
res = Math.max(res, (right[j] + left[j]) / 2 * height[j]);
``` ```
### C ### C
```java ```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
...@@ -2,6 +2,52 @@ ...@@ -2,6 +2,52 @@
<p>给你两个单词 <code>word1</code> 和 <code>word2</code>,请你计算出将 <code>word1</code> 转换成 <code>word2</code><em> </em>所使用的最少操作数 。</p><p>你可以对一个单词进行如下三种操作:</p><ul> <li>插入一个字符</li> <li>删除一个字符</li> <li>替换一个字符</li></ul><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>word1 = "horse", word2 = "ros"<strong><br />输出:</strong>3<strong><br />解释:</strong>horse -> rorse (将 'h' 替换为 'r')rorse -> rose (删除 'r')rose -> ros (删除 'e')</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>word1 = "intention", word2 = "execution"<strong><br />输出:</strong>5<strong><br />解释:</strong>intention -> inention (删除 't')inention -> enention (将 'i' 替换为 'e')enention -> exention (将 'n' 替换为 'x')exention -> exection (将 'n' 替换为 'c')exection -> execution (插入 'u')</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>0 <= word1.length, word2.length <= 500</code></li> <li><code>word1</code><code>word2</code> 由小写英文字母组成</li></ul> <p>给你两个单词 <code>word1</code> 和 <code>word2</code>,请你计算出将 <code>word1</code> 转换成 <code>word2</code><em> </em>所使用的最少操作数 。</p><p>你可以对一个单词进行如下三种操作:</p><ul> <li>插入一个字符</li> <li>删除一个字符</li> <li>替换一个字符</li></ul><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>word1 = "horse", word2 = "ros"<strong><br />输出:</strong>3<strong><br />解释:</strong>horse -> rorse (将 'h' 替换为 'r')rorse -> rose (删除 'r')rose -> ros (删除 'e')</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>word1 = "intention", word2 = "execution"<strong><br />输出:</strong>5<strong><br />解释:</strong>intention -> inention (删除 't')inention -> enention (将 'i' 替换为 'e')enention -> exention (将 'n' 替换为 'x')exention -> exection (将 'n' 替换为 'c')exection -> execution (插入 'u')</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>0 <= word1.length, word2.length <= 500</code></li> <li><code>word1</code><code>word2</code> 由小写英文字母组成</li></ul>
以下程序实现了这一功能,请你填补空白处内容:
```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 ## template
```java ```java
...@@ -56,7 +102,11 @@ class Solution { ...@@ -56,7 +102,11 @@ class Solution {
## 答案 ## 答案
```java ```java
if (i == dp.length - 1) {
dp[i] = min;
} else {
left = min;
}
``` ```
## 选项 ## 选项
...@@ -64,17 +114,29 @@ class Solution { ...@@ -64,17 +114,29 @@ class Solution {
### A ### A
```java ```java
if (i == dp.length) {
dp[i] = min;
} else {
left = min;
}
``` ```
### B ### B
```java ```java
if (i == dp.length + 1) {
dp[i] = min;
} else {
left = min;
}
``` ```
### C ### C
```java ```java
if (i == dp.length - 1) {
left = min;
} else {
dp[i] = min;
}
``` ```
\ No newline at end of file
...@@ -64,6 +64,53 @@ ...@@ -64,6 +64,53 @@
</pre> </pre>
</div> </div>
以下程序实现了这一功能,请你填补空白处内容:
```java
class Solution {
public List<String> fullJustify(String[] words, int maxWidth) {
List<String> 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 ## template
```java ```java
...@@ -117,7 +164,10 @@ class Solution { ...@@ -117,7 +164,10 @@ class Solution {
## 答案 ## 答案
```java ```java
if (i < cur) {
sb.append(left > 0 ? leftStr : baseStr);
left--;
}
``` ```
## 选项 ## 选项
...@@ -125,17 +175,26 @@ class Solution { ...@@ -125,17 +175,26 @@ class Solution {
### A ### A
```java ```java
if (i < cur) {
sb.append(left > 0 ? baseStr : leftStr);
left--;
}
``` ```
### B ### B
```java ```java
if (i < cur) {
sb.append(left > 0 ? baseStr : leftStr);
left++;
}
``` ```
### C ### C
```java ```java
if (i < cur) {
sb.append(left >= 0 ? leftStr : baseStr);
left--;
}
``` ```
\ No newline at end of file
...@@ -2,6 +2,39 @@ ...@@ -2,6 +2,39 @@
<p>给你一个只包含 <code>'('</code> 和 <code>')'</code> 的字符串,找出最长有效(格式正确且连续)括号子串的长度。</p><p> </p><div class="original__bRMd"><div><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>s = "(()"<strong><br />输出:</strong>2<strong><br />解释:</strong>最长有效括号子串是 "()"</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>s = ")()())"<strong><br />输出:</strong>4<strong><br />解释:</strong>最长有效括号子串是 "()()"</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>s = ""<strong><br />输出:</strong>0</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>0 <= s.length <= 3 * 10<sup>4</sup></code></li> <li><code>s[i]</code><code>'('</code><code>')'</code></li></ul></div></div> <p>给你一个只包含 <code>'('</code> 和 <code>')'</code> 的字符串,找出最长有效(格式正确且连续)括号子串的长度。</p><p> </p><div class="original__bRMd"><div><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>s = "(()"<strong><br />输出:</strong>2<strong><br />解释:</strong>最长有效括号子串是 "()"</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>s = ")()())"<strong><br />输出:</strong>4<strong><br />解释:</strong>最长有效括号子串是 "()()"</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>s = ""<strong><br />输出:</strong>0</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>0 <= s.length <= 3 * 10<sup>4</sup></code></li> <li><code>s[i]</code><code>'('</code><code>')'</code></li></ul></div></div>
以下程序实现了这一功能,请你填补空白处内容:
```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 ## template
```java ```java
...@@ -41,7 +74,10 @@ class Solution { ...@@ -41,7 +74,10 @@ class Solution {
## 答案 ## 答案
```java ```java
if (s.charAt(i) == '(')
left++;
else
right++;
``` ```
## 选项 ## 选项
...@@ -49,17 +85,26 @@ class Solution { ...@@ -49,17 +85,26 @@ class Solution {
### A ### A
```java ```java
if (s.charAt(i) == ')')
left++;
else
right++;
``` ```
### B ### B
```java ```java
if (s.charAt(i) == '(')
right++;
else
left++;
``` ```
### C ### C
```java ```java
if (s.charAt(i) == ')')
right++;
else
left++;
``` ```
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册