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

add 22 algorithm exercises

上级 b5d5fd20
{
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"keywords": [
"leetcode",
"矩阵置零"
],
"children": [],
"export": [
"solution.json"
],
"title": "矩阵置零"
}
\ No newline at end of file
<p>给定一个 <code><em>m</em> x <em>n</em></code> 的矩阵,如果一个元素为 <strong>0 </strong>,则将其所在行和列的所有元素都设为 <strong>0</strong> 。请使用 <strong><a href="http://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95" target="_blank">原地</a></strong> 算法<strong></strong></p><p><strong>进阶:</strong></p><ul> <li>一个直观的解决方案是使用  <code>O(<em>m</em><em>n</em>)</code> 的额外空间,但这并不是一个好的解决方案。</li> <li>一个简单的改进方案是使用 <code>O(<em>m</em> + <em>n</em>)</code> 的额外空间,但这仍然不是最好的解决方案。</li> <li>你能想出一个仅使用常量空间的解决方案吗?</li></ul><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0073.Set%20Matrix%20Zeroes/images/mat1.jpg" style="width: 450px; height: 169px;" /><pre><strong>输入:</strong>matrix = [[1,1,1],[1,0,1],[1,1,1]]<strong><br />输出:</strong>[[1,0,1],[0,0,0],[1,0,1]]</pre><p><strong>示例 2:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0073.Set%20Matrix%20Zeroes/images/mat2.jpg" style="width: 450px; height: 137px;" /><pre><strong>输入:</strong>matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]]<strong><br />输出:</strong>[[0,0,0,0],[0,4,5,0],[0,3,1,0]]</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[0].length</code></li> <li><code>1 <= m, n <= 200</code></li> <li><code>-2<sup>31</sup> <= matrix[i][j] <= 2<sup>31</sup> - 1</code></li></ul>
\ No newline at end of file
#include <bits/stdc++.h>
using namespace std;
public:
void setZeroes(vector<vector<int>> &matrix)
{
bool bRow = false, bCol = false;
for (int row = 0; row < matrix.size(); row++)
{
for (int col = 0; col < matrix[row].size(); col++)
{
if (matrix[row][col] == 0)
{
if (row == 0)
{
bRow = true;
}
if (col == 0)
{
bCol = true;
}
matrix[0][col] = matrix[row][0] = 0;
}
}
}
for (int row = 1; row < matrix.size(); row++)
{
for (int col = 1; col < matrix[row].size(); col++)
{
if (matrix[0][col] == 0 || matrix[row][0] == 0)
{
matrix[row][col] = 0;
}
}
}
if (bRow)
{
for (auto &m : matrix[0])
{
m = 0;
}
}
if (bCol)
{
for (int row = 0; row < matrix.size(); row++)
{
matrix[row][0] = 0;
}
}
}
}
;
\ No newline at end of file
{
"type": "code_options",
"author": "CSDN.net",
"source": "solution.md",
"exercise_id": "809ee8412dbd47ba967017d142f698e0"
}
\ No newline at end of file
# 矩阵置零
<p>给定一个 <code><em>m</em> x <em>n</em></code> 的矩阵,如果一个元素为 <strong>0 </strong>,则将其所在行和列的所有元素都设为 <strong>0</strong> 。请使用 <strong><a href="http://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95" target="_blank">原地</a></strong> 算法<strong>。</strong></p><p><strong>进阶:</strong></p><ul> <li>一个直观的解决方案是使用  <code>O(<em>m</em><em>n</em>)</code> 的额外空间,但这并不是一个好的解决方案。</li> <li>一个简单的改进方案是使用 <code>O(<em>m</em> + <em>n</em>)</code> 的额外空间,但这仍然不是最好的解决方案。</li> <li>你能想出一个仅使用常量空间的解决方案吗?</li></ul><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0073.Set%20Matrix%20Zeroes/images/mat1.jpg" style="width: 450px; height: 169px;" /><pre><strong>输入:</strong>matrix = [[1,1,1],[1,0,1],[1,1,1]]<strong><br />输出:</strong>[[1,0,1],[0,0,0],[1,0,1]]</pre><p><strong>示例 2:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0073.Set%20Matrix%20Zeroes/images/mat2.jpg" style="width: 450px; height: 137px;" /><pre><strong>输入:</strong>matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]]<strong><br />输出:</strong>[[0,0,0,0],[0,4,5,0],[0,3,1,0]]</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[0].length</code></li> <li><code>1 <= m, n <= 200</code></li> <li><code>-2<sup>31</sup> <= matrix[i][j] <= 2<sup>31</sup> - 1</code></li></ul>
<p>以下错误的选项是?</p>
## aop
### before
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
```cpp
int main()
{
Solution sol;
int a = 3, b = 3;
vector<vector<int>> matrix = vector<vector<int>>(a, vector<int>(b)) = {{1, 1, 1}, {1, 0, 1}, {1, 1, 1}};
sol.setZeroes(matrix);
for (auto i : matrix)
{
for (auto j : i)
cout << j << " ";
cout << endl;
}
return 0;
}
```
## 答案
```cpp
class Solution
{
public:
void setZeroes(vector<vector<int>> &matrix)
{
int row = matrix.size();
int col = matrix[0].size();
vector<int> pos;
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
if (matrix[i][j] == 0)
{
pos.push_back(i);
pos.push_back(j);
}
}
}
for (int i = 1; i < pos.size(); i += 2)
{
int x = pos[i];
int y = pos[i + 1];
for (int j = 0; j < row; j++)
{
matrix[j][y] = 0;
}
for (int k = 0; k < col; k++)
{
matrix[x][k] = 0;
}
}
}
};
```
## 选项
### A
```cpp
class Solution
{
public:
void setZeroes(vector<vector<int>> &matrix)
{
if (matrix.size() <= 0)
return;
int row = -1, col = -1;
int i = 0;
for (i = 0; i < matrix.size(); i++)
{
for (int j = 0; j < matrix[0].size(); j++)
{
if (matrix[i][j] == 0)
{
row = i;
col = j;
break;
}
}
if (row != -1)
break;
}
if (i == matrix.size())
return;
for (int k = i + 1; k < matrix.size(); k++)
{
for (int j = 0; j < matrix[0].size(); j++)
{
if (matrix[k][j] == 0)
{
matrix[row][j] = 0;
matrix[k][col] = 0;
}
}
}
for (i = 0; i < matrix[0].size(); i++)
{
if (i == col)
continue;
if (matrix[row][i] == 0)
for (int j = 0; j < matrix.size(); j++)
matrix[j][i] = 0;
}
for (i = 0; i < matrix.size(); i++)
{
if (i == row)
continue;
if (matrix[i][col] == 0)
for (int j = 0; j < matrix[0].size(); j++)
matrix[i][j] = 0;
}
for (i = 0; i < matrix.size(); i++)
matrix[i][col] = 0;
for (i = 0; i < matrix[0].size(); i++)
matrix[row][i] = 0;
}
};
```
### B
```cpp
class Solution
{
public:
void setZeroes(vector<vector<int>> &matrix)
{
bool row = false, col = false;
int m = matrix.size(), n = matrix[0].size();
for (int i = 0; i < m; ++i)
{
if (matrix[i][0] == 0)
col = true;
}
for (int i = 0; i < n; ++i)
{
if (matrix[0][i] == 0)
row = true;
}
for (int i = 1; i < m; ++i)
for (int j = 1; j < n; ++j)
{
if (matrix[i][j] == 0)
matrix[i][0] = 0, matrix[0][j] = 0;
}
for (int i = 1; i < m; ++i)
for (int j = 1; j < n; ++j)
{
if (matrix[i][0] == 0 || matrix[0][j] == 0)
matrix[i][j] = 0;
}
if (col)
{
for (int i = 0; i < m; ++i)
matrix[i][0] = 0;
}
if (row)
{
for (int i = 0; i < n; ++i)
matrix[0][i] = 0;
}
}
};
```
### C
```cpp
class Solution
{
public:
void setZeroes(vector<vector<int>> &matrix)
{
int m = matrix.size(), n = matrix[0].size();
vector<bool> row(m, false), column(n, false);
int i, j;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
if (matrix[i][j] == 0)
{
row[i] = true;
column[j] = true;
}
}
}
for (i = 0; i < m; i++)
{
if (row[i])
{
for (j = 0; j < n; j++)
matrix[i][j] = 0;
}
}
for (i = 0; i < n; i++)
{
if (column[i])
{
for (j = 0; j < m; j++)
matrix[j][i] = 0;
}
}
}
};
```
{
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"keywords": [
"leetcode",
"搜索二维矩阵"
],
"children": [],
"export": [
"solution.json"
],
"title": "搜索二维矩阵"
}
\ No newline at end of file
<p>编写一个高效的算法来判断 <code>m x n</code> 矩阵中,是否存在一个目标值。该矩阵具有如下特性:</p><ul> <li>每行中的整数从左到右按升序排列。</li> <li>每行的第一个整数大于前一行的最后一个整数。</li></ul><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0074.Search%20a%202D%20Matrix/images/mat.jpg" style="width: 322px; height: 242px;" /><pre><strong>输入:</strong>matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3<strong><br />输出:</strong>true</pre><p><strong>示例 2:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0074.Search%20a%202D%20Matrix/images/mat2.jpg" style="width: 322px; height: 242px;" /><pre><strong>输入:</strong>matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13<strong><br />输出:</strong>false</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[i].length</code></li> <li><code>1 <= m, n <= 100</code></li> <li><code>-10<sup>4</sup> <= matrix[i][j], target <= 10<sup>4</sup></code></li></ul>
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
static int binary_search(int *nums, int len, int target)
{
int low = -1;
int high = len;
while (low + 1 < high)
{
int mid = low + (high - low) / 2;
if (target > nums[mid])
{
low = mid;
}
else
{
high = mid;
}
}
if (high == len || nums[high] != target)
{
return -high - 1;
}
else
{
return high;
}
}
static bool searchMatrix(int **matrix, int matrixRowSize, int matrixColSize, int target)
{
if (matrixRowSize == 0 || matrixColSize == 0)
{
return false;
}
if (target < matrix[0][0] || target > matrix[matrixRowSize - 1][matrixColSize - 1])
{
return false;
}
int row = 0;
int *nums = NULL;
if (matrixRowSize > 0)
{
nums = malloc(matrixRowSize * sizeof(int));
for (row = 0; row < matrixRowSize; row++)
{
nums[row] = matrix[row][0];
}
row = binary_search(nums, matrixRowSize, target);
if (row >= 0)
{
return true;
}
else
{
row = -row - 1;
if (row == 0)
{
return false;
}
else
{
row--;
}
}
}
int col = binary_search(matrix[row], matrixColSize, target);
return col >= 0;
}
int main(int argc, char **argv)
{
int row = 3;
int col = 4;
int **mat = malloc(row * sizeof(int *));
mat[0] = malloc(col * sizeof(int));
mat[0][0] = 1;
mat[0][1] = 3;
mat[0][2] = 5;
mat[0][3] = 7;
mat[1] = malloc(col * sizeof(int));
mat[1][0] = 10;
mat[1][1] = 11;
mat[1][2] = 16;
mat[1][3] = 20;
mat[2] = malloc(col * sizeof(int));
mat[2][0] = 23;
mat[2][1] = 30;
mat[2][2] = 34;
mat[2][3] = 50;
printf("%s\n", searchMatrix(mat, row, col, atoi(argv[1])) ? "true" : "false");
return 0;
}
\ No newline at end of file
{
"type": "code_options",
"author": "CSDN.net",
"source": "solution.md",
"exercise_id": "059c2f45d5374f4584e7cfa2846fa7d8"
}
\ No newline at end of file
# 搜索二维矩阵
<p>编写一个高效的算法来判断 <code>m x n</code> 矩阵中,是否存在一个目标值。该矩阵具有如下特性:</p><ul> <li>每行中的整数从左到右按升序排列。</li> <li>每行的第一个整数大于前一行的最后一个整数。</li></ul><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0074.Search%20a%202D%20Matrix/images/mat.jpg" style="width: 322px; height: 242px;" /><pre><strong>输入:</strong>matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3<strong><br />输出:</strong>true</pre><p><strong>示例 2:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0074.Search%20a%202D%20Matrix/images/mat2.jpg" style="width: 322px; height: 242px;" /><pre><strong>输入:</strong>matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13<strong><br />输出:</strong>false</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[i].length</code></li> <li><code>1 <= m, n <= 100</code></li> <li><code>-10<sup>4</sup> <= matrix[i][j], target <= 10<sup>4</sup></code></li></ul>
<p>以下错误的选项是?</p>
## aop
### before
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
```cpp
int main()
{
Solution sol;
int a = 3, b = 4;
vector<vector<int>> matrix = vector<vector<int>>(a, vector<int>(b)) = {{1, 3, 5, 7}, {10, 11, 16, 20}, {23, 30, 34, 60}};
int target = 3;
bool res;
res = sol.searchMatrix(matrix, target);
cout << res;
return 0;
}
```
## 答案
```cpp
class Solution
{
public:
bool searchMatrix(vector<vector<int>> &matrix, int target)
{
if (matrix.size() == 0 || matrix[0].size() == 0 || target < matrix[0][0] || target > matrix[matrix.size() - 1][matrix[0].size() - 1])
return false;
int up = 0, down = matrix.size() - 1, left = 0, right = matrix[0].size() - 1;
while (up < down)
{
int mid = up + (down - up + 1) / 2;
if (matrix[mid][0] == target)
return true;
else if (matrix[mid][0] > target)
down = mid - 1;
else
up = mid + 1;
}
while (left < right)
{
int mid = left + (right - left + 1) / 2;
if (matrix[up][mid] == target)
return true;
else if (matrix[up][mid] > target)
right = mid - 1;
else
left = mid + 1;
}
if (matrix[up][left] == target)
return true;
return false;
}
};
```
## 选项
### A
```cpp
class Solution
{
public:
bool searchMatrix(vector<vector<int>> &matrix, int target)
{
if (matrix.size() == 0 || matrix[0].size() == 0)
return false;
decltype(matrix.size()) row_front = 0, c_front = 0, row_back = matrix.size(), c_back = matrix[0].size();
while (row_front < row_back)
{
auto k = row_front + (row_back - row_front) / 2;
if (matrix[k][0] == target)
{
return true;
}
else if (matrix[k][0] < target)
{
row_front = k + 1;
}
else if (matrix[k][0] > target)
{
row_back = k;
}
}
if (row_front == 0)
{
return false;
}
decltype(matrix.size()) target_line = row_front - 1;
while (c_front < c_back)
{
auto j = c_front + (c_back - c_front) / 2;
if (matrix[target_line][j] == target)
{
return true;
}
else if (matrix[target_line][j] < target)
{
c_front = j + 1;
}
else if (matrix[target_line][j] > target)
{
c_back = j;
}
}
return false;
}
};
```
### B
```cpp
class Solution
{
public:
bool searchMatrix(vector<vector<int>> &matrix, int target)
{
if (matrix.empty())
return false;
int m = matrix.size();
int n = matrix[0].size();
int left = 0;
int right = m * n - 1;
while (left <= right)
{
int middle = (left + right) / 2;
int middle_element = matrix[middle / n][middle % n];
if (middle_element == target)
return true;
else if (middle_element < target)
left = middle + 1;
else
right = middle - 1;
}
return false;
}
};
```
### C
```cpp
class Solution
{
public:
bool searchMatrix(vector<vector<int>> &matrix, int target)
{
if (matrix.empty())
return false;
int m = matrix.size();
int n = matrix[0].size();
int row = 0, col = n - 1;
while (row < m && col >= 0)
{
if (matrix[row][col] < target && col == n - 1)
row++;
else if (matrix[row][col] == target)
return true;
else
col--;
}
return false;
}
};
```
{
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"keywords": [
"leetcode",
"子集"
],
"children": [],
"export": [
"solution.json"
],
"title": "子集"
}
\ No newline at end of file
<p>给你一个整数数组 <code>nums</code> ,数组中的元素 <strong>互不相同</strong> 。返回该数组所有可能的子集(幂集)。</p><p>解集 <strong>不能</strong> 包含重复的子集。你可以按 <strong>任意顺序</strong> 返回解集。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>nums = [1,2,3]<strong><br />输出:</strong>[[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>nums = [0]<strong><br />输出:</strong>[[],[0]]</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>1 <= nums.length <= 10</code></li> <li><code>-10 <= nums[i] <= 10</code></li> <li><code>nums</code> 中的所有元素 <strong>互不相同</strong></li></ul>
\ No newline at end of file
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
vector<vector<int>> subsets(vector<int> &nums)
{
vector<vector<int>> res;
dfs(nums, 0, res);
return res;
}
private:
vector<int> stack;
void dfs(vector<int> &nums, int start, vector<vector<int>> &res)
{
res.push_back(stack);
for (int i = start; i < nums.size(); i++)
{
stack.push_back(nums[i]);
dfs(nums, i + 1, res);
stack.pop_back();
}
}
};
\ No newline at end of file
{
"type": "code_options",
"author": "CSDN.net",
"source": "solution.md",
"exercise_id": "10a696dc3f174eccab18fed2ea531866"
}
\ No newline at end of file
# 子集
<p>给你一个整数数组 <code>nums</code> ,数组中的元素 <strong>互不相同</strong> 。返回该数组所有可能的子集(幂集)。</p><p>解集 <strong>不能</strong> 包含重复的子集。你可以按 <strong>任意顺序</strong> 返回解集。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>nums = [1,2,3]<strong><br />输出:</strong>[[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>nums = [0]<strong><br />输出:</strong>[[],[0]]</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>1 <= nums.length <= 10</code></li> <li><code>-10 <= nums[i] <= 10</code></li> <li><code>nums</code> 中的所有元素 <strong>互不相同</strong></li></ul>
<p>以下错误的选项是?</p>
## aop
### before
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
```cpp
int main()
{
Solution sol;
vector<int> nums = {1, 2, 3};
vector<vector<int>> res;
res = sol.subsets(nums);
for (auto i : res)
{
for (auto j : i)
cout << j << " ";
cout << endl;
}
return 0;
}
```
## 答案
```cpp
class Solution
{
private:
void subsetss(int numssize, int i, vector<int> &nums, vector<vector<int>> &output, vector<int> &newtemp)
{
if (i >= numssize)
{
output.push_back(newtemp);
return;
}
newtemp.push_back(nums[i]);
subsetss(numssize, i + 1, nums, output, newtemp);
newtemp.pop_back();
}
public:
vector<vector<int>> subsets(vector<int> &nums)
{
int numssize = nums.size();
int i = 0;
vector<vector<int>> output;
vector<int> newtemp;
subsetss(numssize, i, nums, output, newtemp);
return output;
}
};
```
## 选项
### A
```cpp
class Solution
{
public:
vector<vector<int>> subsets(vector<int> &nums)
{
int numssize = nums.size();
int numscount = 1 << numssize;
vector<vector<int>> output;
int i = 0;
while (i < numscount)
{
vector<int> newtemp;
for (int x = 0; x < numssize; x++)
{
if ((1 << x) & i)
{
newtemp.push_back(nums[x]);
}
}
i++;
output.push_back(newtemp);
}
return output;
}
};
```
### B
```cpp
class Solution
{
public:
vector<vector<int>> subsets(vector<int> &nums)
{
vector<vector<int>> output;
vector<int> subset;
int length = nums.size();
output.push_back(subset);
int current = 0;
int output_size;
while (current < length)
{
output_size = output.size();
for (int i = 0; i < output_size; i++)
{
vector<int> newinsert = output[i];
newinsert.insert(newinsert.end(), nums[current]);
output.push_back(newinsert);
}
current++;
}
return output;
}
};
```
### C
```cpp
class Solution
{
public:
vector<vector<int>> subsets(vector<int> &nums)
{
vector<vector<int>> res;
if (nums.empty())
return res;
res.push_back({});
int n = nums.size();
for (int i = 0; i < n; i++)
{
int nRes = res.size();
for (int j = 0; j < nRes; j++)
{
vector<int> temp = res[j];
temp.push_back(nums[i]);
res.push_back(temp);
}
}
return res;
}
};
```
{
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"keywords": [
"leetcode",
"删除有序数组中的重复项 II"
],
"children": [],
"export": [
"solution.json"
],
"title": "删除有序数组中的重复项 II"
}
\ No newline at end of file
<p>给你一个有序数组 <code>nums</code> ,请你<strong><a href="http://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95"
target="_blank"> 原地</a></strong> 删除重复出现的元素,使每个元素 <strong>最多出现两次</strong> ,返回删除后数组的新长度。</p>
<p>不要使用额外的数组空间,你必须在 <strong><a href="https://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95"
target="_blank">原地 </a>修改输入数组 </strong>并在使用 O(1) 额外空间的条件下完成。</p>
<p> </p>
<p><strong>说明:</strong></p>
<p>为什么返回数值是整数,但输出的答案是数组呢?</p>
<p>请注意,输入数组是以<strong>「引用」</strong>方式传递的,这意味着在函数里修改输入数组对于调用者是可见的。</p>
<p>你可以想象内部操作如下:</p>
<pre>
// <strong>nums</strong> 是以“引用”方式传递的。也就是说,不对实参做任何拷贝
int len = removeDuplicates(nums);// 在函数里修改输入数组对于调用者是可见的。
// 根据你的函数返回的长度, 它会打印出数组中<strong> 该长度范围内</strong> 的所有元素。
for (int i = 0; i < len; i++) {
    print(nums[i]);
}</pre>
<p> </p>
<p><strong>示例 1:</strong></p>
<pre><strong>输入:</strong>nums = [1,1,1,2,2,3]<strong><br />输出:</strong>5, nums = [1,1,2,2,3]<strong><br />解释:</strong>函数应返回新长度 length = <strong>5</strong>, 并且原数组的前五个元素被修改为 <strong>1, 1, 2, 2,</strong> <strong>3 </strong>。 不需要考虑数组中超出新长度后面的元素。</pre>
<p><strong>示例 2:</strong></p>
<pre><strong>输入:</strong>nums = [0,0,1,1,1,1,2,3,3]<strong><br />输出:</strong>7, nums = [0,0,1,1,2,3,3]<strong><br />解释:</strong>函数应返回新长度 length = <strong>7</strong>, 并且原数组的前五个元素被修改为 <strong>0</strong>, <strong>0</strong>, <strong>1</strong>, <strong>1</strong>, <strong>2</strong>, <strong>3</strong>, <strong>3 。</strong> 不需要考虑数组中超出新长度后面的元素。</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= nums.length <= 3 * 10<sup>4</sup></code></li>
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>nums</code> 已按升序排列</li>
</ul>
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
static int removeDuplicates(int *nums, int numsSize)
{
if (numsSize == 0)
{
return 0;
}
int i;
int len = 0;
int count = 1;
for (i = 1; i < numsSize; i++)
{
if (nums[len] == nums[i])
{
if (count < 2)
{
count++;
nums[++len] = nums[i];
}
}
else
{
count = 1;
nums[++len] = nums[i];
}
}
return len + 1;
}
int main(int argc, char **argv)
{
int i, count = argc - 1;
int *nums = malloc(count * sizeof(int));
for (i = 0; i < count; i++)
{
nums[i] = atoi(argv[i + 1]);
}
count = removeDuplicates(nums, count);
for (i = 0; i < count; i++)
{
printf("%d ", nums[i]);
}
printf("\n");
}
\ No newline at end of file
{
"type": "code_options",
"author": "CSDN.net",
"source": "solution.md",
"exercise_id": "56068be5e5d24eb9ba7d357dccdb5315"
}
\ No newline at end of file
# 删除有序数组中的重复项 II
<p>给你一个有序数组 <code>nums</code> ,请你<strong><a href="http://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95"
target="_blank"> 原地</a></strong> 删除重复出现的元素,使每个元素 <strong>最多出现两次</strong> ,返回删除后数组的新长度。</p>
<p>不要使用额外的数组空间,你必须在 <strong><a href="https://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95"
target="_blank">原地 </a>修改输入数组 </strong>并在使用 O(1) 额外空间的条件下完成。</p>
<p> </p>
<p><strong>说明:</strong></p>
<p>为什么返回数值是整数,但输出的答案是数组呢?</p>
<p>请注意,输入数组是以<strong>「引用」</strong>方式传递的,这意味着在函数里修改输入数组对于调用者是可见的。</p>
<p>你可以想象内部操作如下:</p>
<pre>
// <strong>nums</strong> 是以“引用”方式传递的。也就是说,不对实参做任何拷贝
int len = removeDuplicates(nums);// 在函数里修改输入数组对于调用者是可见的。
// 根据你的函数返回的长度, 它会打印出数组中<strong> 该长度范围内</strong> 的所有元素。
for (int i = 0; i < len; i++) {
    print(nums[i]);
}</pre>
<p> </p>
<p><strong>示例 1:</strong></p>
<pre><strong>输入:</strong>nums = [1,1,1,2,2,3]<strong><br />输出:</strong>5, nums = [1,1,2,2,3]<strong><br />解释:</strong>函数应返回新长度 length = <strong>5</strong>, 并且原数组的前五个元素被修改为 <strong>1, 1, 2, 2,</strong> <strong>3 </strong>。 不需要考虑数组中超出新长度后面的元素。</pre>
<p><strong>示例 2:</strong></p>
<pre><strong>输入:</strong>nums = [0,0,1,1,1,1,2,3,3]<strong><br />输出:</strong>7, nums = [0,0,1,1,2,3,3]<strong><br />解释:</strong>函数应返回新长度 length = <strong>7</strong>, 并且原数组的前五个元素被修改为 <strong>0</strong>, <strong>0</strong>, <strong>1</strong>, <strong>1</strong>, <strong>2</strong>, <strong>3</strong>, <strong>3 。</strong> 不需要考虑数组中超出新长度后面的元素。</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= nums.length <= 3 * 10<sup>4</sup></code></li>
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>nums</code> 已按升序排列</li>
</ul>
<p>以下错误的选项是?</p>
## aop
### before
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
```cpp
int main()
{
Solution sol;
vector<int> nums = {1, 1, 1, 2, 2, 3};
int res;
res = sol.removeDuplicates(nums);
cout << res << endl;
for (auto i : nums)
cout << i << " ";
return 0;
}
```
## 答案
```cpp
class Solution
{
public:
int removeDuplicates(vector<int> &nums)
{
int n = nums.size();
if (n <= 2)
{
return n;
}
int sp = 1;
for (int fp = 2; fp < n; fp++)
{
if (nums[fp] != nums[sp - 1])
{
nums[++sp] = nums[fp];
}
}
return sp;
}
};
```
## 选项
### A
```cpp
class Solution
{
public:
int removeDuplicates(vector<int> &nums)
{
unordered_map<int, int> maps;
int n = 0;
for (int i = 0; i < nums.size(); ++i)
{
if (maps[nums[i]] < 2)
{
maps[nums[i]]++;
n++;
}
else
{
nums.erase(nums.begin() + i);
--i;
}
}
return n;
}
};
```
### B
```cpp
class Solution
{
public:
int removeDuplicates(vector<int> &nums)
{
if (nums.empty())
return 0;
int len;
len = 1;
for (int i = 1; i < nums.size(); i++)
{
if (i == 1 || nums[i] != nums[len - 2])
{
nums[len] = nums[i];
++len;
}
}
return len;
}
};
```
### C
```cpp
class Solution
{
public:
int removeDuplicates(vector<int> &nums)
{
int ret = nums.size(), quantity = nums[0], num = 0;
for (vector<int>::iterator iter = nums.begin(); iter != nums.end(); iter++)
{
if (*iter == quantity)
{
num++;
if (num > 2)
{
iter = nums.erase(iter);
iter--;
ret--;
num = 2;
}
}
else
{
quantity = *iter;
num = 1;
}
}
return ret;
}
};
```
{
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"keywords": [
"leetcode",
"搜索旋转排序数组 II"
],
"children": [],
"export": [
"solution.json"
],
"title": "搜索旋转排序数组 II"
}
\ No newline at end of file
<p>已知存在一个按非降序排列的整数数组 <code>nums</code> ,数组中的值不必互不相同。</p>
<p>在传递给函数之前,<code>nums</code> 在预先未知的某个下标 <code>k</code><code>0 <= k < nums.length</code>)上进行了 <strong>旋转
</strong>,使数组变为 <code>[nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]]</code>(下标 <strong>从 0
开始</strong> 计数)。例如, <code>[0,1,2,4,4,4,5,6,6,7]</code> 在下标 <code>5</code> 处经旋转后可能变为
<code>[4,5,6,6,7,0,1,2,4,4]</code>
</p>
<p>给你 <strong>旋转后</strong> 的数组 <code>nums</code> 和一个整数 <code>target</code> ,请你编写一个函数来判断给定的目标值是否存在于数组中。如果
<code>nums</code> 中存在这个目标值 <code>target</code> ,则返回 <code>true</code> ,否则返回 <code>false</code>
</p>
<p> </p>
<p><strong>示例 1:</strong></p>
<pre><strong>输入:</strong>nums = [2,5,6,0,0,1,2], target = 0<strong><br />输出:</strong>true</pre>
<p><strong>示例 2:</strong></p>
<pre><strong>输入:</strong>nums = [2,5,6,0,0,1,2], target = 3<strong><br />输出:</strong>false</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5000</code></li>
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
<li>题目数据保证 <code>nums</code> 在预先未知的某个下标上进行了旋转</li>
<li><code>-10<sup>4</sup> <= target <= 10<sup>4</sup></code></li>
</ul>
<p> </p>
<p><strong>进阶:</strong></p>
<ul>
<li>这是 <a
href="https://leetcode-cn.com/problems/search-in-rotated-sorted-array/description/">搜索旋转排序数组</a> 的延伸题目,本题中的 <code>nums</code> 
可能包含重复元素。</li>
<li>这会影响到程序的时间复杂度吗?会有怎样的影响,为什么?</li>
</ul>
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
static bool search(int *nums, int numsSize, int target)
{
int lo = 0;
int hi = numsSize - 1;
while (lo <= hi)
{
int mid = lo + (hi - lo) / 2;
if (nums[mid] == target)
{
return true;
}
if (nums[lo] == nums[mid] && nums[mid] == nums[hi])
{
lo++;
hi--;
}
else if (nums[lo] <= nums[mid])
{
if (nums[lo] <= target && target < nums[mid])
{
hi = mid - 1;
}
else
{
lo = mid + 1;
}
}
else
{
if (nums[mid] < target && target <= nums[hi])
{
lo = mid + 1;
}
else
{
hi = mid - 1;
}
}
}
return false;
}
int main(int argc, char **argv)
{
int i;
int target = atoi(argv[1]);
int size = argc - 2;
int *nums = malloc(size * sizeof(int));
for (i = 0; i < argc - 2; i++)
{
nums[i] = atoi(argv[i + 2]);
}
printf("%d\n", search(nums, size, target));
return 0;
}
\ No newline at end of file
{
"type": "code_options",
"author": "CSDN.net",
"source": "solution.md",
"exercise_id": "d97465cbc9394c6d917bfa83b756cf42"
}
\ No newline at end of file
# 搜索旋转排序数组 II
<p>已知存在一个按非降序排列的整数数组 <code>nums</code> ,数组中的值不必互不相同。</p>
<p>在传递给函数之前,<code>nums</code> 在预先未知的某个下标 <code>k</code><code>0 <= k < nums.length</code>)上进行了 <strong>旋转
</strong>,使数组变为 <code>[nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]]</code>(下标 <strong>从 0
开始</strong> 计数)。例如, <code>[0,1,2,4,4,4,5,6,6,7]</code> 在下标 <code>5</code> 处经旋转后可能变为
<code>[4,5,6,6,7,0,1,2,4,4]</code>
</p>
<p>给你 <strong>旋转后</strong> 的数组 <code>nums</code> 和一个整数 <code>target</code> ,请你编写一个函数来判断给定的目标值是否存在于数组中。如果
<code>nums</code> 中存在这个目标值 <code>target</code> ,则返回 <code>true</code> ,否则返回 <code>false</code>
</p>
<p> </p>
<p><strong>示例 1:</strong></p>
<pre><strong>输入:</strong>nums = [2,5,6,0,0,1,2], target = 0<strong><br />输出:</strong>true</pre>
<p><strong>示例 2:</strong></p>
<pre><strong>输入:</strong>nums = [2,5,6,0,0,1,2], target = 3<strong><br />输出:</strong>false</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5000</code></li>
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
<li>题目数据保证 <code>nums</code> 在预先未知的某个下标上进行了旋转</li>
<li><code>-10<sup>4</sup> <= target <= 10<sup>4</sup></code></li>
</ul>
<p> </p>
<p><strong>进阶:</strong></p>
<ul>
<li>这是 <a
href="https://leetcode-cn.com/problems/search-in-rotated-sorted-array/description/">搜索旋转排序数组</a> 的延伸题目,本题中的 <code>nums</code> 
可能包含重复元素。</li>
<li>这会影响到程序的时间复杂度吗?会有怎样的影响,为什么?</li>
</ul>
<p>以下错误的选项是?</p>
## aop
### before
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
```cpp
int main()
{
Solution sol;
vector<int> nums = {2, 5, 6, 0, 0, 1, 2};
int target = 0;
int res;
res = sol.search(nums, target);
cout << res << endl;
return 0;
}
```
## 答案
```cpp
class Solution
{
public:
bool search(vector<int> &nums, int target)
{
if (nums.empty() || nums.size() == 0)
{
return false;
}
int start = 0;
int end = nums.size() - 1;
int mid;
while (start <= end)
{
mid = start + (end - start) / 2;
if (nums[mid] == target)
{
return true;
}
if (nums[start] < nums[mid])
{
if (nums[mid] > target && nums[start] <= target)
{
end = mid - 1;
}
else
{
start = mid + 1;
}
}
else
{
if (nums[mid] < target && nums[end] >= target)
{
start = mid + 1;
}
else
{
end = mid - 1;
}
}
}
return false;
}
};
```
## 选项
### A
```cpp
class Solution
{
public:
bool search(const vector<int> &nums, int target)
{
int first = 0, last = nums.size();
while (first != last)
{
const int mid = first + (last - first) / 2;
if (nums[mid == target])
{
return true;
}
if (nums[first] < nums[mid])
{
if (nums[first] <= target && target < nums[mid])
{
last = mid;
}
else
{
first = mid + 1;
}
}
else if (nums[first] > nums[mid])
{
if (nums[mid] < target && target <= nums[last - 1])
{
first = mid + 1;
}
else
{
last = mid;
}
}
else
{
first++;
}
}
return false;
}
};
```
### B
```cpp
class Solution
{
public:
bool search(vector<int> &nums, int target)
{
if (nums.empty())
return false;
int i = 0, j = nums.size() - 1, mid;
mid = (i + j) / 2;
if (nums[mid] == target)
return true;
else if (mid != i && nums[i] == nums[mid])
{
i = mid + 1;
while (i <= j && nums[i] == nums[mid])
i++;
if (i == nums.size())
{
i = 0;
j = mid - 1;
while (i <= j && nums[j] == nums[mid])
j--;
}
}
while (i <= j)
{
mid = (i + j) / 2;
if (nums[mid] == target)
return true;
else if (nums[mid] >= nums[i])
{
if (target >= nums[i] && target < nums[mid])
{
j = mid - 1;
while (i <= j && nums[j] == nums[mid])
j--;
}
else
{
i = mid + 1;
while (i <= j && nums[i] == nums[mid])
i++;
}
}
else
{
if (target > nums[mid] && target <= nums[j])
{
i = mid + 1;
while (i <= j && nums[i] == nums[mid])
i++;
}
else
{
j = mid - 1;
while (i <= j && nums[j] == nums[mid])
j--;
}
}
}
return false;
}
};
```
### C
```cpp
class Solution
{
public:
bool search(vector<int> &nums, int &target)
{
int n = nums.size();
if (n == 0)
return false;
int left = 0, right = n - 1;
while (left <= right)
{
int mid = (left + right) / 2;
if (nums[mid] == target)
return true;
else if (nums[mid] < nums[right])
{
if (nums[mid] < target && nums[right] >= target)
left = mid + 1;
else
right = mid - 1;
}
else if (nums[mid] > nums[right])
{
if (nums[left] <= target && nums[mid] > target)
right = mid - 1;
else
left = mid + 1;
}
else
--right;
}
return false;
}
};
```
{
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"keywords": [
"leetcode",
"颜色分类"
],
"children": [],
"export": [
"solution.json"
],
"title": "颜色分类"
}
\ No newline at end of file
<p>给定一个包含红色、白色和蓝色,一共 <code>n</code><em> </em>个元素的数组,<strong><a href="https://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95" target="_blank">原地</a></strong>对它们进行排序,使得相同颜色的元素相邻,并按照红色、白色、蓝色顺序排列。</p><p>此题中,我们使用整数 <code>0</code>、 <code>1</code><code>2</code> 分别表示红色、白色和蓝色。</p><ul></ul><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>nums = [2,0,2,1,1,0]<strong><br />输出:</strong>[0,0,1,1,2,2]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>nums = [2,0,1]<strong><br />输出:</strong>[0,1,2]</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>nums = [0]<strong><br />输出:</strong>[0]</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>nums = [1]<strong><br />输出:</strong>[1]</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>n == nums.length</code></li> <li><code>1 <= n <= 300</code></li> <li><code>nums[i]</code><code>0</code><code>1</code><code>2</code></li></ul><p> </p><p><strong>进阶:</strong></p><ul> <li>你可以不使用代码库中的排序函数来解决这道题吗?</li> <li>你能想出一个仅使用常数空间的一趟扫描算法吗?</li></ul>
\ No newline at end of file
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
void sortColors(vector<int> &nums)
{
int i = 0, j = nums.size() - 1;
while (i < j)
{
if (nums[i] == 0)
{
i++;
continue;
}
if (nums[j] != 0)
{
j--;
continue;
}
swap(nums[i], nums[j]);
}
j = nums.size() - 1;
while (i < j)
{
if (nums[i] == 1)
{
i++;
continue;
}
if (nums[j] != 1)
{
j--;
continue;
}
swap(nums[i], nums[j]);
}
}
};
\ No newline at end of file
{
"type": "code_options",
"author": "CSDN.net",
"source": "solution.md",
"exercise_id": "3597e6b773e84f019cfa733c2a9d3ad2"
}
\ No newline at end of file
# 颜色分类
<p>给定一个包含红色、白色和蓝色,一共 <code>n</code><em> </em>个元素的数组,<strong><a href="https://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95" target="_blank">原地</a></strong>对它们进行排序,使得相同颜色的元素相邻,并按照红色、白色、蓝色顺序排列。</p><p>此题中,我们使用整数 <code>0</code>、 <code>1</code><code>2</code> 分别表示红色、白色和蓝色。</p><ul></ul><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>nums = [2,0,2,1,1,0]<strong><br />输出:</strong>[0,0,1,1,2,2]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>nums = [2,0,1]<strong><br />输出:</strong>[0,1,2]</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>nums = [0]<strong><br />输出:</strong>[0]</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>nums = [1]<strong><br />输出:</strong>[1]</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>n == nums.length</code></li> <li><code>1 <= n <= 300</code></li> <li><code>nums[i]</code><code>0</code><code>1</code><code>2</code></li></ul><p> </p><p><strong>进阶:</strong></p><ul> <li>你可以不使用代码库中的排序函数来解决这道题吗?</li> <li>你能想出一个仅使用常数空间的一趟扫描算法吗?</li></ul>
<p>以下错误的选项是?</p>
## aop
### before
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
```cpp
int main()
{
Solution sol;
int a = 3, b = 4;
vector<int> nums = {2, 0, 2, 1, 1, 0};
sol.sortColors(nums);
for (auto i : nums)
cout << i << " ";
return 0;
}
```
## 答案
```cpp
class Solution
{
public:
void sortColors(vector<int> &nums)
{
int zero = 0, two = nums.size() - 1;
int i = 0;
while (i <= two)
{
if (nums[i] <= 1)
{
int temp = nums[i];
nums[i] = nums[zero];
nums[zero] = temp;
zero++;
i++;
}
else
{
int temp = nums[i];
nums[i] = nums[two];
nums[two] = temp;
two--;
}
}
}
};
```
## 选项
### A
```cpp
class Solution
{
public:
void sortColors(vector<int> &nums)
{
int p0 = 0;
int p1 = 0;
int p2 = nums.size() - 1;
while (p1 <= p2)
{
if (nums[p1] == 0)
swap(nums[p1++], nums[p0++]);
else if (nums[p1] == 2)
swap(nums[p1], nums[p2--]);
else
p1++;
}
}
};
```
### B
```cpp
class Solution
{
public:
void sortColors(vector<int> &nums)
{
int a[3] = {0};
int b[3] = {0, 1, 2};
int num = 0;
for (int i = 0; i < nums.size(); i++)
{
if (nums[i] == 0)
a[0]++;
if (nums[i] == 1)
a[1]++;
if (nums[i] == 2)
a[2]++;
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < a[i]; j++)
{
nums[num] = b[i];
num++;
}
}
}
};
```
### C
```cpp
class Solution
{
public:
void sortColors(vector<int> &nums)
{
int start = 0;
int end = nums.size() - 1;
int temp;
for (int i = 0; i < nums.size(); i++)
{
if (i > end)
break;
if (nums[i] == 2)
{
if (i == end)
continue;
temp = nums[i];
nums[i] = nums[end];
nums[end] = temp;
end--;
i--;
}
else if (nums[i] == 0)
{
if (i == start)
continue;
temp = nums[i];
nums[i] = nums[start];
nums[start] = temp;
start++;
i--;
}
}
}
};
```
{
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"keywords": [
"leetcode",
"删除排序链表中的重复元素 II"
],
"children": [],
"export": [
"solution.json"
],
"title": "删除排序链表中的重复元素 II"
}
\ No newline at end of file
<p>存在一个按升序排列的链表,给你这个链表的头节点 <code>head</code> ,请你删除链表中所有存在数字重复情况的节点,只保留原始链表中 <strong>没有重复出现</strong><em> </em>的数字。</p><p>返回同样按升序排列的结果链表。</p><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0082.Remove%20Duplicates%20from%20Sorted%20List%20II/images/linkedlist1.jpg" style="width: 500px; height: 142px;" /><pre><strong>输入:</strong>head = [1,2,3,3,4,4,5]<strong><br />输出:</strong>[1,2,5]</pre><p><strong>示例 2:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0082.Remove%20Duplicates%20from%20Sorted%20List%20II/images/linkedlist2.jpg" style="width: 500px; height: 205px;" /><pre><strong>输入:</strong>head = [1,1,1,2,3]<strong><br />输出:</strong>[2,3]</pre><p> </p><p><strong>提示:</strong></p><ul> <li>链表中节点数目在范围 <code>[0, 300]</code></li> <li><code>-100 <= Node.val <= 100</code></li> <li>题目数据保证链表已经按升序排列</li></ul>
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
struct ListNode
{
int val;
struct ListNode *next;
};
struct ListNode *deleteDuplicates(struct ListNode *head)
{
struct ListNode dummy;
struct ListNode *p, *q, *prev;
prev = &dummy;
dummy.next = head;
p = q = head;
while (p != NULL)
{
while (q != NULL && q->val == p->val)
{
q = q->next;
}
if (p->next == q)
{
prev = p;
}
else
{
prev->next = q;
}
p = q;
}
return dummy.next;
}
int main(int argc, char **argv)
{
int i;
struct ListNode *head = NULL;
struct ListNode *prev = NULL;
struct ListNode *p;
for (i = 0; i < argc - 1; i++)
{
p = malloc(sizeof(*p));
p->val = atoi(argv[i + 1]);
p->next = NULL;
if (head == NULL)
{
head = p;
prev = head;
}
else
{
prev->next = p;
prev = p;
}
}
p = deleteDuplicates(head);
while (p != NULL)
{
printf("%d ", p->val);
p = p->next;
}
printf("\n");
return 0;
}
\ No newline at end of file
{
"type": "code_options",
"author": "CSDN.net",
"source": "solution.md",
"exercise_id": "c0bd527c63d04abb93a667c1ad319f59"
}
\ No newline at end of file
# 删除排序链表中的重复元素 II
<p>存在一个按升序排列的链表,给你这个链表的头节点 <code>head</code> ,请你删除链表中所有存在数字重复情况的节点,只保留原始链表中 <strong>没有重复出现</strong><em> </em>的数字。</p><p>返回同样按升序排列的结果链表。</p><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0082.Remove%20Duplicates%20from%20Sorted%20List%20II/images/linkedlist1.jpg" style="width: 500px; height: 142px;" /><pre><strong>输入:</strong>head = [1,2,3,3,4,4,5]<strong><br />输出:</strong>[1,2,5]</pre><p><strong>示例 2:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0082.Remove%20Duplicates%20from%20Sorted%20List%20II/images/linkedlist2.jpg" style="width: 500px; height: 205px;" /><pre><strong>输入:</strong>head = [1,1,1,2,3]<strong><br />输出:</strong>[2,3]</pre><p> </p><p><strong>提示:</strong></p><ul> <li>链表中节点数目在范围 <code>[0, 300]</code> 内</li> <li><code>-100 <= Node.val <= 100</code></li> <li>题目数据保证链表已经按升序排列</li></ul>
<p>以下错误的选项是?</p>
## aop
### before
```cpp
#include <bits/stdc++.h>
using namespace std;
struct ListNode
{
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
```
### after
```cpp
```
## 答案
```cpp
class Solution
{
public:
ListNode *deleteDuplicates(ListNode *head)
{
if (!head || !head->next)
return head;
auto dummy = new ListNode(-1);
dummy->next = head;
auto pre = dummy, cur = head;
while (cur && cur->next)
{
if (cur->val != cur->next->val)
{
pre = cur;
cur = cur->next;
}
else
{
while (cur->next && cur->val == cur->next->val)
{
cur = cur->next;
}
}
}
return dummy->next;
}
};
```
## 选项
### A
```cpp
class Solution
{
public:
ListNode *deleteDuplicates(ListNode *head)
{
if (!head || !head->next)
return head;
if (head->val == head->next->val)
{
while (head->next && head->val == head->next->val)
{
head = head->next;
}
return deleteDuplicates(head->next);
}
else
head->next = deleteDuplicates(head->next);
return head;
}
};
```
### B
```cpp
class Solution
{
public:
ListNode *deleteDuplicates(ListNode *head)
{
ListNode *new_head = new ListNode(0);
ListNode *cur = new_head;
ListNode *temp1 = head;
while (temp1)
{
ListNode *temp2 = temp1->next;
if (!temp2)
{
cur->next = temp1;
temp1 = temp1->next;
cur = cur->next;
}
else if (temp2 && temp1->val != temp2->val)
{
cur->next = temp1;
temp1 = temp1->next;
cur = cur->next;
}
else
{
while (temp2 && temp1->val == temp2->val)
{
temp2 = temp2->next;
}
temp1 = temp2;
}
}
cur->next = NULL;
return new_head->next;
}
};
```
### C
```cpp
class Solution
{
public:
ListNode *deleteDuplicates(ListNode *head)
{
if (head == NULL || head->next == NULL)
return head;
ListNode *L = new ListNode(0);
L->next = head;
ListNode *slow = L;
while (slow->next)
{
ListNode *fast = slow->next;
while (fast->next && fast->val == fast->next->val)
{
fast = fast->next;
}
if (slow->next == fast)
slow = slow->next;
else
{
slow->next = fast->next;
}
}
return L->next;
}
};
```
{
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"keywords": [
"leetcode",
"删除排序链表中的重复元素"
],
"children": [],
"export": [
"solution.json"
],
"title": "删除排序链表中的重复元素"
}
\ No newline at end of file
<p>存在一个按升序排列的链表,给你这个链表的头节点 <code>head</code> ,请你删除所有重复的元素,使每个元素 <strong>只出现一次</strong></p><p>返回同样按升序排列的结果链表。</p><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0083.Remove%20Duplicates%20from%20Sorted%20List/images/list1.jpg" style="width: 302px; height: 242px;" /><pre><strong>输入:</strong>head = [1,1,2]<strong><br />输出:</strong>[1,2]</pre><p><strong>示例 2:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0083.Remove%20Duplicates%20from%20Sorted%20List/images/list2.jpg" style="width: 542px; height: 222px;" /><pre><strong>输入:</strong>head = [1,1,2,3,3]<strong><br />输出:</strong>[1,2,3]</pre><p> </p><p><strong>提示:</strong></p><ul> <li>链表中节点数目在范围 <code>[0, 300]</code></li> <li><code>-100 <= Node.val <= 100</code></li> <li>题目数据保证链表已经按升序排列</li></ul>
\ No newline at end of file
#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 *deleteDuplicates(ListNode *head)
{
if (head == nullptr)
{
return nullptr;
}
ListNode *prev = head;
ListNode *p = prev->next;
while (p != nullptr)
{
if (p->val != prev->val)
{
prev->next = p;
prev = p;
}
p = p->next;
}
prev->next = p;
return head;
}
};
\ No newline at end of file
{
"type": "code_options",
"author": "CSDN.net",
"source": "solution.md",
"exercise_id": "125ae9e70c624ccb8f125134e2f31606"
}
\ No newline at end of file
# 删除排序链表中的重复元素
<p>存在一个按升序排列的链表,给你这个链表的头节点 <code>head</code> ,请你删除所有重复的元素,使每个元素 <strong>只出现一次</strong> 。</p><p>返回同样按升序排列的结果链表。</p><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0083.Remove%20Duplicates%20from%20Sorted%20List/images/list1.jpg" style="width: 302px; height: 242px;" /><pre><strong>输入:</strong>head = [1,1,2]<strong><br />输出:</strong>[1,2]</pre><p><strong>示例 2:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0083.Remove%20Duplicates%20from%20Sorted%20List/images/list2.jpg" style="width: 542px; height: 222px;" /><pre><strong>输入:</strong>head = [1,1,2,3,3]<strong><br />输出:</strong>[1,2,3]</pre><p> </p><p><strong>提示:</strong></p><ul> <li>链表中节点数目在范围 <code>[0, 300]</code> 内</li> <li><code>-100 <= Node.val <= 100</code></li> <li>题目数据保证链表已经按升序排列</li></ul>
<p>以下错误的选项是?</p>
## aop
### before
```cpp
#include <bits/stdc++.h>
using namespace std;
struct ListNode
{
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
```
### after
```cpp
```
## 答案
```cpp
class Solution
{
public:
ListNode *deleteDuplicates(ListNode *head)
{
if (!head || !head->next)
return head;
head->next = deleteDuplicates(head->next);
if (head->val != head->next->val)
head = head->next;
return head;
}
};
```
## 选项
### A
```cpp
class Solution
{
public:
ListNode *deleteDuplicates(ListNode *head)
{
if (head == NULL || head->next == NULL)
return head;
ListNode *p = head;
ListNode *q = head->next;
while (p->next != NULL)
{
if (p->val == q->val)
{
if (q->next == NULL)
p->next = NULL;
else
{
p->next = q->next;
q = q->next;
}
}
else
{
p = p->next;
q = q->next;
}
}
return head;
}
};
```
### B
```cpp
class Solution
{
public:
ListNode *deleteDuplicates(ListNode *head)
{
ListNode *cur = head;
while (cur != NULL)
{
ListNode *rear = cur->next;
if (rear == NULL)
return head;
if (cur->val == rear->val)
cur->next = rear->next;
else
cur = cur->next;
}
return head;
}
};
```
### C
```cpp
class Solution
{
public:
ListNode *deleteDuplicates(ListNode *head)
{
if (head == NULL)
return head;
ListNode *ptr{head};
while (ptr->next != NULL)
{
if (ptr->val == ptr->next->val)
{
ListNode *p = ptr->next;
ptr->next = p->next;
delete p;
}
else
{
ptr = ptr->next;
}
}
return head;
}
};
```
{
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"keywords": [
"leetcode",
"分隔链表"
],
"children": [],
"export": [
"solution.json"
],
"title": "分隔链表"
}
\ No newline at end of file
<p>给你一个链表的头节点 <code>head</code> 和一个特定值<em> </em><code>x</code> ,请你对链表进行分隔,使得所有 <strong>小于</strong> <code>x</code> 的节点都出现在 <strong>大于或等于</strong> <code>x</code> 的节点之前。</p><p>你应当 <strong>保留</strong> 两个分区中每个节点的初始相对位置。</p><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0086.Partition%20List/images/partition.jpg" style="width: 662px; height: 222px;" /><pre><strong>输入:</strong>head = [1,4,3,2,5,2], x = 3<strong><br />输出</strong>:[1,2,2,4,3,5]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>head = [2,1], x = 2<strong><br />输出</strong>:[1,2]</pre><p> </p><p><strong>提示:</strong></p><ul> <li>链表中节点的数目在范围 <code>[0, 200]</code></li> <li><code>-100 <= Node.val <= 100</code></li> <li><code>-200 <= x <= 200</code></li></ul>
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
struct ListNode
{
int val;
struct ListNode *next;
};
struct ListNode *partition(struct ListNode *head, int x)
{
struct ListNode dummy;
struct ListNode *prev1 = &dummy, *pivot;
dummy.next = head;
for (pivot = head; pivot != NULL; pivot = pivot->next)
{
if (pivot->val >= x)
{
break;
}
prev1 = pivot;
}
struct ListNode *p = pivot->next;
struct ListNode *prev2 = pivot;
while (p != NULL)
{
if (p->val < x)
{
prev2->next = p->next;
p->next = prev1->next;
prev1->next = p;
prev1 = p;
p = prev2->next;
}
else
{
prev2 = p;
p = p->next;
}
}
return dummy.next;
}
int main(int argc, char **argv)
{
if (argc < 2)
{
fprintf(stderr, "Usage: ./test target n1 n2 n3...\n");
exit(-1);
}
int i, target = atoi(argv[1]);
struct ListNode *head = NULL;
struct ListNode *prev = NULL;
struct ListNode *p;
for (i = 0; i < argc - 2; i++)
{
p = malloc(sizeof(*p));
p->val = atoi(argv[i + 2]);
p->next = NULL;
if (head == NULL)
{
head = p;
prev = head;
}
else
{
prev->next = p;
prev = p;
}
}
p = partition(head, target);
while (p != NULL)
{
printf("%d ", p->val);
p = p->next;
}
printf("\n");
return 0;
}
\ No newline at end of file
{
"type": "code_options",
"author": "CSDN.net",
"source": "solution.md",
"exercise_id": "92a0f69d294d4ce1adf43c603deb67e3"
}
\ No newline at end of file
# 分隔链表
<p>给你一个链表的头节点 <code>head</code> 和一个特定值<em> </em><code>x</code> ,请你对链表进行分隔,使得所有 <strong>小于</strong> <code>x</code> 的节点都出现在 <strong>大于或等于</strong> <code>x</code> 的节点之前。</p><p>你应当 <strong>保留</strong> 两个分区中每个节点的初始相对位置。</p><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0086.Partition%20List/images/partition.jpg" style="width: 662px; height: 222px;" /><pre><strong>输入:</strong>head = [1,4,3,2,5,2], x = 3<strong><br />输出</strong>:[1,2,2,4,3,5]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>head = [2,1], x = 2<strong><br />输出</strong>:[1,2]</pre><p> </p><p><strong>提示:</strong></p><ul> <li>链表中节点的数目在范围 <code>[0, 200]</code> 内</li> <li><code>-100 <= Node.val <= 100</code></li> <li><code>-200 <= x <= 200</code></li></ul>
<p>以下错误的选项是?</p>
## aop
### before
```cpp
#include <bits/stdc++.h>
using namespace std;
struct ListNode
{
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
```
### after
```cpp
```
## 答案
```cpp
class Solution
{
public:
ListNode *partition(ListNode *head, int x)
{
if (!head || !head->next)
return head;
ListNode *p = head;
ListNode *q = head;
ListNode *qq = q;
int flag = 0;
while (q && q->val < x)
{
flag = 1;
p = q;
q = q->next;
}
while (q)
{
if (flag == 0 && q->val < x)
{
qq->next = q->next;
q->next = p;
p = q;
head = p;
q = qq->next;
flag = 1;
}
else if (flag == 1 && q->val < x)
{
qq->next = q->next;
q->next = p->next;
p->next = q;
p = p->next;
q = qq->next;
}
}
return head;
}
};
```
## 选项
### A
```cpp
class Solution
{
public:
ListNode *partition(ListNode *head, int x)
{
ListNode *dummy = new ListNode(-1);
dummy->next = head;
ListNode *pre = dummy, *cur;
while (pre->next && pre->next->val < x)
pre = pre->next;
cur = pre;
while (cur->next)
{
if (cur->next->val < x)
{
ListNode *tmp = cur->next;
cur->next = tmp->next;
tmp->next = pre->next;
pre->next = tmp;
pre = pre->next;
}
else
{
cur = cur->next;
}
}
return dummy->next;
}
};
```
### B
```cpp
class Solution
{
public:
ListNode *partition(ListNode *head, int x)
{
ListNode *lessNode = new ListNode(-1);
ListNode *moreNode = new ListNode(-1);
ListNode *l = lessNode;
ListNode *m = moreNode;
while (head)
{
if (head->val >= x)
{
m->next = head;
m = m->next;
}
else
{
l->next = head;
l = l->next;
}
head = head->next;
}
l->next = moreNode->next;
m->next = NULL;
return lessNode->next;
}
};
```
### C
```cpp
class Solution
{
public:
ListNode *partition(ListNode *head, int x)
{
if (head == NULL || head->next == NULL)
return head;
ListNode *ahead, *p, *after, *p1;
p = head;
while (p && p->val < x)
{
ahead = p;
p = p->next;
}
if (p == head)
ahead = p;
if (p)
after = p->next;
p1 = p;
while (after)
{
if (after->val < x)
{
if (p == head)
{
head = after;
ahead = head;
}
else
{
ahead->next = after;
ahead = after;
}
p1->next = after->next;
}
else
{
p1 = after;
}
after = after->next;
}
if (ahead != p)
ahead->next = p;
return head;
}
};
```
{
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"keywords": [
"leetcode",
"有效数字"
],
"children": [],
"export": [
"solution.json"
],
"title": "有效数字"
}
\ No newline at end of file
<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>
\ No newline at end of file
#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')
{
++s;
if (*s == '+' || *s == '-')
++s;
if_find_num = false;
while (isdigit(*s))
{
if_find_num = true;
++s;
}
}
while (*s == ' ')
++s;
return *s == '\0' && if_find_num == true;
}
int main(int argc, char **argv)
{
if (argc != 2)
{
fprintf(stderr, "Usage: ./test number\n");
exit(-1);
}
printf("%s\n", isNumber(argv[1]) ? "true" : "false");
return 0;
}
\ No newline at end of file
{
"type": "code_options",
"author": "CSDN.net",
"source": "solution.md",
"exercise_id": "55a54ace6aa54e449b7be515edab62a7"
}
\ No newline at end of file
# 有效数字
<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>以下错误的选项是?</p>
## aop
### before
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
```cpp
int main()
{
Solution sol;
string s = ".1";
bool res;
res = sol.isNumber(s);
cout << res;
return 0;
}
```
## 答案
```cpp
class Solution
{
public:
bool isNumber(string s)
{
int len = s.size();
int left = 0, right = len - 1;
bool eExisted = false;
bool dotExisted = false;
bool digitExisited = false;
while (s[left] == ' ')
++left;
while (s[right] == ' ')
--right;
if (left >= right && (s[left] < '0' || s[left] > '9'))
return false;
if (s[left] == '.')
dotExisted = true;
else if (s[left] >= '0' && s[left] <= '9')
digitExisited = true;
else if (s[left] != '+' && s[left] != '-')
return false;
for (int i = left + 1; i <= right - 1; ++i)
{
if (s[i] >= '0' && s[i] <= '9')
digitExisited = true;
else if (s[i] == 'e' || s[i] == 'E')
{
if (!eExisted && s[i - 1] != '+' && s[i - 1] != '-' && digitExisited)
eExisted = true;
else
return false;
}
else if (s[i] == '+' || s[i] == '-')
{
if (s[i - 1] != 'e' && s[i - 1] != 'E')
return false;
}
else if (s[i] == '.')
{
if (!dotExisted && !eExisted)
dotExisted = true;
else
return false;
}
else
return false;
}
if (s[right] >= '0' && s[right] <= '9')
return true;
else
return false;
}
};
```
## 选项
### A
```cpp
class Solution
{
public:
bool isInt(string &s, int &index)
{
if (index < s.size() && (s[index] == '-' || s[index] == '+'))
index++;
return isUnsigned(s, index);
}
bool isUnsigned(string &s, int &index)
{
int pre = index;
while (index < s.size())
{
if (s[index] >= '0' && s[index] <= '9')
index++;
else
break;
}
return index > pre;
}
bool isNumber(string s)
{
if (s.empty())
{
return false;
}
int index = 0;
while (index < s.size() && s[index] == ' ')
{
index++;
}
bool ans = isInt(s, index);
if (index < s.size() && s[index] == '.')
{
index++;
ans = isUnsigned(s, index) || ans;
}
if (index < s.size() && s[index] == 'e')
{
index++;
ans = isInt(s, index) && ans;
}
while (index < s.size() && s[index] == ' ')
{
index++;
}
return ans && index == s.size();
}
};
```
### B
```cpp
class Solution
{
public:
bool isNumber(string s)
{
int tail = s.size() - 1;
int head = 0;
while (tail >= 0)
{
if (s[tail] == ' ')
--tail;
else
break;
}
while (head <= tail)
{
if (s[head] == ' ')
++head;
else
break;
}
if (head > tail)
return false;
s = s.substr(head, tail - head + 1);
int index = s.find('e');
if (index == string::npos)
return judgea(s);
else
return judgea(s.substr(0, index)) && judgeb(s.substr(index + 1));
}
bool judgea(string s)
{
bool have_num = false;
bool have_pointed = false;
for (int i = 0; i < s.size(); i++)
{
if (s[i] >= '0' && s[i] <= '9')
have_num = true;
else if (s[i] == '+' || s[i] == '-')
{
if (i != 0)
return false;
}
else if (s[i] == '.')
{
if (have_pointed)
return false;
have_pointed = true;
}
else
return false;
}
return have_num;
}
bool judgeb(string s)
{
bool have_num = false;
for (int i = 0; i < s.size(); i++)
{
if (s[i] >= '0' && s[i] <= '9')
have_num = true;
else if (s[i] == '-' || s[i] == '+')
{
if (i != 0)
return false;
}
else
return false;
}
return have_num;
}
};
```
### C
```cpp
class Solution
{
public:
bool isNumber(string s)
{
bool num = false, numAfterE = true, dot = false, sign = false, exp = false;
int n = s.size();
for (int i = 0; i < n; ++i)
{
if (s[i] == ' ')
{
if (i < n - 1 && s[i + 1] != ' ' && (num || dot || sign || exp))
return false;
}
else if (s[i] == '+' || s[i] == '-')
{
if (i > 0 && s[i - 1] != 'e' && s[i - 1] != ' ')
return false;
sign = true;
}
else if (s[i] >= '0' && s[i] <= '9')
{
num = true;
numAfterE = true;
}
else if (s[i] == '.')
{
if (dot || exp)
return false;
dot = true;
}
else if (s[i] == 'e')
{
if (exp || !num)
return false;
exp = true;
numAfterE = false;
}
else
return false;
}
return num && numAfterE;
}
};
```
{
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"keywords": [
"leetcode",
"简化路径"
],
"children": [],
"export": [
"solution.json"
],
"title": "简化路径"
}
\ No newline at end of file
<p>给你一个字符串 <code>path</code> ,表示指向某一文件或目录的 Unix 风格 <strong>绝对路径 </strong>(以 <code>'/'</code> 开头),请你将其转化为更加简洁的规范路径。</p><p class="MachineTrans-lang-zh-CN">在 Unix 风格的文件系统中,一个点(<code>.</code>)表示当前目录本身;此外,两个点 (<code>..</code>) 表示将目录切换到上一级(指向父目录);两者都可以是复杂相对路径的组成部分。任意多个连续的斜杠(即,<code>'//'</code>)都被视为单个斜杠 <code>'/'</code> 。 对于此问题,任何其他格式的点(例如,<code>'...'</code>)均被视为文件/目录名称。</p><p>请注意,返回的 <strong>规范路径</strong> 必须遵循下述格式:</p><ul> <li>始终以斜杠 <code>'/'</code> 开头。</li> <li>两个目录名之间必须只有一个斜杠 <code>'/'</code></li> <li>最后一个目录名(如果存在)<strong>不能 </strong><code>'/'</code> 结尾。</li> <li>此外,路径仅包含从根目录到目标文件或目录的路径上的目录(即,不含 <code>'.'</code><code>'..'</code>)。</li></ul><p>返回简化后得到的 <strong>规范路径</strong></p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>path = "/home/"<strong><br />输出:</strong>"/home"<strong><br />解释:</strong>注意,最后一个目录名后面没有斜杠。 </pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>path = "/../"<strong><br />输出:</strong>"/"<strong><br />解释:</strong>从根目录向上一级是不可行的,因为根目录是你可以到达的最高级。</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>path = "/home//foo/"<strong><br />输出:</strong>"/home/foo"<strong><br />解释:</strong>在规范路径中,多个连续斜杠需要用一个斜杠替换。</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>path = "/a/./b/../../c/"<strong><br />输出:</strong>"/c"</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>1 <= path.length <= 3000</code></li> <li><code>path</code> 由英文字母,数字,<code>'.'</code><code>'/'</code><code>'_'</code> 组成。</li> <li><code>path</code> 是一个有效的 Unix 风格绝对路径。</li></ul>
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static char *simplifyPath(char *path)
{
int len = strlen(path);
if (len == 0)
{
return path;
}
char *p = path;
int *indexes = malloc(len * sizeof(int));
int depth = 0;
int name_start = 1;
while (*p != '\0')
{
if (*p == '/')
{
if (p > path && *(p - 1) != '/' && *(p - 1) != '.')
{
name_start = 1;
}
}
else if (*p == '.')
{
if (*(p + 1) == '\0' || *(p + 1) == '/')
{
p += 1;
}
else if (*(p + 1) == '.' && (*(p + 2) == '\0' || *(p + 2) == '/'))
{
if (depth > 0)
{
depth--;
name_start = 1;
}
p += 2;
}
else
{
indexes[depth++] = p - path;
while (*p != '/' && *p != '\0')
{
p++;
}
}
if (*p == '\0')
{
break;
}
}
else
{
if (name_start && depth >= 0)
{
indexes[depth++] = p - path;
name_start = 0;
}
}
p++;
}
int i;
char *result = malloc(len + 1);
char *q = result;
if (depth <= 0)
{
*q++ = '/';
}
else
{
for (i = 0; i < depth; i++)
{
p = path + indexes[i];
*q++ = '/';
while (*p != '/')
{
*q++ = *p++;
}
}
}
*q = '\0';
return result;
}
int main(int argc, char **argv)
{
if (argc != 2)
{
fprintf(stderr, "Usage: ./test path\n");
exit(-1);
}
printf("%s\n", simplifyPath(argv[1]));
return 0;
}
\ No newline at end of file
{
"type": "code_options",
"author": "CSDN.net",
"source": "solution.md",
"exercise_id": "3afec91807524524a677f4194175dafb"
}
\ No newline at end of file
# 简化路径
<p>给你一个字符串 <code>path</code> ,表示指向某一文件或目录的 Unix 风格 <strong>绝对路径 </strong>(以 <code>'/'</code> 开头),请你将其转化为更加简洁的规范路径。</p><p class="MachineTrans-lang-zh-CN">在 Unix 风格的文件系统中,一个点(<code>.</code>)表示当前目录本身;此外,两个点 (<code>..</code>) 表示将目录切换到上一级(指向父目录);两者都可以是复杂相对路径的组成部分。任意多个连续的斜杠(即,<code>'//'</code>)都被视为单个斜杠 <code>'/'</code> 。 对于此问题,任何其他格式的点(例如,<code>'...'</code>)均被视为文件/目录名称。</p><p>请注意,返回的 <strong>规范路径</strong> 必须遵循下述格式:</p><ul> <li>始终以斜杠 <code>'/'</code> 开头。</li> <li>两个目录名之间必须只有一个斜杠 <code>'/'</code></li> <li>最后一个目录名(如果存在)<strong>不能 </strong><code>'/'</code> 结尾。</li> <li>此外,路径仅包含从根目录到目标文件或目录的路径上的目录(即,不含 <code>'.'</code><code>'..'</code>)。</li></ul><p>返回简化后得到的 <strong>规范路径</strong></p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>path = "/home/"<strong><br />输出:</strong>"/home"<strong><br />解释:</strong>注意,最后一个目录名后面没有斜杠。 </pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>path = "/../"<strong><br />输出:</strong>"/"<strong><br />解释:</strong>从根目录向上一级是不可行的,因为根目录是你可以到达的最高级。</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>path = "/home//foo/"<strong><br />输出:</strong>"/home/foo"<strong><br />解释:</strong>在规范路径中,多个连续斜杠需要用一个斜杠替换。</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>path = "/a/./b/../../c/"<strong><br />输出:</strong>"/c"</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>1 <= path.length <= 3000</code></li> <li><code>path</code> 由英文字母,数字,<code>'.'</code><code>'/'</code><code>'_'</code> 组成。</li> <li><code>path</code> 是一个有效的 Unix 风格绝对路径。</li></ul>
<p>以下错误的选项是?</p>
## aop
### before
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
```cpp
int main()
{
Solution sol;
string path = "/a/./b/../../c/";
string res;
res = sol.simplifyPath(path);
cout << res;
return 0;
}
```
## 答案
```cpp
class Solution
{
public:
string names[1500];
int temp = 0, endflag = 0;
void solve(string s, int start, int end, int n)
{
for (int i = start; i < n; i++)
{
if (s[i] == '/' && s[i + 1] != '/')
{
start = i + 1;
}
if (s[i] != '/' && s[i + 1] == '/')
{
end = i;
endflag = 1;
}
else if (s[i] != '/' && i == n - 1)
{
end = i;
endflag = 1;
}
if (endflag == 1)
{
string name = s.substr(start, end - start + 1);
if (name == "..")
{
if (temp > 0)
temp--;
}
else
{
temp = temp + 1;
names[temp] = name;
}
start = end + 1;
endflag = 0;
}
}
}
string simplifyPath(string path)
{
int n = path.length();
names[temp] = "*";
for (int i = n - 1; i >= 0; i++)
{
if (path[i] == '/')
continue;
if (path[i] != '/')
{
path = path.substr(0, i - 0 + 1);
break;
}
}
solve(path, 0, n - 1, n);
string str = "";
if (temp == 0)
{
str = "/";
}
else
{
for (int i = 0; i <= temp - 1; i++)
{
if (i == 0)
{
str += "/";
}
else
{
str = str + names[i] + "/";
}
}
str = str + names[temp];
}
return str;
}
};
```
## 选项
### A
```cpp
class Solution
{
public:
string simplifyPath(string path)
{
if (path == "/")
return "/";
for (int i = 0; i < path.size(); i++)
{
if (path[i] == '/')
path[i] = ' ';
}
vector<string> stack;
istringstream str(path);
string buf;
while (str >> buf)
{
if (buf == ".." && !stack.empty())
stack.pop_back();
else if (buf != "." && buf != "..")
stack.push_back(buf);
}
if (stack.empty())
return "/";
string res;
for (int i = 0; i < stack.size(); i++)
{
res = res + "/" + stack[i];
}
return res;
}
};
```
### B
```cpp
class Solution
{
public:
string simplifyPath(string path)
{
stringstream ss(path);
vector<string> strs;
strs.reserve(20);
string curr;
while (getline(ss, curr, '/'))
{
if (curr != "." && curr != "")
{
if (curr != "..")
{
strs.push_back(curr);
}
else if (!strs.empty())
{
strs.pop_back();
}
}
}
if (!strs.empty())
{
string res = "";
for (string str : strs)
{
res.append("/");
res.append(str);
}
return res;
}
else
{
return "/";
}
}
};
```
### C
```cpp
class Solution
{
public:
string simplifyPath(string path)
{
vector<string> tmp;
int i = 0;
while (i < path.size())
{
while (path[i] == '/')
++i;
if (i == path.size())
break;
int start = i;
while (path[i] != '/' && i < path.size())
++i;
string s = path.substr(start, i - start);
if (s == "..")
{
if (tmp.size() != 0)
tmp.pop_back();
}
else if (s != ".")
tmp.push_back(s);
}
if (tmp.empty())
return "/";
string ans;
for (int i = 0; i < tmp.size(); ++i)
ans += '/' + tmp[i];
return ans;
}
};
```
{
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"keywords": [
"leetcode",
"柱状图中最大的矩形"
],
"children": [],
"export": [
"solution.json"
],
"title": "柱状图中最大的矩形"
}
\ No newline at end of file
<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>
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
static int largestRectangleArea(int *heights, int heightsSize)
{
int *indexes = malloc(heightsSize * sizeof(int));
int *left = malloc(heightsSize * sizeof(int));
int *right = malloc(heightsSize * sizeof(int));
int i, pos = 0;
for (i = 0; i < heightsSize; i++)
{
while (pos > 0 && heights[indexes[pos - 1]] >= heights[i])
{
pos--;
}
left[i] = pos == 0 ? -1 : indexes[pos - 1];
indexes[pos++] = i;
}
pos = 0;
for (i = heightsSize - 1; i >= 0; i--)
{
while (pos > 0 && heights[indexes[pos - 1]] >= heights[i])
{
pos--;
}
right[i] = pos == 0 ? heightsSize : indexes[pos - 1];
indexes[pos++] = i;
}
int max_area = 0;
for (i = 0; i < heightsSize; i++)
{
int area = heights[i] * (right[i] - left[i] - 1);
max_area = area > max_area ? area : max_area;
}
return max_area;
}
int main(void)
{
int nums[] = {2, 1, 5, 6, 2, 3};
int count = sizeof(nums) / sizeof(*nums);
printf("%d\n", largestRectangleArea(nums, count));
return 0;
}
\ No newline at end of file
{
"type": "code_options",
"author": "CSDN.net",
"source": "solution.md",
"exercise_id": "869c530dafc545fcbc074e8eb379b2cc"
}
\ No newline at end of file
# 柱状图中最大的矩形
<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>以下错误的选项是?</p>
## aop
### before
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
```cpp
int main()
{
Solution sol;
vector<int> heights = {2, 1, 5, 6, 2, 3};
int res;
res = sol.largestRectangleArea(heights);
cout << res << endl;
return 0;
}
```
## 答案
```cpp
class Solution
{
public:
int largestRectangleArea(vector<int> &heights)
{
if (heights.empty())
return 0;
stack<int> st;
heights.push_back(0);
int res = 0;
for (int i = 0; i < heights.size(); i++)
{
while (!st.empty() && heights[i] < heights[st.top()])
{
int curHeight = heights[st.top()];
st.pop();
int width = st.empty() ? i : i - st.top();
if (width * curHeight > res)
res = width * curHeight;
}
st.push(i);
}
return res;
}
};
```
## 选项
### A
```cpp
class Solution
{
public:
int largestRectangleArea(vector<int> &heights)
{
int sz = heights.size();
int ma = 0;
for (int i = 0; i < sz; i++)
{
int len = 1;
int hei = heights[i];
int sta = i - 1, en = i + 1;
while (sta >= 0 && heights[sta] >= hei)
{
len++;
sta--;
}
while (en < sz && heights[en] >= hei)
{
len++;
en++;
}
ma = max(ma, len * hei);
}
return ma;
}
};
```
### B
```cpp
class Solution
{
public:
int largestRectangleArea(vector<int> &heights)
{
int n = heights.size();
vector<int> left(n), right(n, n);
stack<int> mono_stack;
for (int i = 0; i < n; ++i)
{
while (!mono_stack.empty() && heights[mono_stack.top()] >= heights[i])
{
right[mono_stack.top()] = i;
mono_stack.pop();
}
left[i] = (mono_stack.empty() ? -1 : mono_stack.top());
mono_stack.push(i);
}
int ans = 0;
for (int i = 0; i < n; ++i)
{
ans = max(ans, (right[i] - left[i] - 1) * heights[i]);
}
return ans;
}
};
```
### C
```cpp
class Solution
{
public:
int largestRectangleArea(vector<int> &heights)
{
int n = heights.size();
stack<int> index;
int area = 0;
for (int i = 0; i < heights.size(); i++)
{
if (index.empty() || heights[index.top()] < heights[i])
index.push(i);
else
{
while (!index.empty() && heights[index.top()] >= heights[i])
{
int tmp = index.top();
index.pop();
int length = 0;
if (index.empty())
length = i;
else
length = i - index.top() - 1;
area = max(area, length * heights[tmp]);
}
index.push(i);
}
}
while (!index.empty())
{
int tmp = index.top();
index.pop();
int length = 0;
if (index.empty())
length = n;
else
length = n - index.top() - 1;
area = max(area, length * heights[tmp]);
}
return area;
}
};
```
{
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"keywords": [
"leetcode",
"最小路径和"
],
"children": [],
"export": [
"solution.json"
],
"title": "最小路径和"
}
\ No newline at end of file
<p>给定一个包含非负整数的 <code><em>m</em> x <em>n</em></code> 网格 <code>grid</code> ,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小。</p><p><strong>说明:</strong>每次只能向下或者向右移动一步。</p><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0064.Minimum%20Path%20Sum/images/minpath.jpg" style="width: 242px; height: 242px;" /><pre><strong>输入:</strong>grid = [[1,3,1],[1,5,1],[4,2,1]]<strong><br />输出:</strong>7<strong><br />解释:</strong>因为路径 1→3→1→1→1 的总和最小。</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>grid = [[1,2,3],[4,5,6]]<strong><br />输出:</strong>12</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 <= m, n <= 200</code></li> <li><code>0 <= grid[i][j] <= 100</code></li></ul>
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static inline int min(int a, int b)
{
return a < b ? a : b;
}
int minPathSum(int **grid, int gridRowSize, int gridColSize)
{
int i, j;
int **dp = malloc(gridRowSize * sizeof(int *));
for (i = 0; i < gridRowSize; i++)
{
dp[i] = malloc(gridColSize * sizeof(int));
}
dp[0][0] = grid[0][0];
int sum = dp[0][0];
for (i = 1; i < gridRowSize; i++)
{
sum += grid[i][0];
dp[i][0] = sum;
}
sum = dp[0][0];
for (i = 1; i < gridColSize; i++)
{
sum += grid[0][i];
dp[0][i] = sum;
}
for (i = 1; i < gridRowSize; i++)
{
for (j = 1; j < gridColSize; j++)
{
dp[i][j] = grid[i][j] + min(dp[i - 1][j], dp[i][j - 1]);
}
}
return dp[gridRowSize - 1][gridColSize - 1];
}
int main(int argc, char **argv)
{
int i, j;
int row = argc - 1;
int col = strlen(argv[1]);
int **grid = malloc(row * sizeof(int *));
for (i = 0; i < row; i++)
{
grid[i] = malloc(col * sizeof(int));
for (j = 0; j < col; j++)
{
grid[i][j] = argv[i + 1][j] - '0';
printf("%d ", grid[i][j]);
}
printf("\n");
}
printf("%d\n", minPathSum(grid, row, col));
return 0;
}
\ No newline at end of file
{
"type": "code_options",
"author": "CSDN.net",
"source": "solution.md",
"exercise_id": "a9c26e3672b44267a074bf551bc8b987"
}
\ No newline at end of file
# 最小路径和
<p>给定一个包含非负整数的 <code><em>m</em> x <em>n</em></code> 网格 <code>grid</code> ,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小。</p><p><strong>说明:</strong>每次只能向下或者向右移动一步。</p><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0064.Minimum%20Path%20Sum/images/minpath.jpg" style="width: 242px; height: 242px;" /><pre><strong>输入:</strong>grid = [[1,3,1],[1,5,1],[4,2,1]]<strong><br />输出:</strong>7<strong><br />解释:</strong>因为路径 1→3→1→1→1 的总和最小。</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>grid = [[1,2,3],[4,5,6]]<strong><br />输出:</strong>12</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 <= m, n <= 200</code></li> <li><code>0 <= grid[i][j] <= 100</code></li></ul>
<p>以下错误的选项是?</p>
## aop
### before
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
```cpp
int main()
{
Solution sol;
int a = 3, b = 3;
vector<vector<int>> grid = vector<vector<int>>(a, vector<int>(b)) = {{1, 3, 1}, {1, 5, 1}, {4, 2, 1}};
int res;
res = sol.minPathSum(grid);
cout << res;
return 0;
}
```
## 答案
```cpp
class Solution
{
private:
int m, n;
int memo[100][100];
public:
int minPathSum(vector<vector<int>> &grid)
{
m = grid.size(), n = grid[0].size();
for (int i = 0; i < m; i++)
{
memset(memo[i], -1, sizeof(int) * n);
}
return dfs(grid, 0, 0);
}
int dfs(vector<vector<int>> &grid, int r, int c)
{
if (r < 0 || r >= m || c < 0 || c >= n)
return 1000000;
if (memo[r][c] != -1)
return memo[r][c];
if (r == m - 1 && c == n - 1)
{
memo[r][c] = grid[m][n - 1];
return memo[r][c];
}
int right = dfs(grid, r, c + 1);
int down = dfs(grid, r + 1, c);
memo[r][c] = min(right, down) + grid[r][c];
return memo[r][c];
}
};
```
## 选项
### A
```cpp
class Solution
{
public:
int minPathSum(vector<vector<int>> &grid)
{
int row = grid.size();
int col = grid[0].size();
vector<int> f(col, 0);
for (int i = 0; i < row; ++i)
{
f[0] = f[0] + grid[i][0];
for (int j = 1; j < col; ++j)
{
if (i == 0)
f[j] = f[j - 1] + grid[i][j];
else
f[j] = min(f[j - 1], f[j]) + grid[i][j];
}
}
return f[col - 1];
}
};
```
### B
```cpp
class Solution
{
public:
int minPathSum(vector<vector<int>> &grid)
{
int row = grid.size();
int column = grid[0].size();
for (int i = 1; i < column; ++i)
{
grid[0][i] = grid[0][i - 1] + grid[0][i];
}
for (int i = 1; i < row; ++i)
{
grid[i][0] = grid[i - 1][0] + grid[i][0];
}
for (int i = 1; i < row; ++i)
{
for (int j = 1; j < column; ++j)
{
int temp = grid[i - 1][j] > grid[i][j - 1] ? grid[i][j - 1] : grid[i - 1][j];
grid[i][j] = grid[i][j] + temp;
}
}
return grid[row - 1][column - 1];
}
};
```
### C
```cpp
class Solution
{
public:
int minPathSum(vector<vector<int>> &grid)
{
if (grid.size() == 0)
return 0;
int m = grid.size();
int n = grid[0].size();
vector<vector<int>> m_memo = vector<vector<int>>(m + 1, vector<int>(n + 1, 0));
for (int i = n - 1; i >= 0; --i)
m_memo[m - 1][i] = grid[m - 1][i] + m_memo[m - 1][i + 1];
for (int j = m - 1; j >= 0; --j)
m_memo[j][n - 1] = grid[j][n - 1] + m_memo[j + 1][n - 1];
for (int i = m - 2; i >= 0; --i)
{
for (int j = n - 2; j >= 0; --j)
{
m_memo[i][j] = grid[i][j] + min(m_memo[i][j + 1], m_memo[i + 1][j]);
}
}
return m_memo[0][0];
}
};
```
{
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"keywords": [
"leetcode",
"爬楼梯"
],
"children": [],
"export": [
"solution.json"
],
"title": "爬楼梯"
}
\ No newline at end of file
<p>假设你正在爬楼梯。需要 <em>n</em>&nbsp;阶你才能到达楼顶。</p><p>每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢?</p><p><strong>注意:</strong>给定 <em>n</em> 是一个正整数。</p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong> 2<strong><br />输出:</strong> 2<strong><br />解释:</strong> 有两种方法可以爬到楼顶。1. 1 阶 + 1 阶2. 2 阶</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong> 3<strong><br />输出:</strong> 3<strong><br />解释:</strong> 有三种方法可以爬到楼顶。1. 1 阶 + 1 阶 + 1 阶2. 1 阶 + 2 阶3. 2 阶 + 1 阶</pre>
\ No newline at end of file
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
int climbStairs(int n)
{
int a = 1;
int b = 2;
int c = 0;
for (int i = 3; i <= n; i++)
{
c = a + b;
a = b;
b = c;
}
return n == 1 ? a : (n == 2 ? b : c);
}
};
\ No newline at end of file
{
"type": "code_options",
"author": "CSDN.net",
"source": "solution.md",
"exercise_id": "e208713d1240406dade9a52e0d45fda2"
}
\ No newline at end of file
# 爬楼梯
<p>假设你正在爬楼梯。需要 <em>n</em>&nbsp;阶你才能到达楼顶。</p><p>每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢?</p><p><strong>注意:</strong>给定 <em>n</em> 是一个正整数。</p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong> 2<strong><br />输出:</strong> 2<strong><br />解释:</strong> 有两种方法可以爬到楼顶。1. 1 阶 + 1 阶2. 2 阶</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong> 3<strong><br />输出:</strong> 3<strong><br />解释:</strong> 有三种方法可以爬到楼顶。1. 1 阶 + 1 阶 + 1 阶2. 1 阶 + 2 阶3. 2 阶 + 1 阶</pre>
<p>以下错误的选项是?</p>
## aop
### before
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
```cpp
int main()
{
Solution sol;
int x = 3;
int res;
res = sol.climbStairs(x);
cout << res;
return 0;
}
```
## 答案
```cpp
class Solution
{
public:
int climbStairs(int n)
{
double sqrt5 = sqrt(5);
double fibn = pow((1 + sqrt5) / 2, n) - pow((1 - sqrt5) / 2, n + 1);
return (int)(fibn / sqrt5);
}
};
```
## 选项
### A
```cpp
class Solution
{
public:
int climbStairs(int n)
{
vector<int> s;
s.push_back(1);
s.push_back(2);
if (n == 1)
return 1;
if (n == 2)
return 2;
for (int i = 2; i < n; i++)
{
s.push_back(s[i - 1] + s[i - 2]);
}
return s[n - 1];
}
};
```
### B
```cpp
class Solution
{
public:
int climbStairs(int n)
{
vector<int> res(n + 1, 0);
res[1] = 1;
res[2] = 2;
for (int i = 3; i <= n; i++)
res[i] = res[i - 1] + res[i - 2];
return res[n];
}
};
```
### C
```cpp
class Solution
{
public:
int climbStairs(int n)
{
if (n == 1)
{
return 1;
}
int first = 1;
int second = 2;
for (int i = 3; i <= n; i++)
{
int third = first + second;
first = second;
second = third;
}
return second;
}
};
```
{
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"keywords": [
"leetcode",
"编辑距离"
],
"children": [],
"export": [
"solution.json"
],
"title": "编辑距离"
}
\ No newline at end of file
<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>
\ No newline at end of file
#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];
if (word1[i - 1] == word2[j - 1])
{
dp[j] = left_up;
}
else
{
dp[j] = 1 + min(left_up, min(up, dp[j - 1]));
}
left_up = up;
}
}
return dp[l2];
}
};
\ No newline at end of file
{
"type": "code_options",
"author": "CSDN.net",
"source": "solution.md",
"exercise_id": "38e847ac1ca048a8b39a99e90b9c788e"
}
\ No newline at end of file
# 编辑距离
<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>以下错误的选项是?</p>
## aop
### before
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
```cpp
int main()
{
Solution sol;
string word1 = "horse";
string word2 = "ros";
int res;
res = sol.minDistance(word1, word2);
cout << res;
return 0;
}
```
## 答案
```cpp
class Solution
{
public:
int minDistance(string word1, string word2)
{
int len1 = word1.size();
int len2 = word2.size();
int **dp = new int *[len1 + 1];
for (int i = 0; i < len1 + 1; i++)
dp[i] = new int[len2 + 1];
for (int i = 0; i < len1 + 1; i++)
dp[i - 1][0] = i;
for (int i = 1; i < len2 + 1; i++)
dp[0][i - 1] = i;
for (int i = 1; i < len1 + 1; i++)
{
for (int j = 1; j < len2 + 1; j++)
{
if (word1[i - 1] == word2[j - 1])
dp[i][j] = dp[i - 1][j - 1];
else
dp[i][j] = (min(dp[i - 1][j - 1], min(dp[i - 1][j], dp[i][j - 1])) + 1);
}
}
return dp[len1][len2];
}
};
```
## 选项
### A
```cpp
class Solution
{
public:
int minDistance(string word1, string word2)
{
int m = word1.size(), n = word2.size();
if (m == 0)
return n;
if (n == 0)
return m;
int dp[m][n];
bool w1 = false, w2 = false;
if (word1[0] == word2[0])
{
w1 = true;
w2 = true;
dp[0][0] = 0;
}
else
dp[0][0] = 1;
for (int i = 1; i < m; i++)
{
if (!w1 && word1[i] == word2[0])
{
w1 = true;
dp[i][0] = dp[i - 1][0];
}
else
dp[i][0] = dp[i - 1][0] + 1;
}
for (int j = 1; j < n; j++)
{
if (!w2 && word1[0] == word2[j])
{
w2 = true;
dp[0][j] = dp[0][j - 1];
}
else
dp[0][j] = dp[0][j - 1] + 1;
}
for (int i = 1; i < m; i++)
for (int j = 1; j < n; j++)
if (word1[i] == word2[j])
dp[i][j] = min(min(dp[i][j - 1], dp[i - 1][j]) + 1, dp[i - 1][j - 1]);
else
dp[i][j] = min(min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;
return dp[m - 1][n - 1];
}
};
```
### B
```cpp
class Solution
{
public:
int minDistance(string word1, string word2)
{
int n = word1.size();
int m = word2.size();
if (n * m == 0)
{
return n + m;
}
int d[n + 1][m + 1];
for (int i = 0; i < n + 1; ++i)
{
d[i][0] = i;
}
for (int i = 0; i < m + 1; ++i)
{
d[0][i] = i;
}
for (int i = 1; i < n + 1; ++i)
{
for (int j = 1; j < m + 1; ++j)
{
int left = d[i - 1][j] + 1;
int down = d[i][j - 1] + 1;
int left_down = d[i - 1][j - 1];
if (word1[i - 1] != word2[j - 1])
{
left_down += 1;
}
d[i][j] = min(left, min(down, left_down));
}
}
return d[n][m];
}
};
```
### C
```cpp
class Solution
{
public:
int minDistance(string word1, string word2)
{
int m = word1.size(), n = word2.size();
vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0));
for (int i = 1; i <= n; ++i)
dp[0][i] = dp[0][i - 1] + 1;
for (int i = 1; i <= m; ++i)
dp[i][0] = dp[i - 1][0] + 1;
for (int i = 1; i <= m; ++i)
{
for (int j = 1; j <= n; ++j)
{
if (word1[i - 1] == word2[j - 1])
dp[i][j] = dp[i - 1][j - 1];
else
dp[i][j] = min(min(dp[i - 1][j - 1], dp[i - 1][j]), dp[i][j - 1]) + 1;
}
}
return dp[m][n];
}
};
```
{
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"keywords": [
"leetcode",
"最大矩形"
],
"children": [],
"export": [
"solution.json"
],
"title": "最大矩形"
}
\ No newline at end of file
<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>
\ No newline at end of file
#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--)
{
while (pos > 0 && heights[indexes[pos - 1]] >= heights[i])
{
pos--;
}
rhist[i] = pos == 0 ? size : indexes[pos - 1];
indexes[pos++] = i;
}
int max_area = 0;
for (i = 0; i < size; i++)
{
int area = heights[i] * (rhist[i] - lhist[i] - 1);
max_area = max(area, max_area);
}
return max_area;
}
static int maximalRectangle(char **matrix, int matrixRowSize, int matrixColSize)
{
int i, j, max_area = 0;
int *heights = malloc(matrixColSize * sizeof(int));
memset(heights, 0, matrixColSize * sizeof(int));
for (i = 0; i < matrixRowSize; i++)
{
for (j = 0; j < matrixColSize; j++)
{
heights[j] = matrix[i][j] == '1' ? heights[j] + 1 : 0;
}
max_area = max(max_area, area_calc(heights, matrixColSize));
}
return max_area;
}
int main(int argc, char **argv)
{
if (argc < 2)
{
fprintf(stderr, "Usage: ./test row1 row2...\n");
exit(-1);
}
int i, j;
int row_size = argc - 1;
int col_size = strlen(argv[1]);
for (i = 0; i < row_size; i++)
{
printf("%s\n", argv[i + 1]);
}
printf("%d\n", maximalRectangle(argv + 1, argc - 1, strlen(argv[1])));
return 0;
}
\ No newline at end of file
{
"type": "code_options",
"author": "CSDN.net",
"source": "solution.md",
"exercise_id": "b2a08644fd54460fb4d8abcb56c527f1"
}
\ No newline at end of file
# 最大矩形
<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>以下错误的选项是?</p>
## aop
### before
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
```cpp
int main()
{
Solution sol;
vector<vector<char>> matrix = {{'1', '0', '1', '0', '0'},
{'1', '0', '1', '1', '1'},
{'1', '1', '1', '1', '1'},
{'1', '0', '0', '1', '0'}};
int res;
res = sol.maximalRectangle(matrix);
cout << res << endl;
return 0;
}
```
## 答案
```cpp
class Solution
{
public:
int largestRectangleArea(vector<int> &heights)
{
stack<int> h;
heights.push_back(0);
int ans = 0, hsize = heights.size();
for (int i = 0; i < hsize; i++)
{
while (!h.empty() && heights[h.top()] > heights[i])
{
int top = h.top();
h.pop();
ans = max(ans, heights[top] * (h.empty() ? i : (i - h.top())));
}
h.push(i);
}
return ans;
}
int maximalRectangle(vector<vector<char>> &matrix)
{
if (matrix.empty())
return 0;
int n = matrix.size(), m = matrix[0].size(), ans = 0;
vector<vector<int>> num(n, vector<int>(m, 0));
for (int j = 0; j < m; j++)
{
num[0][j] = (matrix[0][j] == '0') ? 0 : 1;
for (int i = 1; i < n; i++)
num[i][j] = (matrix[i][j] == '0') ? 0 : num[i - 1][j] + 1;
}
for (int i = 0; i < n; i++)
{
int area = largestRectangleArea(num[i]);
ans = max(ans, area);
}
return ans;
}
};
```
## 选项
### A
```cpp
class Solution
{
public:
int maximalRectangle(vector<vector<char>> &matrix)
{
int res = 0;
vector<int> height;
for (int i = 0; i < matrix.size(); ++i)
{
height.resize(matrix[i].size());
for (int j = 0; j < matrix[i].size(); ++j)
{
height[j] = matrix[i][j] == '0' ? 0 : (1 + height[j]);
}
res = max(res, largestRectangleArea(height));
}
return res;
}
int largestRectangleArea(vector<int> &heights)
{
if (heights.empty())
return 0;
stack<int> st;
heights.push_back(0);
int res0 = 0;
for (int i = 0; i < heights.size(); i++)
{
while (!st.empty() && heights[i] < heights[st.top()])
{
int curHeight = heights[st.top()];
st.pop();
int width = st.empty() ? i : i - st.top() - 1;
if (width * curHeight > res0)
res0 = width * curHeight;
}
st.push(i);
}
return res0;
}
};
```
### B
```cpp
class Solution
{
public:
int maximalRectangle(vector<vector<char>> &matrix)
{
if (matrix.size() == 0)
{
return 0;
}
int m = matrix.size();
int n = matrix[0].size();
int left[n];
int right[n];
int height[n];
memset(right, n, sizeof(right));
memset(left, n, sizeof(left));
memset(height, n, sizeof(height));
int maxarea = 0;
for (int i = 0; i < m; ++i)
{
int 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] = 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] = min(right[j], cur_right);
}
else
{
right[j] = n;
cur_right = j;
}
}
for (int j = 0; j < n; ++j)
{
maxarea = max(maxarea, (right[j] - left[j]) * height[j]);
}
}
return maxarea;
}
};
```
### C
```cpp
class Solution
{
public:
int maximalRectangle(vector<vector<char>> &matrix)
{
if (matrix.size() == 0)
{
return 0;
}
int maxarea = 0;
int dp[matrix.size()][matrix[0].size()];
memset(dp, 0, sizeof(dp));
for (int i = 0; i < matrix.size(); ++i)
{
for (int j = 0; j < matrix[0].size(); ++j)
{
if (matrix[i][j] == '1')
{
dp[i][j] = j == 0 ? 1 : dp[i][j - 1] + 1;
int width = dp[i][j];
for (int k = i; k >= 0; k--)
{
width = min(width, dp[k][j]);
maxarea = max(maxarea, width * (i - k + 1));
}
}
}
}
return maxarea;
}
};
```
{
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"keywords": [
"leetcode",
"扰乱字符串"
],
"children": [],
"export": [
"solution.json"
],
"title": "扰乱字符串"
}
\ No newline at end of file
<div class="notranslate">使用下面描述的算法可以扰乱字符串 <code>s</code> 得到字符串 <code>t</code>
<ol>
<li>如果字符串的长度为 1 ,算法停止</li>
<li>如果字符串的长度 &gt; 1 ,执行下述步骤:
<ul>
<li>在一个随机下标处将字符串分割成两个非空的子字符串。即,如果已知字符串 <code>s</code> ,则可以将其分成两个子字符串 <code>x</code><code>y</code>
,且满足 <code>s = x + y</code></li>
<li><strong>随机</strong> 决定是要「交换两个子字符串」还是要「保持这两个子字符串的顺序不变」。即,在执行这一步骤之后,<code>s</code> 可能是
<code>s = x + y</code> 或者 <code>s = y + x</code>
</li>
<li><code>x</code><code>y</code> 这两个子字符串上继续从步骤 1 开始递归执行此算法。</li>
</ul>
</li>
</ol>
<p>给你两个 <strong>长度相等</strong> 的字符串 <code>s1</code><em>
</em>&nbsp;<code>s2</code>,判断&nbsp;<code>s2</code><em>&nbsp;</em>是否是&nbsp;<code>s1</code><em>&nbsp;</em>的扰乱字符串。如果是,返回
<code>true</code> ;否则,返回 <code>false</code>
</p>
<p>&nbsp;</p>
<p><strong>示例 1:</strong></p>
<pre><strong>输入:</strong>s1 = "great", s2 = "rgeat"
<strong><br />输出:</strong>true
<strong><br />解释:</strong>s1 上可能发生的一种情形是:
"great" --&gt; "gr/eat" // 在一个随机下标处分割得到两个子字符串
"gr/eat" --&gt; "gr/eat" // 随机决定:「保持这两个子字符串的顺序不变」
"gr/eat" --&gt; "g/r / e/at" // 在子字符串上递归执行此算法。两个子字符串分别在随机下标处进行一轮分割
"g/r / e/at" --&gt; "r/g / e/at" // 随机决定:第一组「交换两个子字符串」,第二组「保持这两个子字符串的顺序不变」
"r/g / e/at" --&gt; "r/g / e/ a/t" // 继续递归执行此算法,将 "at" 分割得到 "a/t"
"r/g / e/ a/t" --&gt; "r/g / e/ a/t" // 随机决定:「保持这两个子字符串的顺序不变」
算法终止,结果字符串和 s2 相同,都是 "rgeat"
这是一种能够扰乱 s1 得到 s2 的情形,可以认为 s2 是 s1 的扰乱字符串,返回 true
</pre>
<p><strong>示例 2:</strong></p>
<pre><strong>输入:</strong>s1 = "abcde", s2 = "caebd"
<strong><br />输出:</strong>false
</pre>
<p><strong>示例 3:</strong></p>
<pre><strong>输入:</strong>s1 = "a", s2 = "a"
<strong><br />输出:</strong>true
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>s1.length == s2.length</code></li>
<li><code>1 &lt;= s1.length &lt;= 30</code></li>
<li><code>s1</code><code>s2</code> 由小写英文字母组成</li>
</ul>
</div>
\ No newline at end of file
#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++)
{
if (scramble(s1, low1, low1 + i - 1, s2, low2, low2 + i - 1) &&
scramble(s1, low1 + i, high1, s2, low2 + i, high2))
{
return true;
}
if (scramble(s1, low1, low1 + i - 1, s2, high2 - i + 1, high2) &&
scramble(s1, low1 + i, high1, s2, low2, high2 - i))
{
return true;
}
}
return false;
}
}
}
static bool isScramble(char *s1, char *s2)
{
return scramble(s1, 0, strlen(s1) - 1, s2, 0, strlen(s2) - 1);
}
int main(int argc, char **argv)
{
if (argc != 3)
{
fprintf(stderr, "Usage: ./test s1 s2\n");
exit(-1);
}
printf("%s\n", isScramble(argv[1], argv[2]) ? "true" : "false");
return 0;
}
\ No newline at end of file
{
"type": "code_options",
"author": "CSDN.net",
"source": "solution.md",
"exercise_id": "ad842cfadb884aedbab6b6b8e8e60a11"
}
\ No newline at end of file
# 扰乱字符串
<div class="notranslate">使用下面描述的算法可以扰乱字符串 <code>s</code> 得到字符串 <code>t</code>
<ol>
<li>如果字符串的长度为 1 ,算法停止</li>
<li>如果字符串的长度 &gt; 1 ,执行下述步骤:
<ul>
<li>在一个随机下标处将字符串分割成两个非空的子字符串。即,如果已知字符串 <code>s</code> ,则可以将其分成两个子字符串 <code>x</code><code>y</code>
,且满足 <code>s = x + y</code></li>
<li><strong>随机</strong> 决定是要「交换两个子字符串」还是要「保持这两个子字符串的顺序不变」。即,在执行这一步骤之后,<code>s</code> 可能是
<code>s = x + y</code> 或者 <code>s = y + x</code>
</li>
<li><code>x</code><code>y</code> 这两个子字符串上继续从步骤 1 开始递归执行此算法。</li>
</ul>
</li>
</ol>
<p>给你两个 <strong>长度相等</strong> 的字符串 <code>s1</code><em>
</em>和&nbsp;<code>s2</code>,判断&nbsp;<code>s2</code><em>&nbsp;</em>是否是&nbsp;<code>s1</code><em>&nbsp;</em>的扰乱字符串。如果是,返回
<code>true</code> ;否则,返回 <code>false</code> 。
</p>
<p>&nbsp;</p>
<p><strong>示例 1:</strong></p>
<pre><strong>输入:</strong>s1 = "great", s2 = "rgeat"
<strong><br />输出:</strong>true
<strong><br />解释:</strong>s1 上可能发生的一种情形是:
"great" --&gt; "gr/eat" // 在一个随机下标处分割得到两个子字符串
"gr/eat" --&gt; "gr/eat" // 随机决定:「保持这两个子字符串的顺序不变」
"gr/eat" --&gt; "g/r / e/at" // 在子字符串上递归执行此算法。两个子字符串分别在随机下标处进行一轮分割
"g/r / e/at" --&gt; "r/g / e/at" // 随机决定:第一组「交换两个子字符串」,第二组「保持这两个子字符串的顺序不变」
"r/g / e/at" --&gt; "r/g / e/ a/t" // 继续递归执行此算法,将 "at" 分割得到 "a/t"
"r/g / e/ a/t" --&gt; "r/g / e/ a/t" // 随机决定:「保持这两个子字符串的顺序不变」
算法终止,结果字符串和 s2 相同,都是 "rgeat"
这是一种能够扰乱 s1 得到 s2 的情形,可以认为 s2 是 s1 的扰乱字符串,返回 true
</pre>
<p><strong>示例 2:</strong></p>
<pre><strong>输入:</strong>s1 = "abcde", s2 = "caebd"
<strong><br />输出:</strong>false
</pre>
<p><strong>示例 3:</strong></p>
<pre><strong>输入:</strong>s1 = "a", s2 = "a"
<strong><br />输出:</strong>true
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>s1.length == s2.length</code></li>
<li><code>1 &lt;= s1.length &lt;= 30</code></li>
<li><code>s1</code> 和 <code>s2</code> 由小写英文字母组成</li>
</ul>
</div>
<p>以下错误的选项是?</p>
## aop
### before
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
```cpp
int main()
{
Solution sol;
string s1 = "great";
string s2 = "rgeat";
bool res;
res = sol.isScramble(s1, s2);
cout << res << endl;
return 0;
}
```
## 答案
```cpp
class Solution
{
unordered_map<string, bool> memo;
public:
bool isScramble(string s1, string s2)
{
if (memo.count(s1 + '_' + s2) != 0)
{
return memo[s1 + '_' + s2];
}
if (s1 == s2)
{
memo[s1 + '_' + s2] = true;
return true;
}
else if (s1.size() == 1)
{
memo[s1 + '_' + s2] = false;
return false;
}
bool do_it = false;
bool not_do = false;
for (int i = 1; i < s1.size(); i++)
{
do_it = isScramble(s1.substr(0, i), s2.substr(s1.size() - 1, s1.size()));
not_do = isScramble(s1.substr(0, i), s2.substr(0, i));
if (do_it || not_do)
{
memo[s1 + '_' + s2] = true;
return true;
}
}
memo[s1 + '_' + s2] = false;
return false;
}
};
```
## 选项
### A
```cpp
class Solution
{
public:
bool isScramble(string s1, string s2)
{
int n1 = s1.length(), n2 = s2.length();
if (n1 != n2)
return false;
vector<vector<vector<bool>>> dp(n1 + 1, vector<vector<bool>>(n1 + 1, vector<bool>(n1 + 1, false)));
int i, j, k;
for (i = 1; i <= n1; i++)
{
for (j = 1; j <= n1; j++)
{
dp[i][j][1] = (s1[i - 1] == s2[j - 1]);
}
}
for (int len = 2; len <= n1; len++)
{
for (i = 1; i <= n1 && i + len <= n1 + 1; i++)
{
for (j = 1; j <= n1 && j + len <= n1 + 1; j++)
{
for (k = 1; k < len; k++)
{
if (dp[i][j][k] && dp[i + k][j + k][len - k])
{
dp[i][j][len] = true;
break;
}
if (dp[i][j + len - k][k] && dp[i + k][j][len - k])
{
dp[i][j][len] = true;
break;
}
}
}
}
}
return dp[1][1][n1];
}
};
```
### B
```cpp
class Solution
{
public:
bool isScramble(string s1, string s2)
{
if (s1.size() != s2.size())
return false;
if (s1 == s2)
return true;
vector<int> hash(26, 0);
for (int i = 0; i < s1.size(); i++)
hash.at(s1[i] - 'a')++;
for (int j = 0; j < s2.size(); j++)
hash.at(s2[j] - 'a')--;
for (int k = 0; k < 26; k++)
{
if (hash.at(k) != 0)
return false;
}
for (int i = 1; i < s1.size(); i++)
{
if (
(isScramble(s1.substr(0, i), s2.substr(0, i)) && isScramble(s1.substr(i, s1.size() - i), s2.substr(i, s1.size() - i))) || (isScramble(s1.substr(0, i), s2.substr(s1.size() - i)) && isScramble(s1.substr(i), s2.substr(0, s1.size() - i))))
return true;
}
return false;
}
};
```
### C
```cpp
class Solution
{
public:
bool isScramble(string s1, string s2)
{
if (s1.size() != s2.size())
return false;
if (s1 == s2)
return true;
string str1 = s1, str2 = s2;
sort(str1.begin(), str1.end());
sort(str2.begin(), str2.end());
if (str1 != str2)
return false;
for (int i = 1; i < s1.size(); ++i)
{
string s11 = s1.substr(0, i);
string s12 = s1.substr(i);
string s21 = s2.substr(0, i);
string s22 = s2.substr(i);
if (isScramble(s11, s21) && isScramble(s12, s22))
return true;
s21 = s2.substr(s2.size() - i);
s22 = s2.substr(0, s2.size() - i);
if (isScramble(s11, s21) && isScramble(s12, s22))
return true;
}
return false;
}
};
```
{
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"keywords": [
"leetcode",
"文本左右对齐"
],
"children": [],
"export": [
"solution.json"
],
"title": "文本左右对齐"
}
\ No newline at end of file
<div class="notranslate">
<p>给定一个单词数组和一个长度&nbsp;<em>maxWidth</em>,重新排版单词,使其成为每行恰好有&nbsp;<em>maxWidth</em>&nbsp;个字符,且左右两端对齐的文本。</p>
<p>你应该使用“贪心算法”来放置给定的单词;也就是说,尽可能多地往每行中放置单词。必要时可用空格&nbsp;<code>' '</code>&nbsp;填充,使得每行恰好有 <em>maxWidth</em>&nbsp;个字符。
</p>
<p>要求尽可能均匀分配单词间的空格数量。如果某一行单词间的空格不能均匀分配,则左侧放置的空格数要多于右侧的空格数。</p>
<p>文本的最后一行应为左对齐,且单词之间不插入<strong>额外的</strong>空格。</p>
<p><strong>说明:</strong></p>
<ul>
<li>单词是指由非空格字符组成的字符序列。</li>
<li>每个单词的长度大于 0,小于等于&nbsp;<em>maxWidth</em></li>
<li>输入单词数组 <code>words</code>&nbsp;至少包含一个单词。</li>
</ul>
<p><strong>示例:</strong></p>
<pre><strong>输入:</strong>
words = ["This", "is", "an", "example", "of", "text", "justification."]
maxWidth = 16
<strong><br />输出:</strong>
[
&nbsp; &nbsp;"This &nbsp; &nbsp;is &nbsp; &nbsp;an",
&nbsp; &nbsp;"example &nbsp;of text",
&nbsp; &nbsp;"justification. &nbsp;"
]
</pre>
<p><strong>示例&nbsp;2:</strong></p>
<pre><strong>输入:</strong>
words = ["What","must","be","acknowledgment","shall","be"]
maxWidth = 16
<strong><br />输出:</strong>
[
&nbsp; "What &nbsp; must &nbsp; be",
&nbsp; "acknowledgment &nbsp;",
&nbsp; "shall be &nbsp; &nbsp; &nbsp; &nbsp;"
]
<strong><br />解释: </strong>注意最后一行的格式应为 "shall be " 而不是 "shall be"
因为最后一行应为左对齐,而不是左右两端对齐,第二行同样为左对齐,这是因为这行只包含一个单词。
</pre>
<p><strong>示例&nbsp;3:</strong></p>
<pre><strong>输入:</strong>
words = ["Science","is","what","we","understand","well","enough","to","explain",
&nbsp; "to","a","computer.","Art","is","everything","else","we","do"]
maxWidth = 20
<strong><br />输出:</strong>
[
&nbsp; "Science &nbsp;is &nbsp;what we",
"understand &nbsp; &nbsp; &nbsp;well",
&nbsp; "enough to explain to",
&nbsp; "a &nbsp;computer. &nbsp;Art is",
&nbsp; "everything &nbsp;else &nbsp;we",
&nbsp; "do &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"
]
</pre>
</div>
\ No newline at end of file
#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)
{
for (j = 0; j < even_spaces; j++)
{
*p++ = ' ';
}
if (remain_spaces > 0)
{
*p++ = ' ';
remain_spaces--;
}
}
}
while (p - line < max_size)
{
*p++ = ' ';
}
*p++ = '\0';
}
static char **fullJustify(char **words, int wordsSize, int maxWidth, int *returnSize)
{
int i, j, k, cap = 100, count = 0;
char **lines = malloc(cap * sizeof(char *));
char *buf = malloc(cap * (maxWidth + 1));
for (i = 0; i < cap; i++)
{
lines[i] = buf + i * (maxWidth + 1);
}
int *word_lens = malloc(wordsSize * sizeof(int));
for (i = 0; i < wordsSize; i++)
{
word_lens[i] = strlen(words[i]);
}
int wc = 0;
int len = 0;
int start = 0;
int chars = 0;
for (i = 0, j = 0; i < wordsSize; i++)
{
if (len + word_lens[i] > maxWidth)
{
int even_spaces = wc == 1 ? 0 : (maxWidth - chars) / (wc - 1);
int remain_spaces = wc == 1 ? 0 : (maxWidth - chars) % (wc - 1);
line_fill(lines[count], len, words, word_lens, maxWidth, even_spaces, remain_spaces, start, i);
count++;
wc = 1;
len = word_lens[i] + 1;
chars = word_lens[i];
start = i;
}
else if (len + word_lens[i] == maxWidth)
{
chars += word_lens[i];
int even_spaces = wc == 0 ? 0 : (maxWidth - chars) / wc;
int remain_spaces = wc == 0 ? 0 : (maxWidth - chars) % wc;
line_fill(lines[count], len, words, word_lens, maxWidth, even_spaces, remain_spaces, start, i + 1);
count++;
wc = 0;
len = 0;
chars = 0;
start = i + 1;
}
else
{
chars += word_lens[i];
len += word_lens[i] + 1;
wc++;
}
}
if (wc > 0)
{
char *p = lines[count];
for (i = start; i < start + wc; i++)
{
memcpy(p, words[i], word_lens[i]);
p += word_lens[i];
if (i < start + wc - 1)
{
*p++ = ' ';
}
}
while (p - lines[count] < maxWidth)
{
*p++ = ' ';
}
*p++ = '\0';
count++;
}
*returnSize = count;
return lines;
}
int main(int argc, char **argv)
{
if (argc <= 2)
{
fprintf(stderr, "Usage: ./test maxsize words...\n");
exit(-1);
}
int i, count;
char **lines = fullJustify(argv + 2, argc - 2, atoi(argv[1]), &count);
for (i = 0; i < count; i++)
{
printf("%s\n", lines[i]);
}
return 0;
}
\ No newline at end of file
{
"type": "code_options",
"author": "CSDN.net",
"source": "solution.md",
"exercise_id": "f7276587952f4c079b9dec0fed935c1b"
}
\ No newline at end of file
{
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"keywords": [
"leetcode",
"最小覆盖子串"
],
"children": [],
"export": [
"solution.json"
],
"title": "最小覆盖子串"
}
\ No newline at end of file
<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> 时间内解决此问题的算法吗?
\ No newline at end of file
#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;
}
if (++count[s[l++]] > 0)
{
need_to_meet++;
}
}
}
return min_len == INT_MAX ? "" : s.substr(start, min_len);
}
};
\ No newline at end of file
{
"type": "code_options",
"author": "CSDN.net",
"source": "solution.md",
"exercise_id": "9cf1c91e49da435aa1d63c2bd5aa7925"
}
\ No newline at end of file
# 最小覆盖子串
<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>以下错误的选项是?</p>
## aop
### before
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
```cpp
int main()
{
Solution sol;
string s = "ADOBECODEBANC";
string t = "ABC";
string res;
res = sol.minWindow(s, t);
cout << res;
return 0;
}
```
## 答案
```cpp
class Solution
{
public:
string minWindow(string s, string t)
{
unordered_map<char, int> hs, ht;
for (auto c : t)
ht[c]++;
string res;
int cnt = 0;
for (int i = 0, j = 0; i < s.size(); i++)
{
hs[s[i]]++;
if (hs[s[i]] <= ht[s[i]])
cnt++;
while (hs[s[j]] > ht[s[j]])
hs[s[j++]]--;
if (cnt == t.size())
{
if (res.empty())
res = s.substr(j, i - j);
}
}
return res;
}
};
```
## 选项
### A
```cpp
class Solution
{
public:
string minWindow(string s, string t)
{
vector<int> need(58, 0), window(58, 0);
for (auto c : t)
++need[c - 'A'];
int left = 0, right = 0;
int count = 0;
int target = 0;
for (auto c : need)
{
if (c)
++target;
}
int start = 0;
int len = INT_MAX;
while (right < s.size())
{
auto c = s[right] - 'A';
++right;
if (need[c] > 0)
{
++window[c];
if (need[c] == window[c])
++count;
}
while (count == target)
{
if (right - left < len)
{
start = left;
len = right - left;
}
auto c = s[left] - 'A';
++left;
if (need[c] > 0)
{
if (need[c] == window[c])
--count;
--window[c];
}
}
}
return len == INT_MAX ? "" : s.substr(start, len);
}
};
```
### B
```cpp
class Solution
{
public:
string minWindow(string s, string t)
{
unordered_map<char, int> findChar;
int n1 = s.length(), n2 = t.length(), i, j;
for (i = 0; i < n2; i++)
findChar[t[i]]++;
int start = 0, cnt = 0, minLength = n1 + 1, now = 0;
for (i = 0; i < n1; i++)
{
if ((--findChar[s[i]]) >= 0)
cnt++;
if (cnt == n2)
{
while (++findChar[s[now]] <= 0)
now++;
cnt--;
if (i - now + 1 < minLength)
{
start = now;
minLength = i - now + 1;
}
now++;
}
}
return minLength > n1 ? "" : s.substr(start, minLength);
}
};
```
### C
```cpp
class Solution
{
public:
string minWindow(string s, string t)
{
string ans = "";
map<char, int> lettercount;
for (char c : t)
++lettercount[c];
int count = 0, left = 0, minlen = INT_MAX;
for (int i = 0; i < s.size(); ++i)
{
if (--lettercount[s[i]] >= 0)
++count;
while (count == t.size())
{
if (minlen > i - left + 1)
{
minlen = i - left + 1;
ans = s.substr(left, minlen);
}
if (++lettercount[s[left]] > 0)
--count;
++left;
}
}
return ans;
}
};
```
<p>给定两个整数 <em>n</em><em>k</em>,返回 1 ... <em>n </em>中所有可能的 <em>k</em> 个数的组合。</p>
<p><strong>示例:</strong></p>
<pre><strong>输入:</strong>&nbsp;n = 4, k = 2<strong><br />输出:</strong>[[2,4],[3,4],[2,3],[1,2],[1,3],[1,4],]</pre>
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册