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

update algorithm tree structure

上级 da45b975
...@@ -13,4 +13,5 @@ leetcode_helper.py ...@@ -13,4 +13,5 @@ leetcode_helper.py
lanqiao_helper.py lanqiao_helper.py
lanqiao_template.md lanqiao_template.md
test.html test.html
./src/__pycache__/*.pyc ./src/__pycache__/*.pyc
\ No newline at end of file leetcode_class.md
\ No newline at end of file
<p>给定从 <code>0</code><code>n-1</code>&nbsp;标号的&nbsp;<code>n</code>
个结点,和一个无向边列表(每条边以结点对来表示),请编写一个函数用来判断这些边是否能够形成一个合法有效的树结构。</p>
<p><strong>示例 1:</strong></p>
<pre><strong>输入:</strong> <code>n = 5</code>, 边列表 edges<code> = [[0,1], [0,2], [0,3], [1,4]]</code>
<strong>输出:</strong> true</pre>
<p><strong>示例 2:</strong></p>
<pre><strong>输入:</strong> <code>n = 5, </code>边列表 edges<code> = [[0,1], [1,2], [2,3], [1,3], [1,4]]</code>
<strong>输出:</strong> false</pre>
<p><strong>注意:</strong>你可以假定边列表 <code>edges</code> 中不会出现重复的边。由于所有的边是无向边,边&nbsp;<code>[0,1]</code>&nbsp;和边
<code>[1,0]</code>&nbsp;是相同的,因此不会同时出现在边列表 <code>edges</code> 中。
</p>
\ No newline at end of file
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
vector<int> parents;
vector<int> ranks;
void initUnion(int n)
{
parents.resize(n);
ranks.resize(n, 1);
for (int i = 0; i < n; ++i)
{
parents[i] = i;
}
}
int find(int x)
{
return x == parents[x] ? x : parents[x] = find(parents[x]);
}
bool merge(int i, int j)
{
int iroot = find(i);
int jroot = find(j);
if (iroot == jroot)
{
return false;
}
if (ranks[iroot] <= ranks[jroot])
{
parents[iroot] = jroot;
}
else
{
parents[jroot] = iroot;
}
if (ranks[iroot] == ranks[jroot] && iroot != jroot)
{
ranks[jroot]++;
}
return true;
}
bool validTree(int n, vector<vector<int>> &edges)
{
if (edges.size() != n - 1)
{
return false;
}
initUnion(n);
for (auto &edge : edges)
{
if (!merge(edge[0], edge[1]))
{
return false;
}
}
return true;
}
};
\ No newline at end of file
{
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"keywords": [
"leetcode",
"两数之和"
],
"children": [],
"export": [
"solution.json"
],
"title": "两数之和"
}
\ No newline at end of file
<p>给定一个整数数组 <code>nums</code> 和一个整数目标值 <code>target</code>,请你在该数组中找出 <strong>和为目标值</strong> 的那 <strong>两个</strong> 整数,并返回它们的数组下标。</p><p>你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。</p><p>你可以按任意顺序返回答案。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>nums = [2,7,11,15], target = 9<strong><br />输出:</strong>[0,1]<strong><br />解释:</strong>因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>nums = [3,2,4], target = 6<strong><br />输出:</strong>[1,2]</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>nums = [3,3], target = 6<strong><br />输出:</strong>[0,1]</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>2 <= nums.length <= 10<sup>3</sup></code></li> <li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li> <li><code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code></li> <li><strong>只会存在一个有效答案</strong></li></ul>
\ No newline at end of file
#include <unordered_map>
#include <vector>
#include <iostream>
using namespace std;
class Solution
{
public:
vector<int> twoSum(vector<int> &nums, int target)
{
std::unordered_map<int, int> hset;
vector<int> r;
for (int i = 0; i < nums.size(); ++i)
{
int c = target - nums[i];
auto iter = hset.find(c);
if (iter != hset.end() && iter->second != i)
{
r.push_back(i);
r.push_back(iter->second);
return r;
}
hset.insert(std::make_pair(nums[i], i));
}
return r;
}
};
int main()
{
Solution test;
int arr[] = {3, 2, 4};
vector<int> ret;
vector<int> vec(arr, arr + 4);
ret = test.twoSum(vec, 6);
for (auto i = ret.begin(); i != ret.end(); i++)
{
cout << *i << ' ';
}
return 0;
}
{
"type": "code_options",
"author": "CSDN.net",
"source": "solution.md",
"exercise_id": "852817b216ad4b69829c52281a508f9f"
}
\ No newline at end of file
# 两数之和
<p>给定一个整数数组 <code>nums</code> 和一个整数目标值 <code>target</code>,请你在该数组中找出 <strong>和为目标值</strong> 的那 <strong>两个</strong> 整数,并返回它们的数组下标。</p><p>你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。</p><p>你可以按任意顺序返回答案。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>nums = [2,7,11,15], target = 9<strong><br />输出:</strong>[0,1]<strong><br />解释:</strong>因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>nums = [3,2,4], target = 6<strong><br />输出:</strong>[1,2]</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>nums = [3,3], target = 6<strong><br />输出:</strong>[0,1]</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>2 <= nums.length <= 10<sup>3</sup></code></li> <li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li> <li><code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code></li> <li><strong>只会存在一个有效答案</strong></li></ul>
<p><p>给定一个整数数组 <code>nums</code> 和一个整数目标值 <code>target</code>,请你在该数组中找出 <strong>和为目标值</strong> 的那 <strong>两个</strong> 整数,并返回它们的数组下标。</p><p>你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。</p><p>你可以按任意顺序返回答案。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>nums = [2,7,11,15], target = 9<strong><br />输出:</strong>[0,1]<strong><br />解释:</strong>因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>nums = [3,2,4], target = 6<strong><br />输出:</strong>[1,2]</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>nums = [3,3], target = 6<strong><br />输出:</strong>[0,1]</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>2 <= nums.length <= 10<sup>3</sup></code></li> <li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li> <li><code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code></li> <li><strong>只会存在一个有效答案</strong></li></ul>
<p>以下错误的选项是?</p></p>
## aop
### before
```cpp
#include <unordered_map>
#include <vector>
#include <iostream>
#include <map>
using namespace std;
```
### after
```cpp
int main()
{
Solution test;
int arr[] = {3,2,4};
vector<int> ret;
vector<int> vec(arr, arr+4);
ret = test.twoSum(vec, 6);
for (auto i = ret.begin(); i != ret.end(); i++)
{
cout << *i << ' ';
}
return 0;
}
```
## 答案
```cpp
class Solution
{
public:
vector<int> twoSum(vector<int> &nums, int target)
{
vector<int> vec;
map<int, int> dic;
for (int i = 0; i < nums.size(); ++i)
{
dic[nums[i]] = i;
}
for (int i = 0; i < nums.size(); ++i)
{
if (dic.count(target - nums[i]) != 0 && dic[target - nums[i]] != i)
{
vec.push_back(i);
vec.push_back(dic[target]);
break;
}
}
return vec;
}
};
```
## 选项
### A
```cpp
class Solution
{
public:
vector<int> twoSum(vector<int> &nums, int target)
{
vector<int> a;
map<int, int> map;
for (int i = 0; i < nums.size(); i++)
{
map[nums[i]] = i;
}
for (int j = 0; j < nums.size(); j++)
{
if (map.count(target - nums[j]) == 1 && map[target - nums[j]] != j)
{
a.push_back(j);
a.push_back(map[target - nums[j]]);
return a;
}
}
return a;
}
};
```
### B
```cpp
class Solution
{
public:
vector<int> twoSum(vector<int> &nums, int target)
{
vector<int> a;
map<int, int> map;
for (int i = 0; i < nums.size(); i++)
{
if (map.find(target - nums[i]) != map.end())
{
a.push_back(map[target - nums[i]]);
a.push_back(i);
return a;
}
else
{
map[nums[i]] = i;
}
}
return a;
}
};
```
### C
```cpp
class Solution
{
public:
vector<int> twoSum(vector<int> &nums, int target)
{
vector<int> a;
for (int i = 0; i < nums.size(); i++)
{
for (int j = i + 1; j < nums.size(); j++)
{
if (nums[i] + nums[j] == target)
{
a.push_back(i);
a.push_back(j);
return a;
}
}
}
}
};
```
{
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"keywords": [
"leetcode",
"三数之和"
],
"children": [],
"export": [
"solution.json"
],
"title": "三数之和"
}
\ No newline at end of file
<p>给你一个包含 <code>n</code> 个整数的数组 <code>nums</code>,判断 <code>nums</code> 中是否存在三个元素 <em>a,b,c ,</em>使得 <em>a + b + c = </em>0 ?请你找出所有和为 <code>0</code> 且不重复的三元组。</p><p><strong>注意:</strong>答案中不可以包含重复的三元组。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>nums = [-1,0,1,2,-1,-4]<strong><br />输出:</strong>[[-1,-1,2],[-1,0,1]]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>nums = []<strong><br />输出:</strong>[]</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>nums = [0]<strong><br />输出:</strong>[]</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>0 <= nums.length <= 3000</code></li> <li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li></ul>
\ No newline at end of file
#include <algorithm>
#include <set>
class Solution
{
public:
vector<vector<int>> threeSum(vector<int> &nums)
{
vector<vector<int>> r;
if (nums.size() == 0)
return r;
sort(nums.begin(), nums.end());
int cur, left, right;
cur = 0;
while (cur < nums.size())
{
if (nums[cur] > 0)
break;
left = cur + 1;
right = nums.size() - 1;
while (left < right)
{
int n = nums[cur] + nums[left] + nums[right];
if (n == 0)
{
r.emplace_back(vector<int>({nums[cur], nums[left], nums[right]}));
int t = left + 1;
while (t < right && nums[t] == nums[left])
t++;
left = t;
t = right - 1;
while (t > left && nums[t] == nums[right])
t--;
right = t;
}
else if (n > 0)
{
int t = right - 1;
while (t > left && nums[t] == nums[right])
t--;
right = t;
}
else
{
int t = left + 1;
while (t < right && nums[t] == nums[left])
t++;
left = t;
}
}
int t = cur + 1;
while (t < nums.size() && nums[t] == nums[cur])
t++;
cur = t;
}
return r;
}
};
\ No newline at end of file
{
"type": "code_options",
"author": "CSDN.net",
"source": "solution.md",
"exercise_id": "bc006d65025c41c9a61792b89eff8cd5"
}
\ No newline at end of file
# 三数之和
<p>给你一个包含 <code>n</code> 个整数的数组 <code>nums</code>,判断 <code>nums</code> 中是否存在三个元素 <em>a,b,c ,</em>使得 <em>a + b + c = </em>0 ?请你找出所有和为 <code>0</code> 且不重复的三元组。</p><p><strong>注意:</strong>答案中不可以包含重复的三元组。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>nums = [-1,0,1,2,-1,-4]<strong><br />输出:</strong>[[-1,-1,2],[-1,0,1]]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>nums = []<strong><br />输出:</strong>[]</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>nums = [0]<strong><br />输出:</strong>[]</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>0 <= nums.length <= 3000</code></li> <li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li></ul>
<p><p>给你一个包含 <code>n</code> 个整数的数组 <code>nums</code>,判断 <code>nums</code> 中是否存在三个元素 <em>a,b,c ,</em>使得 <em>a + b + c = </em>0 ?请你找出所有和为 <code>0</code> 且不重复的三元组。</p><p><strong>注意:</strong>答案中不可以包含重复的三元组。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>nums = [-1,0,1,2,-1,-4]<strong><br />输出:</strong>[[-1,-1,2],[-1,0,1]]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>nums = []<strong><br />输出:</strong>[]</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>nums = [0]<strong><br />输出:</strong>[]</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>0 <= nums.length <= 3000</code></li> <li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li></ul>
<p>以下错误的选项是?</p></p>
## aop
### before
```cpp
#include <vector>
#include <iostream>
#include <set>
using namespace std;
```
### after
```cpp
int main()
{
Solution sol;
int arr[] = {-1,0,1,2,-1,-4};
int length1 = sizeof(arr) / sizeof(arr[0]);
vector<int> nums(arr, arr + length1);
vector<vector<int> > res;
res = sol.threeSum(nums);
for (auto i : res)
{
for (auto j : i)
cout << j << ' ';
cout << endl;
}
return 0;
}
```
## 答案
```cpp
class Solution
{
public:
vector<vector<int>> threeSum(vector<int> &nums)
{
sort(nums.begin(), nums.end());
int len = nums.size();
vector<vector<int>> ans;
if (nums.empty())
return {};
for (int i = 0; i < len; ++i)
{
if (i > 0 && nums[i] == nums[i - 1])
continue;
int newtar = -nums[i];
int low = i + 1;
int high = len - 1;
while (low < high)
{
if (nums[low] + nums[high] == newtar)
{
ans.push_back({nums[i], nums[low], nums[high]});
while (low < high && nums[low] == nums[low + 1])
++low;
while (low < high && nums[high] == nums[high - 1])
--high;
}
else
high--;
}
}
return ans;
}
};
```
## 选项
### A
```cpp
class Solution
{
public:
vector<vector<int>> threeSum(vector<int> &nums)
{
sort(nums.begin(), nums.end());
if (nums.size() < 3 || nums.front() > 0 || nums.back() < 0)
return {};
vector<vector<int>> res;
for (int i = 0; i < nums.size(); i++)
{
int fix = nums[i];
if (fix > 0)
break;
if (i > 0 && fix == nums[i - 1])
continue;
int l = i + 1;
int r = nums.size() - 1;
while (l < r)
{
if (nums[l] + nums[r] == -fix)
{
if (l == i + 1 || r == nums.size() - 1)
{
res.push_back(vector<int>{nums[i], nums[l], nums[r]});
l++;
r--;
}
else if (nums[l] == nums[l - 1])
l++;
else if (nums[r] == nums[r + 1])
r--;
else
{
res.push_back(vector<int>{nums[i], nums[l], nums[r]});
l++;
r--;
}
}
else if (nums[l] + nums[r] < -fix)
l++;
else
r--;
}
}
return res;
}
};
```
### B
```cpp
class Solution
{
public:
static bool cmp(const int &a, const int &b)
{
return a < b;
}
vector<vector<int>> threeSum(vector<int> &nums)
{
sort(nums.begin(), nums.end(), cmp);
vector<int>::iterator it;
vector<vector<int>> res;
set<vector<int>> a;
for (int i = 0; i < nums.size(); i++)
{
if (nums[i] > 0)
break;
for (int j = i + 1; j < nums.size(); j++)
{
it = find(nums.begin() + j + 1, nums.end(), 0 - nums[i] - nums[j]);
if (it != nums.end())
{
vector<int> temp;
temp.push_back(nums[i]);
temp.push_back(nums[j]);
temp.push_back(*it);
a.insert(temp);
}
}
}
for (auto k : a)
{
res.push_back(k);
}
return res;
}
};
```
### C
```cpp
class Solution
{
public:
vector<vector<int> > threeSum(vector<int> &nums)
{
if (nums.size() < 3)
return {};
vector<vector<int> > res;
set<vector<int> > ret;
for (int i = 0; i < nums.size() - 2; i++)
{
for (int j = i + 1; j < nums.size() - 1; j++)
{
for (int k = j + 1; k < nums.size(); k++)
{
if (nums[i] + nums[k] + nums[j] == 0)
{
vector<int> temp;
int a = (nums[i] < nums[j] ? nums[i] : nums[j]) < nums[k] ? (nums[i] < nums[j] ? nums[i] : nums[j]) : nums[k];
int b = (nums[i] > nums[j] ? nums[i] : nums[j]) > nums[k] ? (nums[i] > nums[j] ? nums[i] : nums[j]) : nums[k];
int c = 0 - a - b;
temp.push_back(a);
temp.push_back(c);
temp.push_back(b);
ret.insert(temp);
}
}
}
}
for (auto it : ret)
{
res.push_back(it);
}
return res;
}
};
```
{
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"keywords": [
"leetcode",
"四数之和"
],
"children": [],
"export": [
"solution.json"
],
"title": "四数之和"
}
\ No newline at end of file
<p>给定一个包含 <em>n</em> 个整数的数组 <code>nums</code> 和一个目标值 <code>target</code>,判断 <code>nums</code> 中是否存在四个元素 <em>a,</em><em>b,c</em> 和 <em>d</em> ,使得 <em>a</em> + <em>b</em> + <em>c</em> + <em>d</em> 的值与 <code>target</code> 相等?找出所有满足条件且不重复的四元组。</p><p><strong>注意:</strong>答案中不可以包含重复的四元组。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>nums = [1,0,-1,0,-2,2], target = 0<strong><br />输出:</strong>[[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>nums = [], target = 0<strong><br />输出:</strong>[]</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>0 <= nums.length <= 200</code></li> <li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li> <li><code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code></li></ul>
\ No newline at end of file
class Solution
{
public:
vector<vector<int>> fourSum(vector<int> &nums, int target)
{
long long l_target = target;
sort(nums.begin(), nums.end());
vector<vector<int>> results;
int N = nums.size();
for (int i = 0; i < N - 3; i++)
{
if (i > 0 && nums[i] == nums[i - 1])
continue;
for (int j = i + 1; j < N - 2; j++)
{
if (j > i + 1 && nums[j] == nums[j - 1])
continue;
for (int k = j + 1, l = N - 1; k < l; k++)
{
if (k > j + 1 && nums[k] == nums[k - 1])
continue;
while (k < l &&
(l_target - nums[i] - nums[j] - nums[k] - nums[l]) < 0)
{
l--;
}
if (k >= l)
{
break;
}
if ((target - nums[i] - nums[j] - nums[k] - nums[l]) == 0)
{
results.emplace_back(
vector<int>({nums[i], nums[j], nums[k], nums[l]}));
}
}
}
}
return results;
}
};
\ No newline at end of file
{
"type": "code_options",
"author": "CSDN.net",
"source": "solution.md",
"exercise_id": "4631d8e8bad64b4cae23dfcb9120f05f"
}
\ No newline at end of file
# 四数之和
<p>给定一个包含 <em>n</em> 个整数的数组 <code>nums</code> 和一个目标值 <code>target</code>,判断 <code>nums</code> 中是否存在四个元素 <em>a,</em><em>b,c</em> 和 <em>d</em> ,使得 <em>a</em> + <em>b</em> + <em>c</em> + <em>d</em> 的值与 <code>target</code> 相等?找出所有满足条件且不重复的四元组。</p><p><strong>注意:</strong>答案中不可以包含重复的四元组。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>nums = [1,0,-1,0,-2,2], target = 0<strong><br />输出:</strong>[[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>nums = [], target = 0<strong><br />输出:</strong>[]</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>0 <= nums.length <= 200</code></li> <li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li> <li><code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code></li></ul>
<p><p>给定一个包含 <em>n</em> 个整数的数组 <code>nums</code> 和一个目标值 <code>target</code>,判断 <code>nums</code> 中是否存在四个元素 <em>a,</em><em>b,c</em> 和 <em>d</em> ,使得 <em>a</em> + <em>b</em> + <em>c</em> + <em>d</em> 的值与 <code>target</code> 相等?找出所有满足条件且不重复的四元组。</p><p><strong>注意:</strong>答案中不可以包含重复的四元组。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>nums = [1,0,-1,0,-2,2], target = 0<strong><br />输出:</strong>[[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>nums = [], target = 0<strong><br />输出:</strong>[]</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>0 <= nums.length <= 200</code></li> <li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li> <li><code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code></li></ul>
<p>以下错误的选项是?</p></p>
## aop
### before
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
```cpp
int main()
{
Solution sol;
int target = 0;
int nums[] = {1, 0, -1, 0, -2, 2};
int length1 = sizeof(nums) / sizeof(nums[0]);
vector<int> nums1(nums, nums + length1);
vector<vector<int>> res;
res = sol.fourSum(nums1, target);
for (auto i : res)
{
for (auto j : i)
cout << j << ' ';
cout << endl;
}
return 0;
}
```
## 答案
```cpp
class Solution
{
public:
vector<vector<int>> fourSum(vector<int> &nums, int target)
{
vector<vector<int>> res;
vector<int> temp;
sort(nums.begin(), nums.end());
for (int base_begin = 0; base_begin < nums.size(); base_begin++)
{
if (base_begin > 0 && nums.at(base_begin) == nums.at(base_begin - 1))
continue;
for (int base_end = nums.size() - 1; base_end > base_begin + 2; base_end--)
{
int left = base_begin + 1;
int right = base_end - 1;
while (left < right)
{
int tt = nums.at(base_begin) + nums.at(base_end) + nums.at(left) + nums.at(right);
if (tt > target)
right--;
else
{
temp.clear();
temp.push_back(nums.at(base_begin));
temp.push_back(nums.at(left));
temp.push_back(nums.at(right));
temp.push_back(nums.at(base_end));
res.push_back(temp);
++left;
--right;
}
}
}
}
return res;
}
};
```
## 选项
### A
```cpp
class Solution
{
public:
vector<vector<int>> fourSum(vector<int> &nums, int target)
{
vector<vector<int>> res;
sort(nums.begin(), nums.end());
if (nums.size() < 4)
return res;
for (int a = 0; a < nums.size() - 3; ++a)
{
if (a > 0 && nums[a] == nums[a - 1])
{
continue;
}
for (int b = a + 1; b < nums.size() - 2; ++b)
{
if (b > a + 1 && nums[b] == nums[b - 1])
{
continue;
}
int c = b + 1, d = nums.size() - 1;
while (c < d)
{
int sum = nums[a] + nums[b] + nums[c] + nums[d];
if (sum < target)
{
c++;
}
else if (sum > target)
{
d--;
}
else
{
res.push_back({nums[a], nums[b], nums[c], nums[d]});
while (c < d && nums[c] == nums[c + 1])
{
c++;
}
while (c < d && nums[d - 1] == nums[d])
{
d--;
}
c++;
d--;
}
}
}
}
return res;
}
};
```
### B
```cpp
class Solution
{
public:
vector<vector<int>> fourSum(vector<int> &nums, int target)
{
if (nums.size() < 4)
return {};
sort(nums.begin(), nums.end());
vector<vector<int>> res;
set<vector<int>> a;
for (int i = 0; i < nums.size() - 3; i++)
{
if (nums[i] > target && target > 0)
break;
for (int j = i + 1; j < nums.size() - 2; j++)
{
int l = j + 1;
int r = nums.size() - 1;
while (l < r)
{
if (nums[i] + nums[j] + nums[l] + nums[r] < target)
l++;
else if (nums[i] + nums[j] + nums[l] + nums[r] > target)
r--;
else
{
vector<int> temp{nums[i], nums[j], nums[l], nums[r]};
a.insert(temp);
l++;
r--;
}
}
}
}
for (auto c : a)
{
res.push_back(c);
}
return res;
}
};
```
### C
```cpp
class Solution
{
public:
vector<vector<int>> fourSum(vector<int> &nums, int target)
{
if (nums.size() < 4)
return {};
sort(nums.begin(), nums.end());
set<vector<int>> a;
vector<vector<int>> res;
for (int i = 0; i < nums.size() - 3; i++)
{
if (nums[i] > target && target > 0)
break;
for (int j = i + 1; j < nums.size() - 2; j++)
{
for (int l = j + 1; l < nums.size() - 1; l++)
{
for (int r = l + 1; r < nums.size(); r++)
{
if (nums[i] + nums[j] + nums[l] + nums[r] == target)
a.insert(vector<int>{nums[i], nums[j], nums[l], nums[r]});
}
}
}
}
for (auto c : a)
{
res.push_back(c);
}
return res;
}
};
```
{
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"keywords": [
"leetcode",
"搜索旋转排序数组"
],
"children": [],
"export": [
"solution.json"
],
"title": "搜索旋转排序数组"
}
\ No newline at end of file
<p>整数数组 <code>nums</code> 按升序排列,数组中的值 <strong>互不相同</strong></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,5,6,7]</code> 在下标 <code>3</code> 处经旋转后可能变为 <code>[4,5,6,7,0,1,2]</code>
</p>
<p>给你 <strong>旋转后</strong> 的数组 <code>nums</code> 和一个整数 <code>target</code> ,如果 <code>nums</code> 中存在这个目标值
<code>target</code> ,则返回它的下标,否则返回 <code>-1</code> 。
</p>
<p> </p>
<p><strong>示例 1:</strong></p>
<pre><strong>输入:</strong>nums = [4,5,6,7,0,1,2], target = 0<strong><br />输出:</strong>4</pre>
<p><strong>示例 2:</strong></p>
<pre><strong>输入:</strong>nums = [4,5,6,7,0,1,2], target = 3<strong><br />输出:</strong>-1</pre>
<p><strong>示例 3:</strong></p>
<pre><strong>输入:</strong>nums = [1], target = 0<strong><br />输出:</strong>-1</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5000</code></li>
<li><code>-10^4 <= nums[i] <= 10^4</code></li>
<li><code>nums</code> 中的每个值都 <strong>独一无二</strong></li>
<li>题目数据保证 <code>nums</code> 在预先未知的某个下标上进行了旋转</li>
<li><code>-10^4 <= target <= 10^4</code></li>
</ul>
<p> </p>
<p><strong>进阶:</strong>你可以设计一个时间复杂度为 <code>O(log n)</code> 的解决方案吗?</p>
\ No newline at end of file
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
int search(vector<int> &nums, int target)
{
int lo = 0;
int hi = nums.size() - 1;
for (lo <= hi)
{
int mid = lo + (hi - lo) / 2;
if (nums[mid] == target)
{
return mid;
}
if (nums[lo] <= nums[mid])
{
if (nums[lo] <= target && target < nums[mid])
{
hi = mid - 1;
}
else
{
lo = mid + 1;
}
}
else
{
if (nums[mid] < target && target <= nums[hi])
{
lo = mid + 1;
}
else
{
hi = mid - 1;
}
}
}
return -1;
}
};
\ No newline at end of file
{
"type": "code_options",
"author": "CSDN.net",
"source": "solution.md",
"exercise_id": "fad580e1c8d04bffbbaea845eefdc45d"
}
\ No newline at end of file
# 搜索旋转排序数组
<p>整数数组 <code>nums</code> 按升序排列,数组中的值 <strong>互不相同</strong></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,5,6,7]</code> 在下标 <code>3</code> 处经旋转后可能变为 <code>[4,5,6,7,0,1,2]</code>
</p>
<p>给你 <strong>旋转后</strong> 的数组 <code>nums</code> 和一个整数 <code>target</code> ,如果 <code>nums</code> 中存在这个目标值
<code>target</code> ,则返回它的下标,否则返回 <code>-1</code> 。
</p>
<p> </p>
<p><strong>示例 1:</strong></p>
<pre><strong>输入:</strong>nums = [4,5,6,7,0,1,2], target = 0<strong><br />输出:</strong>4</pre>
<p><strong>示例 2:</strong></p>
<pre><strong>输入:</strong>nums = [4,5,6,7,0,1,2], target = 3<strong><br />输出:</strong>-1</pre>
<p><strong>示例 3:</strong></p>
<pre><strong>输入:</strong>nums = [1], target = 0<strong><br />输出:</strong>-1</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5000</code></li>
<li><code>-10^4 <= nums[i] <= 10^4</code></li>
<li><code>nums</code> 中的每个值都 <strong>独一无二</strong></li>
<li>题目数据保证 <code>nums</code> 在预先未知的某个下标上进行了旋转</li>
<li><code>-10^4 <= target <= 10^4</code></li>
</ul>
<p> </p>
<p><strong>进阶:</strong>你可以设计一个时间复杂度为 <code>O(log n)</code> 的解决方案吗?</p>
<p><p>整数数组 <code>nums</code> 按升序排列,数组中的值 <strong>互不相同</strong></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,5,6,7]</code> 在下标 <code>3</code> 处经旋转后可能变为 <code>[4,5,6,7,0,1,2]</code>
</p>
<p>给你 <strong>旋转后</strong> 的数组 <code>nums</code> 和一个整数 <code>target</code> ,如果 <code>nums</code> 中存在这个目标值
<code>target</code> ,则返回它的下标,否则返回 <code>-1</code> 。
</p>
<p> </p>
<p><strong>示例 1:</strong></p>
<pre><strong>输入:</strong>nums = [4,5,6,7,0,1,2], target = 0<strong><br />输出:</strong>4</pre>
<p><strong>示例 2:</strong></p>
<pre><strong>输入:</strong>nums = [4,5,6,7,0,1,2], target = 3<strong><br />输出:</strong>-1</pre>
<p><strong>示例 3:</strong></p>
<pre><strong>输入:</strong>nums = [1], target = 0<strong><br />输出:</strong>-1</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5000</code></li>
<li><code>-10^4 <= nums[i] <= 10^4</code></li>
<li><code>nums</code> 中的每个值都 <strong>独一无二</strong></li>
<li>题目数据保证 <code>nums</code> 在预先未知的某个下标上进行了旋转</li>
<li><code>-10^4 <= target <= 10^4</code></li>
</ul>
<p> </p>
<p><strong>进阶:</strong>你可以设计一个时间复杂度为 <code>O(log n)</code> 的解决方案吗?</p>
<p>以下错误的选项是?</p></p>
## aop
### before
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
```cpp
int main()
{
Solution sol;
int res;
vector<int> nums{4, 5, 6, 7, 0, 1, 2};
int target = 0;
res = sol.search(nums, target);
cout << res << " ";
return 0;
}
```
## 答案
```cpp
class Solution
{
public:
int search(vector<int> &nums, int target)
{
return bs(nums, 0, nums.size(), target);
}
int bs(vector<int> &nums, int i, int j, int &target)
{
if (i > j)
return -1;
int k = (i + j) / 2;
if (nums[k] == target)
return k;
if (nums[k] < nums[j])
{
if (target < nums[k] || target > nums[j])
return bs(nums, i + 1, k - 1, target);
else
return bs(nums, k - 1, j, target);
}
else
{
if (target > nums[k] || target < nums[i])
return bs(nums, k + 1, j, target);
else
return bs(nums, i - 1, k, target);
}
}
};
```
## 选项
### A
```cpp
class Solution
{
public:
int search(vector<int> &nums, int target)
{
int lo = 0;
int hi = nums.size() - 1;
while (lo <= hi)
{
int mid = lo + (hi - lo) / 2;
if (nums[mid] == target)
{
return mid;
}
if (nums[lo] <= nums[mid])
{
if (nums[lo] <= target && target < nums[mid])
{
hi = mid - 1;
}
else
{
lo = mid + 1;
}
}
else
{
if (nums[mid] < target && target <= nums[hi])
{
lo = mid + 1;
}
else
{
hi = mid - 1;
}
}
}
return -1;
}
};
```
### B
```cpp
class Solution
{
public:
int search(vector<int> &nums, int target)
{
int l = 0, r = nums.size() - 1;
while (l <= r)
{
int mid = (l + r) / 2;
int newMid = nums[0] > nums[mid] ? nums[mid] + 0x3f3f3f3f : nums[mid];
int newTarget = nums[0] > target ? target + 0x3f3f3f3f : target;
if (newMid == newTarget)
return mid;
else if (newMid < newTarget)
l = mid + 1;
else if (newMid > newTarget)
r = mid - 1;
}
return -1;
}
};
```
### C
```cpp
class Solution
{
public:
int search(vector<int> &nums, int target)
{
int n = nums.size();
if (n == 0)
return -1;
int loc = -1;
int i = 0;
int j = n - 1;
int cur = i;
while (i <= j)
{
if (nums[i] == target)
{
loc = i;
break;
}
else if (nums[j] == target)
{
loc = j;
break;
}
if ((nums[i] > target && nums[j] < target) || i == j)
{
break;
}
if (nums[i] < target)
{
int mid = (i + j) / 2;
if (i == mid || j == mid)
return loc;
while (nums[mid] < nums[i])
{
j = mid - 1;
mid = (i + j) / 2;
}
if (nums[mid] < target)
{
i = mid;
continue;
}
else
{
j = mid;
while (i <= j)
{
mid = (i + j) / 2;
if (nums[mid] == target)
{
loc = mid;
break;
}
if (nums[mid] > target)
j = mid - 1;
else
i = mid + 1;
}
}
}
if (nums[j] > target)
{
int mid = (i + j) / 2;
if (i == mid || j == mid)
return loc;
while (nums[mid] > nums[j])
{
i = mid + 1;
mid = (i + j) / 2;
}
if (nums[mid] > target)
{
j = mid;
continue;
}
else
{
i = mid;
while (i <= j)
{
mid = (i + j) / 2;
if (nums[mid] == target)
{
loc = mid;
break;
}
if (nums[mid] > target)
j = mid - 1;
else
i = mid + 1;
}
}
}
}
return loc;
}
};
```
{
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"keywords": [
"leetcode",
"在排序数组中查找元素的第一个和最后一个位置"
],
"children": [],
"export": [
"solution.json"
],
"title": "在排序数组中查找元素的第一个和最后一个位置"
}
\ No newline at end of file
<p>给定一个按照升序排列的整数数组 <code>nums</code>,和一个目标值 <code>target</code>。找出给定目标值在数组中的开始位置和结束位置。</p>
<p>如果数组中不存在目标值 <code>target</code>,返回 <code>[-1, -1]</code></p>
<p><strong>进阶:</strong></p>
<ul>
<li>你可以设计并实现时间复杂度为 <code>O(log n)</code> 的算法解决此问题吗?</li>
</ul>
<p> </p>
<p><strong>示例 1:</strong></p>
<pre><strong>输入:</strong>nums = [5,7,7,8,8,10], target = 8<strong><br />输出:</strong>[3,4]</pre>
<p><strong>示例 2:</strong></p>
<pre><strong>输入:</strong>nums = [5,7,7,8,8,10], target = 6<strong><br />输出:</strong>[-1,-1]</pre>
<p><strong>示例 3:</strong></p>
<pre><strong>输入:</strong>nums = [], target = 0<strong><br />输出:</strong>[-1,-1]</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>0 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>nums</code> 是一个非递减数组</li>
<li><code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code></li>
</ul>
\ No newline at end of file
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
vector<int> searchRange(vector<int> &nums, int target)
{
vector<int> res;
res.push_back(binary_search_begin(nums, target));
res.push_back(binary_search_end(nums, target));
return res;
}
private:
int binary_search_begin(vector<int> nums, int target)
{
int lo = -1;
int hi = nums.size();
while (lo + 1 < hi)
{
int mid = lo + (hi - lo) / 2;
if (target > nums[mid])
{
lo = mid;
}
else
{
hi = mid;
}
}
if (hi == nums.size() || nums[hi] != target)
{
return -1;
}
else
{
return hi;
}
}
int binary_search_end(vector<int> nums, int target)
{
int lo = -1;
int hi = nums.size();
while (lo + 1 < hi)
{
int mid = lo + (hi - lo) / 2;
if (target < nums[mid])
{
hi = mid;
}
else
{
lo = mid;
}
}
if (lo == -1 || nums[lo] != target)
{
return -1;
}
else
{
return lo;
}
}
};
\ No newline at end of file
{
"type": "code_options",
"author": "CSDN.net",
"source": "solution.md",
"exercise_id": "816587517ce64d7f907bff5e0d450eb7"
}
\ No newline at end of file
# 在排序数组中查找元素的第一个和最后一个位置
<p>给定一个按照升序排列的整数数组 <code>nums</code>,和一个目标值 <code>target</code>。找出给定目标值在数组中的开始位置和结束位置。</p>
<p>如果数组中不存在目标值 <code>target</code>,返回 <code>[-1, -1]</code></p>
<p><strong>进阶:</strong></p>
<ul>
<li>你可以设计并实现时间复杂度为 <code>O(log n)</code> 的算法解决此问题吗?</li>
</ul>
<p> </p>
<p><strong>示例 1:</strong></p>
<pre><strong>输入:</strong>nums = [5,7,7,8,8,10], target = 8<strong><br />输出:</strong>[3,4]</pre>
<p><strong>示例 2:</strong></p>
<pre><strong>输入:</strong>nums = [5,7,7,8,8,10], target = 6<strong><br />输出:</strong>[-1,-1]</pre>
<p><strong>示例 3:</strong></p>
<pre><strong>输入:</strong>nums = [], target = 0<strong><br />输出:</strong>[-1,-1]</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>0 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>nums</code> 是一个非递减数组</li>
<li><code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code></li>
</ul>
<p><p>给定一个按照升序排列的整数数组 <code>nums</code>,和一个目标值 <code>target</code>。找出给定目标值在数组中的开始位置和结束位置。</p>
<p>如果数组中不存在目标值 <code>target</code>,返回 <code>[-1, -1]</code></p>
<p><strong>进阶:</strong></p>
<ul>
<li>你可以设计并实现时间复杂度为 <code>O(log n)</code> 的算法解决此问题吗?</li>
</ul>
<p> </p>
<p><strong>示例 1:</strong></p>
<pre><strong>输入:</strong>nums = [5,7,7,8,8,10], target = 8<strong><br />输出:</strong>[3,4]</pre>
<p><strong>示例 2:</strong></p>
<pre><strong>输入:</strong>nums = [5,7,7,8,8,10], target = 6<strong><br />输出:</strong>[-1,-1]</pre>
<p><strong>示例 3:</strong></p>
<pre><strong>输入:</strong>nums = [], target = 0<strong><br />输出:</strong>[-1,-1]</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>0 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>nums</code> 是一个非递减数组</li>
<li><code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code></li>
</ul>
<p>以下错误的选项是?</p></p>
## aop
### before
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
```cpp
int main()
{
Solution sol;
vector<int> res;
vector<int> nums{5, 7, 7, 8, 8, 10};
int target = 8;
res = sol.searchRange(nums, target);
for (auto i : res)
cout << i << " ";
return 0;
}
```
## 答案
```cpp
class Solution
{
public:
int left(vector<int> &nums, int target)
{
int left = 0;
int right = nums.size() - 1;
while (left <= right)
{
int mid = (left + right) / 2;
if (nums[mid] == target)
{
if (mid == 0 || nums[mid - 1] < target)
{
return mid;
}
right = mid + 1;
}
else if (nums[mid] > target)
{
right = mid + 1;
}
else
{
left = mid - 1;
}
}
return -1;
}
int right(vector<int> &nums, int target)
{
int left = 0;
int right = nums.size() - 1;
while (left <= right)
{
int mid = (left + right) / 2;
if (nums[mid] == target)
{
if (mid == nums.size() - 1 || nums[mid + 1] > target)
{
return mid;
}
left = mid - 1;
}
else if (nums[mid] > target)
{
right = mid + 1;
}
else
{
left = mid - 1;
}
}
return -1;
}
vector<int> searchRange(vector<int> &nums, int target)
{
vector<int> result;
result.push_back(left(nums, target));
result.push_back(right(nums, target));
return result;
}
};
```
## 选项
### A
```cpp
class Solution
{
public:
vector<int> searchRange(vector<int> &nums, int target)
{
vector<int> res(2, -1);
int i = 0, j = nums.size();
int mid = (i + j) / 2;
int p = -1;
while (i < j)
{
if (nums[mid] == target)
{
p = mid;
break;
}
if (nums[mid] > target)
{
if (j == mid)
break;
j = mid;
mid = (i + j) / 2;
}
else
{
if (i == mid)
break;
i = mid;
mid = (i + j) / 2;
}
}
if (p == -1)
{
return res;
}
else
{
int a = p, b = p;
while (a > 0 && nums[a - 1] == target)
a--;
while (b < nums.size() - 1 && nums[b + 1] == target)
b++;
vector<int> h;
h.push_back(a);
h.push_back(b);
return h;
}
}
};
```
### B
```cpp
class Solution
{
public:
vector<int> searchRange(vector<int> &nums, int target)
{
vector<int> res;
res.push_back(binary_search_begin(nums, target));
res.push_back(binary_search_end(nums, target));
return res;
}
private:
int binary_search_begin(vector<int> nums, int target)
{
int lo = -1;
int hi = nums.size();
while (lo + 1 < hi)
{
int mid = lo + (hi - lo) / 2;
if (target > nums[mid])
{
lo = mid;
}
else
{
hi = mid;
}
}
if (hi == nums.size() || nums[hi] != target)
{
return -1;
}
else
{
return hi;
}
}
int binary_search_end(vector<int> nums, int target)
{
int lo = -1;
int hi = nums.size();
while (lo + 1 < hi)
{
int mid = lo + (hi - lo) / 2;
if (target < nums[mid])
{
hi = mid;
}
else
{
lo = mid;
}
}
if (lo == -1 || nums[lo] != target)
{
return -1;
}
else
{
return lo;
}
}
};
```
### C
```cpp
class Solution
{
public:
vector<int> searchRange(vector<int> &nums, int target)
{
int start = -1, end = -1;
for (int i = 0; i < nums.size(); i++)
{
if (nums[i] == target)
{
if (start == -1)
start = i;
end = i;
}
}
vector<int> ans;
ans.push_back(start);
ans.push_back(end);
return ans;
};
};
```
{
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"keywords": [
"leetcode",
"搜索插入位置"
],
"children": [],
"export": [
"solution.json"
],
"title": "搜索插入位置"
}
\ No newline at end of file
<p>给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。</p><p>你可以假设数组中无重复元素。</p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong> [1,3,5,6], 5<strong><br />输出:</strong> 2</pre><p><strong>示例&nbsp;2:</strong></p><pre><strong>输入:</strong> [1,3,5,6], 2<strong><br />输出:</strong> 1</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong> [1,3,5,6], 7<strong><br />输出:</strong> 4</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong> [1,3,5,6], 0<strong><br />输出:</strong> 0</pre>
\ No newline at end of file
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
int searchInsert(vector<int> &nums, int target)
{
int lo = -1;
int hi = nums.size();
while (lo + 1 < hi)
{
int mid = lo + (hi - lo) / 2;
if (target > nums[mid])
{
lo = mid;
}
else
{
hi = mid;
}
}
return hi;
}
};
\ No newline at end of file
{
"type": "code_options",
"author": "CSDN.net",
"source": "solution.md",
"exercise_id": "52ed66dd57bd43f88dc256c41aea7e59"
}
\ No newline at end of file
# 搜索插入位置
<p>给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。</p><p>你可以假设数组中无重复元素。</p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong> [1,3,5,6], 5<strong><br />输出:</strong> 2</pre><p><strong>示例&nbsp;2:</strong></p><pre><strong>输入:</strong> [1,3,5,6], 2<strong><br />输出:</strong> 1</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong> [1,3,5,6], 7<strong><br />输出:</strong> 4</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong> [1,3,5,6], 0<strong><br />输出:</strong> 0</pre>
<p><p>给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。</p><p>你可以假设数组中无重复元素。</p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong> [1,3,5,6], 5<strong><br />输出:</strong> 2</pre><p><strong>示例&nbsp;2:</strong></p><pre><strong>输入:</strong> [1,3,5,6], 2<strong><br />输出:</strong> 1</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong> [1,3,5,6], 7<strong><br />输出:</strong> 4</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong> [1,3,5,6], 0<strong><br />输出:</strong> 0</pre>
<p>以下错误的选项是?</p></p>
## aop
### before
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
```cpp
int main()
{
Solution sol;
int res;
vector<int> nums{1, 3, 5, 6};
int target = 5;
res = sol.searchInsert(nums, target);
cout << res;
return 0;
}
```
## 答案
```cpp
class Solution
{
public:
int searchInsert(vector<int> &nums, int target)
{
int len = nums.size();
if (target <= nums[0])
return 0;
for (int i = 0; i < len; i++)
{
if (nums[i] == target)
return i - 1;
else if (target < nums[i])
return i + 1;
}
return len;
}
};
```
## 选项
### A
```cpp
class Solution
{
public:
int searchInsert(vector<int> &nums, int target)
{
int lo = -1;
int hi = nums.size();
while (lo + 1 < hi)
{
int mid = lo + (hi - lo) / 2;
if (target > nums[mid])
{
lo = mid;
}
else
{
hi = mid;
}
}
return hi;
}
};
```
### B
```cpp
class Solution
{
public:
int searchInsert(vector<int> &nums, int target)
{
int len = nums.size();
if (len == 0)
return 0;
for (int i = 0; i < len; i++)
{
if (nums[i] >= target)
return i;
}
return len;
}
};
```
### C
```cpp
class Solution
{
public:
int searchInsert(vector<int> &nums, int target)
{
int mid = 0;
int head = 0;
int last = nums.size() - 1;
while (head < last)
{
mid = (last - head) / 2 + head;
if (target > nums[mid])
{
head = mid + 1;
}
else if (target < nums[mid])
{
last = mid - 1;
}
else
return mid;
}
if (target <= nums[head])
return head;
return head + 1;
}
};
```
{
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"keywords": [
"leetcode",
"寻找两个正序数组的中位数"
],
"children": [],
"export": [
"solution.json"
],
"title": "寻找两个正序数组的中位数"
}
\ No newline at end of file
<p>给定两个大小分别为 <code>m</code><code>n</code> 的正序(从小到大)数组 <code>nums1</code> 和 <code>nums2</code>。请你找出并返回这两个正序数组的 <strong>中位数</strong></p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>nums1 = [1,3], nums2 = [2]<strong><br />输出:</strong>2.00000<strong><br />解释:</strong>合并数组 = [1,2,3] ,中位数 2</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>nums1 = [1,2], nums2 = [3,4]<strong><br />输出:</strong>2.50000<strong><br />解释:</strong>合并数组 = [1,2,3,4] ,中位数 (2 + 3) / 2 = 2.5</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>nums1 = [0,0], nums2 = [0,0]<strong><br />输出:</strong>0.00000</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>nums1 = [], nums2 = [1]<strong><br />输出:</strong>1.00000</pre><p><strong>示例 5:</strong></p><pre><strong>输入:</strong>nums1 = [2], nums2 = []<strong><br />输出:</strong>2.00000</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>nums1.length == m</code></li> <li><code>nums2.length == n</code></li> <li><code>0 <= m <= 1000</code></li> <li><code>0 <= n <= 1000</code></li> <li><code>1 <= m + n <= 2000</code></li> <li><code>-10<sup>6</sup> <= nums1[i], nums2[i] <= 10<sup>6</sup></code></li></ul><p> </p><p><strong>进阶:</strong>你能设计一个时间复杂度为 <code>O(log (m+n))</code> 的算法解决此问题吗?</p>
\ No newline at end of file
class Solution
{
public:
double findMedianSortedArrays(vector<int> &nums1, vector<int> &nums2)
{
int nums1Size = nums1.size();
int nums2Size = nums2.size();
int na = nums1Size + nums2Size;
int *ns = (int *)malloc(4 * na);
int i = 0, j = 0, d = 0;
int m = na / 2 + 1;
while (d < m)
{
int n;
if (i < nums1Size && j < nums2Size)
{
n = (nums1[i] < nums2[j]) ? nums1[i++] : nums2[j++];
}
else if (i < nums1Size)
{
n = nums1[i++];
}
else if (j < nums2Size)
{
n = nums2[j++];
}
ns[d++] = n;
}
if (na % 2)
{
return ns[d - 1];
}
return (ns[d - 1] + ns[d - 2]) / 2.0;
}
};
\ No newline at end of file
{
"type": "code_options",
"author": "CSDN.net",
"source": "solution.md",
"exercise_id": "8fb4a29435f1437483111aa7523875cb"
}
\ No newline at end of file
# 寻找两个正序数组的中位数
<p>给定两个大小分别为 <code>m</code><code>n</code> 的正序(从小到大)数组 <code>nums1</code> 和 <code>nums2</code>。请你找出并返回这两个正序数组的 <strong>中位数</strong></p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>nums1 = [1,3], nums2 = [2]<strong><br />输出:</strong>2.00000<strong><br />解释:</strong>合并数组 = [1,2,3] ,中位数 2</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>nums1 = [1,2], nums2 = [3,4]<strong><br />输出:</strong>2.50000<strong><br />解释:</strong>合并数组 = [1,2,3,4] ,中位数 (2 + 3) / 2 = 2.5</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>nums1 = [0,0], nums2 = [0,0]<strong><br />输出:</strong>0.00000</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>nums1 = [], nums2 = [1]<strong><br />输出:</strong>1.00000</pre><p><strong>示例 5:</strong></p><pre><strong>输入:</strong>nums1 = [2], nums2 = []<strong><br />输出:</strong>2.00000</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>nums1.length == m</code></li> <li><code>nums2.length == n</code></li> <li><code>0 <= m <= 1000</code></li> <li><code>0 <= n <= 1000</code></li> <li><code>1 <= m + n <= 2000</code></li> <li><code>-10<sup>6</sup> <= nums1[i], nums2[i] <= 10<sup>6</sup></code></li></ul><p> </p><p><strong>进阶:</strong>你能设计一个时间复杂度为 <code>O(log (m+n))</code> 的算法解决此问题吗?</p>
<p><p>给定两个大小分别为 <code>m</code><code>n</code> 的正序(从小到大)数组 <code>nums1</code> 和 <code>nums2</code>。请你找出并返回这两个正序数组的 <strong>中位数</strong></p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>nums1 = [1,3], nums2 = [2]<strong><br />输出:</strong>2.00000<strong><br />解释:</strong>合并数组 = [1,2,3] ,中位数 2</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>nums1 = [1,2], nums2 = [3,4]<strong><br />输出:</strong>2.50000<strong><br />解释:</strong>合并数组 = [1,2,3,4] ,中位数 (2 + 3) / 2 = 2.5</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>nums1 = [0,0], nums2 = [0,0]<strong><br />输出:</strong>0.00000</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>nums1 = [], nums2 = [1]<strong><br />输出:</strong>1.00000</pre><p><strong>示例 5:</strong></p><pre><strong>输入:</strong>nums1 = [2], nums2 = []<strong><br />输出:</strong>2.00000</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>nums1.length == m</code></li> <li><code>nums2.length == n</code></li> <li><code>0 <= m <= 1000</code></li> <li><code>0 <= n <= 1000</code></li> <li><code>1 <= m + n <= 2000</code></li> <li><code>-10<sup>6</sup> <= nums1[i], nums2[i] <= 10<sup>6</sup></code></li></ul><p> </p><p><strong>进阶:</strong>你能设计一个时间复杂度为 <code>O(log (m+n))</code> 的算法解决此问题吗?</p>
<p>以下错误的选项是?</p></p>
## aop
### before
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
```cpp
int main()
{
Solution test;
float ret;
int arr1[] = {1, 2};
int arr2[] = {3, 4};
int length1 = sizeof(arr1) / sizeof(arr1[0]);
int length2 = sizeof(arr2) / sizeof(arr2[0]);
vector<int> nums1(arr1, arr1 + length1);
vector<int> nums2(arr2, arr2 + length2);
ret = test.findMedianSortedArrays(nums1, nums2);
cout << setiosflags(ios::fixed) << setprecision(5) << ret << endl;
return 0;
}
```
## 答案
```cpp
class Solution
{
public:
int getK(vector<int> nums1, vector<int> nums2, int k)
{
int len1 = nums1.size();
int len2 = nums2.size();
int index1 = 0, index2 = 0;
while (1)
{
if (index1 == len1)
{
return nums2[index2 + k - 1];
}
if (index2 == len2)
{
return nums1[index1 + k - 1];
}
if (k == 1)
{
return max(nums1[index1], nums2[index2]);
}
int newIndex1 = max(index1 + k / 2 - 1, len1 - 1);
int newIndex2 = max(index2 + k / 2 - 1, len2 - 1);
if (nums1[newIndex1] <= nums2[newIndex2])
{
k -= newIndex1 - index1 + 1;
index1 = newIndex1 + 1;
}
else
{
k -= newIndex2 - index2 + 1;
index2 = newIndex2 + 1;
}
}
}
double findMedianSortedArrays(vector<int> &nums1, vector<int> &nums2)
{
int wholeLen = nums1.size() + nums2.size();
if (wholeLen % 2 == 0)
{
return (getK(nums1, nums2, wholeLen / 2) + getK(nums1, nums2, wholeLen / 2 + 1)) / 2.0;
}
else
{
return getK(nums1, nums2, wholeLen / 2 + 1);
}
}
};
```
## 选项
### A
```cpp
class Solution
{
int len1;
int len2;
public:
double findMedianSortedArrays(vector<int> &nums1, vector<int> &nums2)
{
if (nums1.size() <= 0 && nums2.size() <= 0)
return 0;
len1 = nums1.size();
len2 = nums2.size();
int left = (len1 + len2 + 1) / 2, right = (len1 + len2 + 2) / 2;
return (find(nums1, 0, nums2, 0, left) + find(nums1, 0, nums2, 0, right)) / 2.0;
}
int find(vector<int> &nums1, int i, vector<int> &nums2, int j, int k)
{
if (i >= nums1.size())
return nums2[j + k - 1];
if (j >= nums2.size())
return nums1[i + k - 1];
if (k == 1)
return min(nums1[i], nums2[j]);
int midval1 = i + k / 2 - 1 < nums1.size() ? nums1[i + k / 2 - 1] : INT_MAX;
int midval2 = j + k / 2 - 1 < nums2.size() ? nums2[j + k / 2 - 1] : INT_MAX;
if (midval1 < midval2)
return find(nums1, i + k / 2, nums2, j, k - k / 2);
else
return find(nums1, i, nums2, j + k / 2, k - k / 2);
}
};
```
### B
```cpp
class Solution
{
public:
double findMedianSortedArrays(vector<int> &nums1, vector<int> &nums2)
{
vector<int> vecSumArr(nums1.size() + nums2.size());
merge(nums1.begin(), nums1.end(), nums2.begin(), nums2.end(), vecSumArr.begin());
if (vecSumArr.size() % 2 == 0)
return (*(vecSumArr.begin() + vecSumArr.size() / 2 - 1) + *(vecSumArr.begin() + vecSumArr.size() / 2)) * 1.0 / 2;
else
return *(vecSumArr.begin() + vecSumArr.size() / 2);
}
};
```
### C
```cpp
class Solution
{
public:
double findMedianSortedArrays(vector<int> &nums1, vector<int> &nums2)
{
int nums1Size = nums1.size();
int nums2Size = nums2.size();
int na = nums1Size + nums2Size;
int *ns = (int *)malloc(4 * na);
int i = 0, j = 0, d = 0;
int m = na / 2 + 1;
while (d < m)
{
int n;
if (i < nums1Size && j < nums2Size)
{
n = (nums1[i] < nums2[j]) ? nums1[i++] : nums2[j++];
}
else if (i < nums1Size)
{
n = nums1[i++];
}
else if (j < nums2Size)
{
n = nums2[j++];
}
ns[d++] = n;
}
if (na % 2)
{
return ns[d - 1];
}
return (ns[d - 1] + ns[d - 2]) / 2.0;
}
};
```
{
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"keywords": [
"leetcode",
"旋转图像"
],
"children": [],
"export": [
"solution.json"
],
"title": "旋转图像"
}
\ No newline at end of file
<p>给定一个 <em></em>× <em>n</em> 的二维矩阵 <code>matrix</code> 表示一个图像。请你将图像顺时针旋转 90 度。</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> 旋转图像,这意味着你需要直接修改输入的二维矩阵。<strong>请不要 </strong>使用另一个矩阵来旋转图像。</p><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0048.Rotate%20Image/images/mat1.jpg" style="width: 642px; height: 242px;" /><pre><strong>输入:</strong>matrix = [[1,2,3],[4,5,6],[7,8,9]]<strong><br />输出:</strong>[[7,4,1],[8,5,2],[9,6,3]]</pre><p><strong>示例 2:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0048.Rotate%20Image/images/mat2.jpg" style="width: 800px; height: 321px;" /><pre><strong>输入:</strong>matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]<strong><br />输出:</strong>[[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>matrix = [[1]]<strong><br />输出:</strong>[[1]]</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>matrix = [[1,2],[3,4]]<strong><br />输出:</strong>[[3,1],[4,2]]</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>matrix.length == n</code></li> <li><code>matrix[i].length == n</code></li> <li><code>1 <= n <= 20</code></li> <li><code>-1000 <= matrix[i][j] <= 1000</code></li></ul>
\ No newline at end of file
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
void rotate(vector<vector<int>> &matrix)
{
int size = matrix.size();
for (int i = 0; i < size / 2; i++)
{
int low = i, high = size - i - 1;
for (int j = low; j < high; j++)
{
int tmp = matrix[i][j];
matrix[i][j] = matrix[size - 1 - j][i];
matrix[size - 1 - j][i] = matrix[size - 1 - i][size - 1 - j];
matrix[size - 1 - i][size - 1 - j] = matrix[j][size - 1 - i];
matrix[j][size - 1 - i] = tmp;
}
}
}
};
\ No newline at end of file
{
"type": "code_options",
"author": "CSDN.net",
"source": "solution.md",
"exercise_id": "a1354b3b97dd4f30880f929b3bfaaf28"
}
\ No newline at end of file
# 旋转图像
<p>给定一个 <em>n </em>× <em>n</em> 的二维矩阵 <code>matrix</code> 表示一个图像。请你将图像顺时针旋转 90 度。</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> 旋转图像,这意味着你需要直接修改输入的二维矩阵。<strong>请不要 </strong>使用另一个矩阵来旋转图像。</p><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0048.Rotate%20Image/images/mat1.jpg" style="width: 642px; height: 242px;" /><pre><strong>输入:</strong>matrix = [[1,2,3],[4,5,6],[7,8,9]]<strong><br />输出:</strong>[[7,4,1],[8,5,2],[9,6,3]]</pre><p><strong>示例 2:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0048.Rotate%20Image/images/mat2.jpg" style="width: 800px; height: 321px;" /><pre><strong>输入:</strong>matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]<strong><br />输出:</strong>[[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>matrix = [[1]]<strong><br />输出:</strong>[[1]]</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>matrix = [[1,2],[3,4]]<strong><br />输出:</strong>[[3,1],[4,2]]</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>matrix.length == n</code></li> <li><code>matrix[i].length == n</code></li> <li><code>1 <= n <= 20</code></li> <li><code>-1000 <= matrix[i][j] <= 1000</code></li></ul>
<p><p>给定一个 <em>n </em>× <em>n</em> 的二维矩阵 <code>matrix</code> 表示一个图像。请你将图像顺时针旋转 90 度。</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> 旋转图像,这意味着你需要直接修改输入的二维矩阵。<strong>请不要 </strong>使用另一个矩阵来旋转图像。</p><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0048.Rotate%20Image/images/mat1.jpg" style="width: 642px; height: 242px;" /><pre><strong>输入:</strong>matrix = [[1,2,3],[4,5,6],[7,8,9]]<strong><br />输出:</strong>[[7,4,1],[8,5,2],[9,6,3]]</pre><p><strong>示例 2:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0048.Rotate%20Image/images/mat2.jpg" style="width: 800px; height: 321px;" /><pre><strong>输入:</strong>matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]<strong><br />输出:</strong>[[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>matrix = [[1]]<strong><br />输出:</strong>[[1]]</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>matrix = [[1,2],[3,4]]<strong><br />输出:</strong>[[3,1],[4,2]]</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>matrix.length == n</code></li> <li><code>matrix[i].length == n</code></li> <li><code>1 <= n <= 20</code></li> <li><code>-1000 <= matrix[i][j] <= 1000</code></li></ul>
<p>以下错误的选项是?</p></p>
## aop
### before
```cpp
```
### after
```cpp
```
## 答案
```cpp
class Solution
{
public:
void rotate(vector<vector<int>> &matrix)
{
int n = matrix.size();
int temp = 0;
for (int i = 0; i < n; ++i)
{
for (int j = i; j < n; ++j)
{
temp = matrix[i][j];
matrix[i][j] = matrix[n - j - 1][i];
matrix[j][i] = temp;
}
}
for (int i = 0; i < n; ++i)
{
reverse(matrix[i].begin(), matrix[i].end());
}
}
};
```
## 选项
### A
```cpp
class Solution
{
public:
void rotate(vector<vector<int>> &matrix)
{
int temp = 0;
int n = matrix.size();
for (int i = 0; i < n / 2; i++)
{
for (int j = i; j < n - i - 1; j++)
{
temp = matrix[i][j];
matrix[i][j] = matrix[n - j - 1][i];
matrix[n - j - 1][i] = matrix[n - i - 1][n - j - 1];
matrix[n - i - 1][n - j - 1] = matrix[j][n - i - 1];
matrix[j][n - i - 1] = temp;
}
}
}
};
```
### B
```cpp
class Solution
{
public:
void rotate(vector<vector<int>> &matrix)
{
int n = matrix.size();
int tmp = 0;
for (int i = 0; i < n; i++)
for (int j = i; j < n; j++)
{
tmp = matrix[j][i];
matrix[j][i] = matrix[i][j];
matrix[i][j] = tmp;
}
for (int i = 0; i < n; i++)
for (int j = 0; j < n / 2; j++)
{
tmp = matrix[i][j];
matrix[i][j] = matrix[i][n - j - 1];
matrix[i][n - j - 1] = tmp;
}
}
};
```
### C
```cpp
class Solution
{
public:
void rotate(vector<vector<int>> &matrix)
{
int n = matrix.size();
vector<vector<int>> matrix_temp = matrix;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
matrix_temp[j][n - i - 1] = matrix[i][j];
}
}
matrix = matrix_temp;
}
};
```
{
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"keywords": [
"leetcode",
"螺旋矩阵"
],
"children": [],
"export": [
"solution.json"
],
"title": "螺旋矩阵"
}
\ No newline at end of file
<p>给你一个 <code>m</code><code>n</code> 列的矩阵 <code>matrix</code> ,请按照 <strong>顺时针螺旋顺序</strong> ,返回矩阵中的所有元素。</p><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0054.Spiral%20Matrix/images/spiral1.jpg" style="width: 242px; height: 242px;" /><pre><strong>输入:</strong>matrix = [[1,2,3],[4,5,6],[7,8,9]]<strong><br />输出:</strong>[1,2,3,6,9,8,7,4,5]</pre><p><strong>示例 2:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0054.Spiral%20Matrix/images/spiral.jpg" style="width: 322px; height: 242px;" /><pre><strong>输入:</strong>matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]<strong><br />输出:</strong>[1,2,3,4,8,12,11,10,9,5,6,7]</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 <= 10</code></li> <li><code>-100 <= matrix[i][j] <= 100</code></li></ul>
\ No newline at end of file
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
vector<int> spiralOrder(vector<vector<int>> &matrix)
{
vector<int> res;
int hor_top = 0;
int hor_bottom = matrix.size() - 1;
int ver_left = 0;
int ver_right = matrix[0].size() - 1;
int direction = 0;
while (hor_top <= hor_bottom && ver_left <= ver_right)
{
switch (direction)
{
case 0:
for (int i = ver_left; i <= ver_right; i++)
{
res.push_back(matrix[hor_top][i]);
}
hor_top++;
break;
case 1:
for (int i = hor_top; i <= hor_bottom; i++)
{
res.push_back(matrix[i][ver_right]);
}
ver_right--;
break;
case 2:
for (int i = ver_right; i >= ver_left; i--)
{
res.push_back(matrix[hor_bottom][i]);
}
hor_bottom--;
break;
case 3:
for (int i = hor_bottom; i >= hor_top; i--)
{
res.push_back(matrix[i][ver_left]);
}
ver_left++;
break;
default:
break;
}
direction++;
direction %= 4;
}
return res;
}
};
\ No newline at end of file
{
"type": "code_options",
"author": "CSDN.net",
"source": "solution.md",
"exercise_id": "52110cd446c14664843d4143fc37ca93"
}
\ No newline at end of file
# 螺旋矩阵
<p>给你一个 <code>m</code> 行 <code>n</code> 列的矩阵 <code>matrix</code> ,请按照 <strong>顺时针螺旋顺序</strong> ,返回矩阵中的所有元素。</p><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0054.Spiral%20Matrix/images/spiral1.jpg" style="width: 242px; height: 242px;" /><pre><strong>输入:</strong>matrix = [[1,2,3],[4,5,6],[7,8,9]]<strong><br />输出:</strong>[1,2,3,6,9,8,7,4,5]</pre><p><strong>示例 2:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0054.Spiral%20Matrix/images/spiral.jpg" style="width: 322px; height: 242px;" /><pre><strong>输入:</strong>matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]<strong><br />输出:</strong>[1,2,3,4,8,12,11,10,9,5,6,7]</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 <= 10</code></li> <li><code>-100 <= matrix[i][j] <= 100</code></li></ul>
<p>以下错误的选项是?</p>
## aop
### before
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
```cpp
```
## 答案
```cpp
class Solution
{
public:
vector<int> printMatrix(vector<vector<int>> matrix)
{
vector<int> res;
if (matrix.size() == 0)
return res;
int r1 = 0, r2 = matrix.size() - 1;
int c1 = 0, c2 = matrix[0].size() - 1;
int i = 0, j = 0;
int sum = matrix.size() * matrix[0].size();
while (res.size() < sum)
{
if (j == c1 && i == r1)
{
while (j <= c2)
res.push_back(matrix[i][j++]);
++r1;
--j;
++i;
}
else if (j == c2 && i == r1)
{
while (i <= r2)
res.push_back(matrix[i++][j]);
++c2;
--i;
--j;
}
else if (j == c2 && i == r2)
{
while (j >= c1)
res.push_back(matrix[i][j--]);
--r2;
++j;
--i;
}
else
{
while (i >= r1)
res.push_back(matrix[i--][j]);
++c1;
++i;
++j;
}
}
return res;
}
};
```
## 选项
### A
```cpp
class Solution
{
public:
vector<int> spiralOrder(vector<vector<int>> &matrix)
{
vector<int> res;
int hor_top = 0;
int hor_bottom = matrix.size() - 1;
int ver_left = 0;
int ver_right = matrix[0].size() - 1;
int direction = 0;
while (hor_top <= hor_bottom && ver_left <= ver_right)
{
switch (direction)
{
case 0:
for (int i = ver_left; i <= ver_right; i++)
{
res.push_back(matrix[hor_top][i]);
}
hor_top++;
break;
case 1:
for (int i = hor_top; i <= hor_bottom; i++)
{
res.push_back(matrix[i][ver_right]);
}
ver_right--;
break;
case 2:
for (int i = ver_right; i >= ver_left; i--)
{
res.push_back(matrix[hor_bottom][i]);
}
hor_bottom--;
break;
case 3:
for (int i = hor_bottom; i >= hor_top; i--)
{
res.push_back(matrix[i][ver_left]);
}
ver_left++;
break;
default:
break;
}
direction++;
direction %= 4;
}
return res;
}
};
```
### B
```cpp
class Solution
{
public:
vector<int> spiralOrder(vector<vector<int>> &matrix)
{
vector<int> res;
if (matrix.size() == 0)
return res;
int up = 0, down = matrix.size() - 1, left = 0, right = matrix[0].size() - 1;
while (up <= down && left <= right)
{
for (int i = left; i <= right; i++)
res.push_back(matrix[up][i]);
for (int i = up + 1; i <= down; i++)
res.push_back(matrix[i][right]);
if (up < down && left < right)
{
for (int i = right - 1; i > left; i--)
res.push_back(matrix[down][i]);
for (int i = down; i > up; i--)
res.push_back(matrix[i][left]);
}
up++;
down--;
left++;
right--;
}
return res;
}
};
```
### C
```cpp
class Solution
{
public:
vector<int> spiralOrder(vector<vector<int>> &matrix)
{
vector<int> path;
if (matrix.empty())
return path;
int rows = matrix.size(), cols = matrix[0].size();
int top = 0, bottom = rows - 1, left = 0, right = cols - 1;
while (true)
{
for (int col = left; col <= right; ++col)
path.push_back(matrix[top][col]);
++top;
if (top > bottom)
break;
for (int row = top; row <= bottom; ++row)
path.push_back(matrix[row][right]);
--right;
if (right < left)
break;
for (int col = right; col >= left; --col)
path.push_back(matrix[bottom][col]);
--bottom;
if (top > bottom)
break;
for (int row = bottom; row >= top; --row)
path.push_back(matrix[row][left]);
++left;
if (right < left)
break;
}
return path;
}
};
```
{
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"keywords": [
"leetcode",
"合并区间"
],
"children": [],
"export": [
"solution.json"
],
"title": "合并区间"
}
\ No newline at end of file
<p>以数组 <code>intervals</code> 表示若干个区间的集合,其中单个区间为 <code>intervals[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> 。请你合并所有重叠的区间,并返回一个不重叠的区间数组,该数组需恰好覆盖输入中的所有区间。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>intervals = [[1,3],[2,6],[8,10],[15,18]]<strong><br />输出:</strong>[[1,6],[8,10],[15,18]]<strong><br />解释:</strong>区间 [1,3] 和 [2,6] 重叠, 将它们合并为 [1,6].</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>intervals = [[1,4],[4,5]]<strong><br />输出:</strong>[[1,5]]<strong><br />解释:</strong>区间 [1,4] 和 [4,5] 可被视为重叠区间。</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>1 <= intervals.length <= 10<sup>4</sup></code></li> <li><code>intervals[i].length == 2</code></li> <li><code>0 <= start<sub>i</sub> <= end<sub>i</sub> <= 10<sup>4</sup></code></li></ul>
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static int compare(const void *a, const void *b)
{
return ((int *)a)[0] - ((int *)b)[0];
}
int **merge(int **intervals, int intervalsSize, int *intervalsColSize, int *returnSize, int **returnColumnSizes)
{
if (intervalsSize == 0)
{
*returnSize = 0;
return intervals;
}
int i, len = 0;
int *tmp = malloc(intervalsSize * 2 * sizeof(int));
for (i = 0; i < intervalsSize; i++)
{
tmp[i * 2] = intervals[i][0];
tmp[i * 2 + 1] = intervals[i][1];
}
qsort(tmp, intervalsSize, 2 * sizeof(int), compare);
intervals[0][0] = tmp[0];
intervals[0][1] = tmp[1];
for (i = 1; i < intervalsSize; i++)
{
if (tmp[i * 2] > intervals[len][1])
{
len++;
intervals[len][0] = tmp[i * 2];
intervals[len][1] = tmp[i * 2 + 1];
}
else if (tmp[i * 2 + 1] > intervals[len][1])
{
intervals[len][1] = tmp[i * 2 + 1];
}
}
len += 1;
*returnSize = len;
*returnColumnSizes = malloc(len * sizeof(int));
for (i = 0; i < len; i++)
{
(*returnColumnSizes)[i] = 2;
}
return intervals;
}
int main(int argc, char **argv)
{
if (argc < 1 || argc % 2 == 0)
{
fprintf(stderr, "Usage: ./test s0 e0 s1 e1...");
exit(-1);
}
int i, count = 0;
int *sizes = malloc((argc - 1) / 2 * sizeof(int));
int **intervals = malloc((argc - 1) / 2 * sizeof(int *));
for (i = 0; i < (argc - 1) / 2; i++)
{
sizes[i] = 2;
intervals[i] = malloc(2 * sizeof(int));
intervals[i][0] = atoi(argv[i * 2 + 1]);
intervals[i][1] = atoi(argv[i * 2 + 2]);
}
int *col_sizes;
int **results = merge(intervals, (argc - 1) / 2, sizes, &count, &col_sizes);
for (i = 0; i < count; i++)
{
printf("[%d,%d]\n", results[i][0], results[i][1]);
}
return 0;
}
\ No newline at end of file
{
"type": "code_options",
"author": "CSDN.net",
"source": "solution.md",
"exercise_id": "474bcf456a18412eafcb156d6f616acd"
}
\ No newline at end of file
# 合并区间
<p>以数组 <code>intervals</code> 表示若干个区间的集合,其中单个区间为 <code>intervals[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> 。请你合并所有重叠的区间,并返回一个不重叠的区间数组,该数组需恰好覆盖输入中的所有区间。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>intervals = [[1,3],[2,6],[8,10],[15,18]]<strong><br />输出:</strong>[[1,6],[8,10],[15,18]]<strong><br />解释:</strong>区间 [1,3] 和 [2,6] 重叠, 将它们合并为 [1,6].</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>intervals = [[1,4],[4,5]]<strong><br />输出:</strong>[[1,5]]<strong><br />解释:</strong>区间 [1,4] 和 [4,5] 可被视为重叠区间。</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>1 <= intervals.length <= 10<sup>4</sup></code></li> <li><code>intervals[i].length == 2</code></li> <li><code>0 <= start<sub>i</sub> <= end<sub>i</sub> <= 10<sup>4</sup></code></li></ul>
<p>以下错误的选项是?</p>
## aop
### before
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
```cpp
```
## 答案
```cpp
class Solution
{
public:
vector<vector<int>> merge(vector<vector<int>> &intervals)
{
vector<vector<int>> res;
sort(intervals.begin(), intervals.end(), [](const auto &u, const auto &v)
{
if (u[0] == v[0])
return u[1] > v[1];
else
return u[0] < v[0];
});
auto it = intervals.begin();
int first = (*it)[0], second = (*it)[1];
for (it++; it != intervals.end(); it++)
{
if ((*it)[0] <= second)
second = max(second, (*it)[0]);
else
{
res.push_back({first, second});
first = (*it)[0];
second = (*it)[1];
}
}
res.push_back({first, second});
return res;
}
};
```
## 选项
### A
```cpp
class Solution
{
public:
vector<vector<int>> merge(vector<vector<int>> &intervals)
{
vector<vector<int>> res;
sort(intervals.begin(), intervals.end());
for (auto it = intervals.begin(); it != intervals.end(); it++)
{
int first = (*it)[0], second = (*it)[1];
if (!res.size() || res.back()[1] < first)
res.push_back({first, second});
else
res.back()[1] = max(res.back()[1], second);
}
return res;
}
};
```
### B
```cpp
class Solution
{
public:
vector<vector<int>> merge(vector<vector<int>> &intervals)
{
sort(intervals.begin(), intervals.end());
vector<vector<int>> res;
for (int i = 0; i < intervals.size();)
{
int t = intervals[i][1];
int j = i + 1;
while (j < intervals.size() && intervals[j][0] <= t)
{
t = max(t, intervals[j][1]);
j++;
}
res.push_back({intervals[i][0], t});
i = j;
}
return res;
}
};
```
### C
```cpp
class Solution
{
public:
static bool cmp(const vector<int> &a, const vector<int> &b)
{
if (a[0] == b[0])
return a[1] > b[1];
return a[0] < b[0];
}
vector<vector<int>> merge(vector<vector<int>> &intervals)
{
if (intervals.empty())
return intervals;
vector<vector<int>> res;
int count = 0;
sort(intervals.begin(), intervals.end(), cmp);
vector<int> temp;
temp.push_back(intervals[0][0]);
temp.push_back(intervals[0][1]);
res.push_back(temp);
for (int i = 1; i < intervals.size(); i++)
{
if (res[count][1] >= intervals[i][0])
{
if (res[count][1] <= intervals[i][1])
{
res[count][1] = intervals[i][1];
}
}
else
{
count++;
temp[0] = intervals[i][0];
temp[1] = intervals[i][1];
res.push_back(temp);
}
}
return res;
}
};
```
{
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"keywords": [
"leetcode",
"插入区间"
],
"children": [],
"export": [
"solution.json"
],
"title": "插入区间"
}
\ No newline at end of file
<p>给你一个<strong> 无重叠的</strong><em></em>按照区间起始端点排序的区间列表。</p>
<p>在列表中插入一个新的区间,你需要确保列表中的区间仍然有序且不重叠(如果有必要的话,可以合并区间)。</p>
<p> </p>
<p><strong>示例 1:</strong></p>
<pre><strong>输入:</strong>intervals = [[1,3],[6,9]], newInterval = [2,5]<strong><br />输出:</strong>[[1,5],[6,9]]</pre>
<p><strong>示例 2:</strong></p>
<pre><strong>输入:</strong>intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8]<strong><br />输出:</strong>[[1,2],[3,10],[12,16]]<strong><br />解释:</strong>这是因为新的区间 [4,8] 与 [3,5],[6,7],[8,10] 重叠。</pre>
<p><strong>示例 3:</strong></p>
<pre><strong>输入:</strong>intervals = [], newInterval = [5,7]<strong><br />输出:</strong>[[5,7]]</pre>
<p><strong>示例 4:</strong></p>
<pre><strong>输入:</strong>intervals = [[1,5]], newInterval = [2,3]<strong><br />输出:</strong>[[1,5]]</pre>
<p><strong>示例 5:</strong></p>
<pre><strong>输入:</strong>intervals = [[1,5]], newInterval = [2,7]<strong><br />输出:</strong>[[1,7]]</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>0 <= intervals.length <= 10<sup>4</sup></code></li>
<li><code>intervals[i].length == 2</code></li>
<li><code>0 <intervals[i][0] <= intervals[i][1] <= 10<sup>5</sup></code></li>
<li><code>intervals</code> 根据 <code>intervals[i][0]</code><strong>升序</strong> 排列</li>
<li><code>newInterval.length == 2</code></li>
<li><code>0 <newInterval[0] <= newInterval[1] <= 10<sup>5</sup></code></li>
</ul>
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
static int compare(const void *a, const void *b)
{
return ((int *)a)[0] - ((int *)b)[0];
}
int **insert(int **intervals, int intervalsSize, int *intervalsColSize, int *newInterval,
int newIntervalSize, int *returnSize, int **returnColumnSizes)
{
int i, len = 0;
int *tmp = malloc((intervalsSize + 1) * 2 * sizeof(int));
for (i = 0; i < intervalsSize; i++)
{
tmp[i * 2] = intervals[i][0];
tmp[i * 2 + 1] = intervals[i][1];
}
tmp[i * 2] = newInterval[0];
tmp[i * 2 + 1] = newInterval[1];
qsort(tmp, intervalsSize + 1, 2 * sizeof(int), compare);
int **results = malloc((intervalsSize + 1) * sizeof(int *));
results[0] = malloc(2 * sizeof(int));
results[0][0] = tmp[0];
results[0][1] = tmp[1];
for (i = 1; i < intervalsSize + 1; i++)
{
results[i] = malloc(2 * sizeof(int));
if (tmp[i * 2] > results[len][1])
{
len++;
results[len][0] = tmp[i * 2];
results[len][1] = tmp[i * 2 + 1];
}
else if (tmp[i * 2 + 1] > results[len][1])
{
results[len][1] = tmp[i * 2 + 1];
}
}
len += 1;
*returnSize = len;
*returnColumnSizes = malloc(len * sizeof(int));
for (i = 0; i < len; i++)
{
(*returnColumnSizes)[i] = 2;
}
return results;
}
int main(int argc, char **argv)
{
if (argc < 3 || argc % 2 == 0)
{
fprintf(stderr, "Usage: ./test new_s new_e s0 e0 s1 e1...");
exit(-1);
}
int new_interv[2];
new_interv[0] = atoi(argv[1]);
new_interv[1] = atoi(argv[2]);
int i, count = 0;
int *size = malloc((argc - 3) / 2 * sizeof(int));
int **intervals = malloc((argc - 3) / 2 * sizeof(int *));
for (i = 0; i < (argc - 3) / 2; i++)
{
intervals[i] = malloc(2 * sizeof(int));
intervals[i][0] = atoi(argv[i * 2 + 3]);
intervals[i][1] = atoi(argv[i * 2 + 4]);
}
int *col_sizes;
int **results = insert(intervals, (argc - 3) / 2, size, new_interv, 2, &count, &col_sizes);
for (i = 0; i < count; i++)
{
printf("[%d,%d]\n", results[i][0], results[i][1]);
}
return 0;
}
\ No newline at end of file
{
"type": "code_options",
"author": "CSDN.net",
"source": "solution.md",
"exercise_id": "d17234eba287409780673653f6c3866b"
}
\ No newline at end of file
# 插入区间
<p>给你一个<strong> 无重叠的</strong><em></em>按照区间起始端点排序的区间列表。</p>
<p>在列表中插入一个新的区间,你需要确保列表中的区间仍然有序且不重叠(如果有必要的话,可以合并区间)。</p>
<p> </p>
<p><strong>示例 1:</strong></p>
<pre><strong>输入:</strong>intervals = [[1,3],[6,9]], newInterval = [2,5]<strong><br />输出:</strong>[[1,5],[6,9]]</pre>
<p><strong>示例 2:</strong></p>
<pre><strong>输入:</strong>intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8]<strong><br />输出:</strong>[[1,2],[3,10],[12,16]]<strong><br />解释:</strong>这是因为新的区间 [4,8] 与 [3,5],[6,7],[8,10] 重叠。</pre>
<p><strong>示例 3:</strong></p>
<pre><strong>输入:</strong>intervals = [], newInterval = [5,7]<strong><br />输出:</strong>[[5,7]]</pre>
<p><strong>示例 4:</strong></p>
<pre><strong>输入:</strong>intervals = [[1,5]], newInterval = [2,3]<strong><br />输出:</strong>[[1,5]]</pre>
<p><strong>示例 5:</strong></p>
<pre><strong>输入:</strong>intervals = [[1,5]], newInterval = [2,7]<strong><br />输出:</strong>[[1,7]]</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>0 <= intervals.length <= 10<sup>4</sup></code></li>
<li><code>intervals[i].length == 2</code></li>
<li><code>0 <intervals[i][0] <= intervals[i][1] <= 10<sup>5</sup></code></li>
<li><code>intervals</code> 根据 <code>intervals[i][0]</code><strong>升序</strong> 排列</li>
<li><code>newInterval.length == 2</code></li>
<li><code>0 <newInterval[0] <= newInterval[1] <= 10<sup>5</sup></code></li>
</ul>
<p>以下错误的选项是?</p>
## aop
### before
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
```cpp
```
## 答案
```cpp
class Solution
{
public:
vector<vector<int>> insert(vector<vector<int>> &intervals, vector<int> &newInterval)
{
intervals.push_back(newInterval);
sort(intervals.begin(), intervals.end());
int n = intervals.size();
vector<vector<int>> ans;
for (int i = 0; i < n; i++)
{
int l = intervals[i][0], r = intervals[i][1];
if (!ans.size())
ans.push_back({l, r});
else
ans.back()[1] = max(ans.back()[1], r);
}
return ans;
}
};
```
## 选项
### A
```cpp
class Solution
{
public:
vector<vector<int>> insert(vector<vector<int>> &intervals, vector<int> &newInterval)
{
if (intervals.size() == 0)
return {newInterval};
vector<vector<int>> res;
int lpos = 0, rpos = 0;
for (int i = 0; i < intervals.size(); i++)
{
if (intervals[i][0] < newInterval[0])
lpos++;
if (intervals[i][1] < newInterval[1])
rpos++;
}
if (lpos > rpos)
{
return intervals;
}
if (rpos == 0)
{
if (intervals[rpos][0] > newInterval[1])
{
res.push_back(newInterval);
for (int i = 0; i < intervals.size(); i++)
{
res.push_back(intervals[i]);
}
}
else
{
res.push_back({newInterval[0], intervals[0][1]});
for (int i = 1; i < intervals.size(); i++)
{
res.push_back(intervals[i]);
}
}
}
else if (lpos == 0)
{
if (rpos == intervals.size())
{
res.push_back(newInterval);
}
else if (intervals[rpos][0] > newInterval[1])
{
res.push_back(newInterval);
for (int i = rpos; i < intervals.size(); i++)
{
res.push_back(intervals[i]);
}
}
else if (intervals[rpos][0] <= newInterval[1])
{
res.push_back({newInterval[0], intervals[rpos][1]});
for (int i = rpos + 1; i < intervals.size(); i++)
{
res.push_back(intervals[i]);
}
}
}
else
{
if (intervals[lpos - 1][1] >= newInterval[0] && rpos == intervals.size())
{
for (int i = 0; i < lpos - 1; i++)
{
res.push_back(intervals[i]);
}
res.push_back({intervals[lpos - 1][0], newInterval[1]});
}
else if (intervals[lpos - 1][1] < newInterval[0] && rpos == intervals.size())
{
for (int i = 0; i < lpos; i++)
{
res.push_back(intervals[i]);
}
res.push_back(newInterval);
}
else if (intervals[lpos - 1][1] >= newInterval[0] && intervals[rpos][0] <= newInterval[1])
{
for (int i = 0; i < lpos - 1; i++)
{
res.push_back(intervals[i]);
}
res.push_back({intervals[lpos - 1][0], intervals[rpos][1]});
for (int i = rpos + 1; i < intervals.size(); i++)
{
res.push_back(intervals[i]);
}
}
else if (intervals[lpos - 1][1] >= newInterval[0] && intervals[rpos][0] > newInterval[1])
{
for (int i = 0; i < lpos - 1; i++)
{
res.push_back(intervals[i]);
}
res.push_back({intervals[lpos - 1][0], newInterval[1]});
for (int i = rpos; i < intervals.size(); i++)
{
res.push_back(intervals[i]);
}
}
else if (intervals[lpos - 1][1] < newInterval[0] && intervals[rpos][0] > newInterval[1])
{
for (int i = 0; i <= lpos - 1; i++)
{
res.push_back(intervals[i]);
}
res.push_back({newInterval[0], newInterval[1]});
for (int i = rpos; i < intervals.size(); i++)
{
res.push_back(intervals[i]);
}
}
else if (intervals[lpos - 1][1] < newInterval[0] && intervals[rpos][0] <= newInterval[1])
{
for (int i = 0; i <= lpos - 1; i++)
{
res.push_back(intervals[i]);
}
res.push_back({newInterval[0], intervals[rpos][1]});
for (int i = rpos + 1; i < intervals.size(); i++)
{
res.push_back(intervals[i]);
}
}
}
return res;
}
};
```
### B
```cpp
class Solution
{
public:
vector<vector<int>> insert(vector<vector<int>> &intervals, vector<int> &newInterval)
{
if (newInterval.empty())
return intervals;
else if (intervals.empty())
{
intervals.push_back(newInterval);
return intervals;
}
vector<vector<int>> res;
int i = 0;
while (i < intervals.size() && intervals[i][1] < newInterval[0])
{
res.push_back(intervals[i]);
i++;
}
while (i < intervals.size() && intervals[i][0] <= newInterval[1])
{
newInterval[0] = min(intervals[i][0], newInterval[0]);
newInterval[1] = max(intervals[i][1], newInterval[1]);
i++;
}
res.push_back(newInterval);
while (i < intervals.size())
{
res.push_back(intervals[i]);
i++;
}
return res;
}
};
```
### C
```cpp
class Solution
{
public:
vector<vector<int>> insert(vector<vector<int>> &intervals, vector<int> &newInterval)
{
if (intervals.empty())
{
intervals.push_back(newInterval);
return intervals;
}
int n = intervals.size();
int i = 0, j = n - 1;
vector<vector<int>> ans;
for (i = 0; i < n; i++)
if (newInterval[0] <= intervals[i][1])
break;
for (j = n - 1; j >= 0; j--)
if (newInterval[1] >= intervals[j][0])
break;
if (i == n)
intervals.push_back(newInterval);
else if (j == -1)
intervals.insert(intervals.begin(), newInterval);
else if (i > j)
{
intervals.insert(intervals.begin() + i, newInterval);
}
else if (i == j)
{
intervals[i][0] = min(intervals[i][0], newInterval[0]);
intervals[i][1] = max(intervals[i][1], newInterval[1]);
}
else
{
int minN = min(intervals[i][0], newInterval[0]);
int maxN = max(intervals[j][1], newInterval[1]);
intervals.erase(intervals.begin() + i, intervals.begin() + j + 1);
intervals.insert(intervals.begin() + i, {minN, maxN});
}
return intervals;
}
};
```
{
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"keywords": [
"leetcode",
"螺旋矩阵 II"
],
"children": [],
"export": [
"solution.json"
],
"title": "螺旋矩阵 II"
}
\ No newline at end of file
<p>给你一个正整数 <code>n</code> ,生成一个包含 <code>1</code> 到 <code>n<sup>2</sup></code> 所有元素,且元素按顺时针顺序螺旋排列的 <code>n x n</code> 正方形矩阵 <code>matrix</code></p><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0059.Spiral%20Matrix%20II/images/spiraln.jpg" style="width: 242px; height: 242px;" /><pre><strong>输入:</strong>n = 3<strong><br />输出:</strong>[[1,2,3],[8,9,4],[7,6,5]]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>n = 1<strong><br />输出:</strong>[[1]]</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>1 <= n <= 20</code></li></ul>
\ No newline at end of file
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
vector<vector<int>> generateMatrix(int n)
{
vector<vector<int>> matrix(n, vector<int>(n));
int direction = 0;
int hor_top = 0;
int hor_bottom = n - 1;
int ver_left = 0;
int ver_right = n - 1;
int num = 0;
while (num < n * n)
{
switch (direction)
{
case 0:
for (int i = ver_left; i <= ver_right; i++)
{
matrix[hor_top][i] = ++num;
}
hor_top++;
break;
case 1:
for (int i = hor_top; i <= hor_bottom; i++)
{
matrix[i][ver_right] = ++num;
}
ver_right--;
break;
case 2:
for (int i = ver_right; i >= ver_left; i--)
{
matrix[hor_bottom][i] = ++num;
}
hor_bottom--;
break;
case 3:
for (int i = hor_bottom; i >= hor_top; i--)
{
matrix[i][ver_left] = ++num;
}
ver_left++;
break;
}
direction++;
direction %= 4;
}
return matrix;
}
};
\ No newline at end of file
{
"type": "code_options",
"author": "CSDN.net",
"source": "solution.md",
"exercise_id": "b0e07b1e65d3417ba93daba5cf2281b8"
}
\ No newline at end of file
# 螺旋矩阵 II
<p>给你一个正整数 <code>n</code> ,生成一个包含 <code>1</code> 到 <code>n<sup>2</sup></code> 所有元素,且元素按顺时针顺序螺旋排列的 <code>n x n</code> 正方形矩阵 <code>matrix</code> 。</p><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0059.Spiral%20Matrix%20II/images/spiraln.jpg" style="width: 242px; height: 242px;" /><pre><strong>输入:</strong>n = 3<strong><br />输出:</strong>[[1,2,3],[8,9,4],[7,6,5]]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>n = 1<strong><br />输出:</strong>[[1]]</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>1 <= n <= 20</code></li></ul>
<p>以下错误的选项是?</p>
## aop
### before
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
```cpp
int main()
{
Solution sol;
int n = 3;
vector<vector<int>> res;
res = sol.generateMatrix(n);
for (auto i : res)
{
for (auto j : i)
cout << j << " ";
cout << endl;
}
return 0;
}
```
## 答案
```cpp
class Solution
{
public:
vector<vector<int>> generateMatrix(int n)
{
vector<vector<int>> ans(n, vector<int>(n, 0));
int c = n / 2;
int val = 1;
for (int i = 0; i < c; ++i, n -= 2)
{
for (int col = i; col < i + n; ++col)
ans[i][col] = val++;
if (n == 1)
return ans;
for (int row = i + 1; row < i + n; ++row)
ans[row][i + n - 1] = val++;
for (int col = i + n - 2; col >= i; --col)
ans[i + n - 1][col] = val++;
for (int row = i + n - 2; row > i; --row)
ans[row][i] = val++;
}
return ans;
}
};
```
## 选项
### A
```cpp
class Solution
{
public:
vector<vector<int>> generateMatrix(int n)
{
vector<vector<int>> matrix(n, vector<int>(n));
int u = 0, d = n - 1, l = 0, r = n - 1, k = 0;
while (true)
{
for (int col = l; col <= r; col++)
matrix[u][col] = ++k;
if (++u > d)
break;
for (int row = u; row <= d; row++)
matrix[row][r] = ++k;
if (--r < l)
break;
for (int col = r; col >= l; col--)
matrix[d][col] = ++k;
if (--d < u)
break;
for (int row = d; row >= u; row--)
matrix[row][l] = ++k;
if (++l > r)
break;
}
return matrix;
}
};
```
### B
```cpp
class Solution
{
public:
vector<vector<int>> generateMatrix(int n)
{
vector<vector<int>> result(n, vector<int>(n, 0));
int top = 0, right = n - 1, left = 0, bottom = n - 1;
int k = 1;
while (k <= n * n)
{
for (int i = left; i <= right; i++)
result[top][i] = k++;
top++;
for (int i = top; i <= bottom; i++)
result[i][right] = k++;
right--;
for (int i = right; i >= left; i--)
result[bottom][i] = k++;
bottom--;
for (int i = bottom; i >= top; i--)
result[i][left] = k++;
left++;
}
return result;
}
};
```
### C
```cpp
class Solution
{
public:
vector<vector<int>> generateMatrix(int n)
{
vector<vector<int>> matrix(n, vector<int>(n));
int direction = 0;
int hor_top = 0;
int hor_bottom = n - 1;
int ver_left = 0;
int ver_right = n - 1;
int num = 0;
while (num < n * n)
{
switch (direction)
{
case 0:
for (int i = ver_left; i <= ver_right; i++)
{
matrix[hor_top][i] = ++num;
}
hor_top++;
break;
case 1:
for (int i = hor_top; i <= hor_bottom; i++)
{
matrix[i][ver_right] = ++num;
}
ver_right--;
break;
case 2:
for (int i = ver_right; i >= ver_left; i--)
{
matrix[hor_bottom][i] = ++num;
}
hor_bottom--;
break;
case 3:
for (int i = hor_bottom; i >= hor_top; i--)
{
matrix[i][ver_left] = ++num;
}
ver_left++;
break;
}
direction++;
direction %= 4;
}
return matrix;
}
};
```
{
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"keywords": [
"leetcode",
"括号生成"
],
"children": [],
"export": [
"solution.json"
],
"title": "括号生成"
}
\ No newline at end of file
<p>数字 <code>n</code> 代表生成括号的对数,请你设计一个函数,用于能够生成所有可能的并且 <strong>有效的 </strong>括号组合。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>n = 3<strong><br />输出:</strong>["((()))","(()())","(())()","()(())","()()()"]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>n = 1<strong><br />输出:</strong>["()"]</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>1 <= n <= 8</code></li></ul>
\ No newline at end of file
class Solution
{
public:
void gen(string &p, int lc, int rc, vector<string> &r, int n)
{
if (lc > n)
return;
if (lc == n && rc == n)
{
r.push_back(p);
return;
}
p.push_back('(');
lc++;
gen(p, lc, rc, r, n);
p.pop_back();
lc--;
if (lc > rc)
{
p.push_back(')');
rc++;
gen(p, lc, rc, r, n);
p.pop_back();
rc--;
}
}
vector<string> generateParenthesis(int n)
{
string p;
int lc = 0, rc = 0;
vector<string> r;
gen(p, lc, rc, r, n);
return r;
}
};
\ No newline at end of file
{
"type": "code_options",
"author": "CSDN.net",
"source": "solution.md",
"exercise_id": "03bbe42adf204341bf303c1f7bef2961"
}
\ No newline at end of file
# 括号生成
<p>数字 <code>n</code> 代表生成括号的对数,请你设计一个函数,用于能够生成所有可能的并且 <strong>有效的 </strong>括号组合。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>n = 3<strong><br />输出:</strong>["((()))","(()())","(())()","()(())","()()()"]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>n = 1<strong><br />输出:</strong>["()"]</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>1 <= n <= 8</code></li></ul>
<p><p>数字 <code>n</code> 代表生成括号的对数,请你设计一个函数,用于能够生成所有可能的并且 <strong>有效的 </strong>括号组合。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>n = 3<strong><br />输出:</strong>["((()))","(()())","(())()","()(())","()()()"]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>n = 1<strong><br />输出:</strong>["()"]</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>1 <= n <= 8</code></li></ul>
<p>以下错误的选项是?</p></p>
## aop
### before
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
```cpp
int main()
{
Solution sol;
int n = 3;
vector<string> res;
res = sol.generateParenthesis(n);
for (auto i : res)
cout << i << endl;
return 0;
}
```
## 答案
```cpp
class Solution
{
public:
vector<string> generateParenthesis(int n)
{
vector<string> ans;
string s;
back(0, 2 * n, 0, 0, s, ans);
return ans;
}
void back(int left, int right, int l_n, int r_n, string &s, vector<string> &ans)
{
if (r_n > l_n || l_n > right)
return;
if (left == right)
{
ans.push_back(s);
return;
}
s += '(';
l_n++;
left++;
back(left, right, l_n, r_n, s, ans);
s.erase(s.end() - 1);
l_n--;
s += ')';
r_n++;
back(left, right, l_n, r_n, s, ans);
s.erase(s.end() - 1);
r_n--;
}
};
```
## 选项
### A
```cpp
class Solution
{
public:
void generateParenthesis(string i, int left, int right, vector<string> &output)
{
if (left == 0 && right == 0)
{
output.push_back(i);
return;
}
if (left > 0)
{
generateParenthesis(i + '(', left - 1, right, output);
}
if (left < right)
{
generateParenthesis(i + ')', left, right - 1, output);
}
}
vector<string> generateParenthesis(int n)
{
string s = "";
vector<string> output;
generateParenthesis(s, n, n, output);
return output;
}
};
```
### B
```cpp
class Solution
{
public:
vector<string> res;
void backtrack(int n, int key, string cur)
{
if (key < 0 || key > n)
return;
if (cur.size() == 2 * n)
{
if (key == 0)
res.push_back(cur);
return;
}
backtrack(n, key - 1, cur + ')');
backtrack(n, key + 1, cur + '(');
}
vector<string> generateParenthesis(int n)
{
if (n == 0)
return vector<string>{""};
string cur("(");
backtrack(n, 1, cur);
return res;
}
};
```
### C
```cpp
class Solution
{
public:
vector<string> generateParenthesis(int n)
{
vector<string> res;
dfs("", 0, n, &res);
return res;
}
void dfs(const string &path, int m, int n, vector<string> *res)
{
if (m == 0 && n == 0)
{
if (!path.empty())
res->push_back(path);
return;
}
if (n > 0)
dfs(path + '(', m + 1, n - 1, res);
if (m > 0)
dfs(path + ')', m - 1, n, res);
}
};
```
{
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"keywords": [
"leetcode",
"最长有效括号"
],
"children": [],
"export": [
"solution.json"
],
"title": "最长有效括号"
}
\ No newline at end of file
<p>给你一个只包含 <code>'('</code> 和 <code>')'</code> 的字符串,找出最长有效(格式正确且连续)括号子串的长度。</p><p> </p><div class="original__bRMd"><div><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>s = "(()"<strong><br />输出:</strong>2<strong><br />解释:</strong>最长有效括号子串是 "()"</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>s = ")()())"<strong><br />输出:</strong>4<strong><br />解释:</strong>最长有效括号子串是 "()()"</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>s = ""<strong><br />输出:</strong>0</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>0 <= s.length <= 3 * 10<sup>4</sup></code></li> <li><code>s[i]</code><code>'('</code><code>')'</code></li></ul></div></div>
\ No newline at end of file
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
int longestValidParentheses(string s)
{
stack<int> stk;
int invalid = -1;
int len = 0, max_len = 0;
for (int i = 0; i < s.length(); i++)
{
if (s[i] == '(')
{
stk.push(i);
}
else
{
if (stk.empty())
{
invalid = i;
}
else
{
stk.pop();
if (stk.empty())
{
max_len = max(i - invalid, max_len);
}
else
{
max_len = max(i - stk.top(), max_len);
}
}
}
}
return max_len;
}
};
\ No newline at end of file
{
"type": "code_options",
"author": "CSDN.net",
"source": "solution.md",
"exercise_id": "b5af0c58150048899e80a6abb3107452"
}
\ No newline at end of file
# 最长有效括号
<p>给你一个只包含 <code>'('</code> 和 <code>')'</code> 的字符串,找出最长有效(格式正确且连续)括号子串的长度。</p><p> </p><div class="original__bRMd"><div><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>s = "(()"<strong><br />输出:</strong>2<strong><br />解释:</strong>最长有效括号子串是 "()"</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>s = ")()())"<strong><br />输出:</strong>4<strong><br />解释:</strong>最长有效括号子串是 "()()"</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>s = ""<strong><br />输出:</strong>0</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>0 <= s.length <= 3 * 10<sup>4</sup></code></li> <li><code>s[i]</code><code>'('</code><code>')'</code></li></ul></div></div>
<p><p>给你一个只包含 <code>'('</code> 和 <code>')'</code> 的字符串,找出最长有效(格式正确且连续)括号子串的长度。</p><p> </p><div class="original__bRMd"><div><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>s = "(()"<strong><br />输出:</strong>2<strong><br />解释:</strong>最长有效括号子串是 "()"</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>s = ")()())"<strong><br />输出:</strong>4<strong><br />解释:</strong>最长有效括号子串是 "()()"</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>s = ""<strong><br />输出:</strong>0</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>0 <= s.length <= 3 * 10<sup>4</sup></code></li> <li><code>s[i]</code><code>'('</code><code>')'</code></li></ul></div></div>
<p>以下错误的选项是?</p></p>
## aop
### before
```cpp
```
### after
```cpp
```
## 答案
```cpp
class Solution
{
public:
int longestValidParentheses(string s)
{
int n = s.size();
if (n == 1)
return 0;
if (n == 2)
{
if (s[0] == '(' && s[1] == ')')
return 2;
else
return 0;
}
vector<int> dp(n, 0);
int max = 0;
for (int i = 1; i < n; i++)
{
if (s[i] == ')' && s[i - 1] == '(' && i >= 2)
dp[i] = dp[i - 2] + 2;
if (s[i] == ')' && s[i - 1] == '(' && i == 1)
dp[i] = 2;
if (s[i] == ')' && s[i - 1] == ')' && s[i - dp[i - 1] - 1] == '(')
dp[i] = dp[i - 1] + 2 + dp[dp[i - 1] - 2];
if (dp[i] > max)
max = dp[i];
}
return max;
}
};
```
## 选项
### A
```cpp
class Solution
{
public:
int longestValidParentheses(string s)
{
stack<int> stk;
int invalid = -1;
int len = 0, max_len = 0;
for (int i = 0; i < s.length(); i++)
{
if (s[i] == '(')
{
stk.push(i);
}
else
{
if (stk.empty())
{
invalid = i;
}
else
{
stk.pop();
if (stk.empty())
{
max_len = max(i - invalid, max_len);
}
else
{
max_len = max(i - stk.top(), max_len);
}
}
}
}
return max_len;
}
};
```
### B
```cpp
class Solution
{
public:
int longestValidParentheses(string s)
{
stack<int> left;
left.push(-1);
int size = 0, maxSize = 0;
for (int i = 0; i < s.size(); i++)
{
if (s[i] == '(')
left.push(i);
else
{
if (left.size() != 1)
{
left.pop();
size = i - left.top();
maxSize = max(maxSize, size);
}
else
{
left.pop();
left.push(i);
}
}
}
maxSize = max(maxSize, size);
return maxSize;
}
};
```
### C
```cpp
class Solution
{
public:
int longestValidParentheses(string s)
{
queue<pair<int, int>> q;
bool *valid = new bool[s.length() + 1];
int imax = -1;
memset(valid, 0, sizeof(bool) * (s.length() + 1));
for (int i = 0; i < int(s.length()) - 1; i++)
{
if (s[i] == '(' && s[i + 1] == ')')
{
valid[i] = true;
valid[i + 1] = true;
q.push(make_pair(i, i + 1));
}
}
while (!q.empty())
{
pair<int, int> parentheses;
parentheses = q.front();
q.pop();
int l = parentheses.first, r = parentheses.second;
while (l >= 0 && valid[l])
l--;
while (r < s.length() && valid[r])
r++;
if (!valid[l] && !valid[r])
{
if (s[l] == '(' && s[r] == ')')
{
valid[l] = true;
valid[r] = true;
q.push(make_pair(l, r));
}
else
{
l = l + 1;
r = r - 1;
}
}
if (r - l > imax)
imax = r - l;
}
return imax + 1;
}
};
```
{
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"keywords": [
"leetcode",
"通配符匹配"
],
"children": [],
"export": [
"solution.json"
],
"title": "通配符匹配"
}
\ No newline at end of file
<p>给定一个字符串&nbsp;(<code>s</code>) 和一个字符模式&nbsp;(<code>p</code>) ,实现一个支持&nbsp;<code>&#39;?&#39;</code>&nbsp;&nbsp;<code>&#39;*&#39;</code>&nbsp;的通配符匹配。</p><pre>&#39;?&#39; 可以匹配任何单个字符。&#39;*&#39; 可以匹配任意字符串(包括空字符串)。</pre><p>两个字符串<strong>完全匹配</strong>才算匹配成功。</p><p><strong>说明:</strong></p><ul> <li><code>s</code>&nbsp;可能为空,且只包含从&nbsp;<code>a-z</code>&nbsp;的小写字母。</li> <li><code>p</code>&nbsp;可能为空,且只包含从&nbsp;<code>a-z</code>&nbsp;的小写字母,以及字符&nbsp;<code>?</code>&nbsp;&nbsp;<code>*</code></li></ul><p><strong>示例&nbsp;1:</strong></p><pre><strong>输入:</strong>s = &quot;aa&quot;p = &quot;a&quot;<strong><br />输出:</strong> false<strong><br />解释:</strong> &quot;a&quot; 无法匹配 &quot;aa&quot; 整个字符串。</pre><p><strong>示例&nbsp;2:</strong></p><pre><strong>输入:</strong>s = &quot;aa&quot;p = &quot;*&quot;<strong><br />输出:</strong> true<strong><br />解释:</strong>&nbsp;&#39;*&#39; 可以匹配任意字符串。</pre><p><strong>示例&nbsp;3:</strong></p><pre><strong>输入:</strong>s = &quot;cb&quot;p = &quot;?a&quot;<strong><br />输出:</strong> false<strong><br />解释:</strong>&nbsp;&#39;?&#39; 可以匹配 &#39;c&#39;, 但第二个 &#39;a&#39; 无法匹配 &#39;b&#39;</pre><p><strong>示例&nbsp;4:</strong></p><pre><strong>输入:</strong>s = &quot;adceb&quot;p = &quot;*a*b&quot;<strong><br />输出:</strong> true<strong><br />解释:</strong>&nbsp;第一个 &#39;*&#39; 可以匹配空字符串, 第二个 &#39;*&#39; 可以匹配字符串 &quot;dce&quot;.</pre><p><strong>示例&nbsp;5:</strong></p><pre><strong>输入:</strong>s = &quot;acdcb&quot;p = &quot;a*c?b&quot;<strong><br />输出:</strong> false</pre>
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
static bool isMatch(char *s, char *p)
{
char *last_s = NULL;
char *last_p = NULL;
while (*s != '\0')
{
if (*p == '*')
{
if (*++p == '\0')
{
return true;
}
last_s = s;
last_p = p;
}
else if (*p == '?' || *s == *p)
{
s++;
p++;
}
else if (last_s != NULL)
{
p = last_p;
s = ++last_s;
}
else
{
return false;
}
}
while (*p == '*')
{
p++;
}
return *p == '\0';
}
int main(int argc, char **argv)
{
if (argc != 3)
{
fprintf(stderr, "Usage: ./test string pattern\n");
exit(-1);
}
printf("%s\n", isMatch(argv[1], argv[2]) ? "true" : "false");
return 0;
}
\ No newline at end of file
{
"type": "code_options",
"author": "CSDN.net",
"source": "solution.md",
"exercise_id": "d39c84ccfb5945afac794f75005270f3"
}
\ No newline at end of file
# 通配符匹配
<p>给定一个字符串&nbsp;(<code>s</code>) 和一个字符模式&nbsp;(<code>p</code>) ,实现一个支持&nbsp;<code>&#39;?&#39;</code>&nbsp;&nbsp;<code>&#39;*&#39;</code>&nbsp;的通配符匹配。</p><pre>&#39;?&#39; 可以匹配任何单个字符。&#39;*&#39; 可以匹配任意字符串(包括空字符串)。</pre><p>两个字符串<strong>完全匹配</strong>才算匹配成功。</p><p><strong>说明:</strong></p><ul> <li><code>s</code>&nbsp;可能为空,且只包含从&nbsp;<code>a-z</code>&nbsp;的小写字母。</li> <li><code>p</code>&nbsp;可能为空,且只包含从&nbsp;<code>a-z</code>&nbsp;的小写字母,以及字符&nbsp;<code>?</code>&nbsp;&nbsp;<code>*</code></li></ul><p><strong>示例&nbsp;1:</strong></p><pre><strong>输入:</strong>s = &quot;aa&quot;p = &quot;a&quot;<strong><br />输出:</strong> false<strong><br />解释:</strong> &quot;a&quot; 无法匹配 &quot;aa&quot; 整个字符串。</pre><p><strong>示例&nbsp;2:</strong></p><pre><strong>输入:</strong>s = &quot;aa&quot;p = &quot;*&quot;<strong><br />输出:</strong> true<strong><br />解释:</strong>&nbsp;&#39;*&#39; 可以匹配任意字符串。</pre><p><strong>示例&nbsp;3:</strong></p><pre><strong>输入:</strong>s = &quot;cb&quot;p = &quot;?a&quot;<strong><br />输出:</strong> false<strong><br />解释:</strong>&nbsp;&#39;?&#39; 可以匹配 &#39;c&#39;, 但第二个 &#39;a&#39; 无法匹配 &#39;b&#39;</pre><p><strong>示例&nbsp;4:</strong></p><pre><strong>输入:</strong>s = &quot;adceb&quot;p = &quot;*a*b&quot;<strong><br />输出:</strong> true<strong><br />解释:</strong>&nbsp;第一个 &#39;*&#39; 可以匹配空字符串, 第二个 &#39;*&#39; 可以匹配字符串 &quot;dce&quot;.</pre><p><strong>示例&nbsp;5:</strong></p><pre><strong>输入:</strong>s = &quot;acdcb&quot;p = &quot;a*c?b&quot;<strong><br />输出:</strong> false</pre>
<p><p>给定一个字符串&nbsp;(<code>s</code>) 和一个字符模式&nbsp;(<code>p</code>) ,实现一个支持&nbsp;<code>&#39;?&#39;</code>&nbsp;&nbsp;<code>&#39;*&#39;</code>&nbsp;的通配符匹配。</p><pre>&#39;?&#39; 可以匹配任何单个字符。&#39;*&#39; 可以匹配任意字符串(包括空字符串)。</pre><p>两个字符串<strong>完全匹配</strong>才算匹配成功。</p><p><strong>说明:</strong></p><ul> <li><code>s</code>&nbsp;可能为空,且只包含从&nbsp;<code>a-z</code>&nbsp;的小写字母。</li> <li><code>p</code>&nbsp;可能为空,且只包含从&nbsp;<code>a-z</code>&nbsp;的小写字母,以及字符&nbsp;<code>?</code>&nbsp;&nbsp;<code>*</code></li></ul><p><strong>示例&nbsp;1:</strong></p><pre><strong>输入:</strong>s = &quot;aa&quot;p = &quot;a&quot;<strong><br />输出:</strong> false<strong><br />解释:</strong> &quot;a&quot; 无法匹配 &quot;aa&quot; 整个字符串。</pre><p><strong>示例&nbsp;2:</strong></p><pre><strong>输入:</strong>s = &quot;aa&quot;p = &quot;*&quot;<strong><br />输出:</strong> true<strong><br />解释:</strong>&nbsp;&#39;*&#39; 可以匹配任意字符串。</pre><p><strong>示例&nbsp;3:</strong></p><pre><strong>输入:</strong>s = &quot;cb&quot;p = &quot;?a&quot;<strong><br />输出:</strong> false<strong><br />解释:</strong>&nbsp;&#39;?&#39; 可以匹配 &#39;c&#39;, 但第二个 &#39;a&#39; 无法匹配 &#39;b&#39;</pre><p><strong>示例&nbsp;4:</strong></p><pre><strong>输入:</strong>s = &quot;adceb&quot;p = &quot;*a*b&quot;<strong><br />输出:</strong> true<strong><br />解释:</strong>&nbsp;第一个 &#39;*&#39; 可以匹配空字符串, 第二个 &#39;*&#39; 可以匹配字符串 &quot;dce&quot;.</pre><p><strong>示例&nbsp;5:</strong></p><pre><strong>输入:</strong>s = &quot;acdcb&quot;p = &quot;a*c?b&quot;<strong><br />输出:</strong> false</pre>
<p>以下错误的选项是?</p></p>
## aop
### before
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
```cpp
int main()
{
Solution sol;
string s = "adceb", p = "*a*b";
bool res;
res = sol.isMatch(s, p);
cout << res;
return 0;
}
```
## 答案
```cpp
class Solution
{
public:
bool isMatch(string s, string p)
{
if (s == "" && p == "" || (s == "" && p == "*"))
return true;
if (s == p)
return true;
int lens = s.length();
int lenp = p.length();
bool questionm = false, starm = false;
for (int k = 0; k < lenp; k++)
{
if (p[k] == '?')
questionm = true;
if (p[k] == '*')
starm = true;
}
if (lenp != lens && questionm == false && starm == false)
return false;
int i = 0, j = 0;
int mstar, sstar = -1;
while (i < lens)
{
if (j < lenp && p[j] == '*')
{
mstar = i;
sstar = j;
j += 1;
}
else if (sstar != -1)
{
mstar += 1;
j = sstar + 1;
i = mstar;
}
else
return false;
}
while (j < lenp)
{
if (p[j] != '*')
return false;
j++;
}
return true;
}
};
```
## 选项
### A
```cpp
class Solution
{
public:
bool isMatch(string s, string p)
{
int m = s.size();
int n = p.size();
vector<vector<int>> dp(m + 1, vector<int>(n + 1));
dp[0][0] = true;
for (int i = 1; i <= n; i++)
{
if (p[i - 1] == '*')
dp[0][i] = true;
else
break;
}
for (int i = 1; i <= m; i++)
{
for (int j = 1; j <= n; j++)
{
if (p[j - 1] == '*')
{
dp[i][j] |= dp[i][j - 1];
dp[i][j] |= dp[i - 1][j];
}
else
{
if (p[j - 1] == '?' || s[i - 1] == p[j - 1])
{
dp[i][j] |= dp[i - 1][j - 1];
}
}
}
}
return dp[m][n];
}
};
```
### B
```cpp
class Solution
{
public:
bool isMatch(string s, string p)
{
vector<vector<bool>> dp(s.size() + 1, vector<bool>(p.size() + 1));
dp[0][0] = 1;
for (int j = 1; j <= p.size(); j++)
{
dp[0][j] = dp[0][j - 1] && p[j - 1] == '*';
}
for (int i = 1; i <= s.size(); i++)
{
for (int j = 1; j <= p.size(); j++)
{
if (p[j - 1] == '*')
{
dp[i][j] = dp[i][j - 1] || dp[i - 1][j];
}
else
{
dp[i][j] = (s[i - 1] == p[j - 1] || p[j - 1] == '?') && dp[i - 1][j - 1];
}
}
}
return dp[s.size()][p.size()];
}
};
```
### C
```cpp
class Solution
{
public:
bool isMatch(string s, string p)
{
if (p.empty())
return s.empty();
if (s.empty())
{
if (p[0] == '*')
return isMatch(s, p.substr(1));
else
return false;
}
if (p[0] == '*')
return isMatch(s, p.substr(1)) || isMatch(s.substr(1), p);
else
return (s[0] == p[0] || p[0] == '?') && isMatch(s.substr(1), p.substr(1));
}
};
```
{
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"keywords": [
"leetcode",
"最长回文子串"
],
"children": [],
"export": [
"solution.json"
],
"title": "最长回文子串"
}
\ No newline at end of file
<p>给你一个字符串 <code>s</code>,找到 <code>s</code> 中最长的回文子串。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>s = "babad"<strong><br />输出:</strong>"bab"<strong><br />解释:</strong>"aba" 同样是符合题意的答案。</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>s = "cbbd"<strong><br />输出:</strong>"bb"</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>s = "a"<strong><br />输出:</strong>"a"</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>s = "ac"<strong><br />输出:</strong>"a"</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>1 <= s.length <= 1000</code></li> <li><code>s</code> 仅由数字和英文字母(大写和/或小写)组成</li></ul>
\ No newline at end of file
class Solution
{
public:
string longestPalindrome(string s)
{
int ti = 0, maxlen = 0, i, t;
for (i = 0; s[i]; i++)
{
t = 1;
while (t <= i && s[i + t])
{
if (s[i + t] == s[i - t])
t++;
else
break;
}
t--;
if (2 * t + 1 > maxlen)
{
ti = i - t;
maxlen = 2 * t + 1;
}
}
for (i = 0; s[i]; i++)
{
t = 1;
while (t <= i + 1 && s[i + t])
{
if (s[i - t + 1] == s[i + t])
t++;
else
break;
}
t--;
if (2 * t > maxlen)
{
ti = i - t + 1;
maxlen = 2 * t;
}
}
s[ti + maxlen] = 0;
return s.c_str() + ti;
}
};
\ No newline at end of file
{
"type": "code_options",
"author": "CSDN.net",
"source": "solution.md",
"exercise_id": "afafdeda6ef045029a9ac8f1e18bc27d"
}
\ No newline at end of file
# 最长回文子串
<p>给你一个字符串 <code>s</code>,找到 <code>s</code> 中最长的回文子串。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>s = "babad"<strong><br />输出:</strong>"bab"<strong><br />解释:</strong>"aba" 同样是符合题意的答案。</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>s = "cbbd"<strong><br />输出:</strong>"bb"</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>s = "a"<strong><br />输出:</strong>"a"</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>s = "ac"<strong><br />输出:</strong>"a"</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>1 <= s.length <= 1000</code></li> <li><code>s</code> 仅由数字和英文字母(大写和/或小写)组成</li></ul>
<p><p>给你一个字符串 <code>s</code>,找到 <code>s</code> 中最长的回文子串。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>s = "babad"<strong><br />输出:</strong>"bab"<strong><br />解释:</strong>"aba" 同样是符合题意的答案。</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>s = "cbbd"<strong><br />输出:</strong>"bb"</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>s = "a"<strong><br />输出:</strong>"a"</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>s = "ac"<strong><br />输出:</strong>"a"</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>1 <= s.length <= 1000</code></li> <li><code>s</code> 仅由数字和英文字母(大写和/或小写)组成</li></ul>
<p>以下错误的选项是?</p></p>
## aop
### before
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
```cpp
int main()
{
Solution test;
string ret;
string s = "cbbd";
ret = test.longestPalindrome(s);
cout << ret << endl;
return 0;
}
```
## 答案
```cpp
class Solution
{
public:
string longestPalindrome(string s)
{
int len = s.size();
if (len == 0 || len == 1)
return s;
int start = 0;
int end = 0;
int mlen = 0;
for (int i = 0; i < len; i++)
{
int len1 = expendaroundcenter(s, i, i);
int len2 = expendaroundcenter(s, i, i + 1);
mlen = max(min(len1, len2), mlen);
if (mlen > end - start + 1)
{
start = i - (mlen - 1) / 2;
end = i + mlen / 2;
}
}
return s.substr(start, mlen);
}
private:
int expendaroundcenter(string s, int left, int right)
{
int L = left;
int R = right;
while (L >= 0 && R < s.length() && s[R] == s[L])
{
L--;
R++;
}
return R - L - 1;
}
};
```
## 选项
### A
```cpp
class Solution
{
public:
string longestPalindrome(string s)
{
int len = s.size();
if (len == 0 || len == 1)
return s;
int start = 0;
int max = 1;
vector<vector<int>> dp(len, vector<int>(len));
for (int i = 0; i < len; i++)
{
dp[i][i] = 1;
if (i < len - 1 && s[i] == s[i + 1])
{
dp[i][i + 1] = 1;
max = 2;
start = i;
}
}
for (int l = 3; l <= len; l++)
{
for (int i = 0; i + l - 1 < len; i++)
{
int j = l + i - 1;
if (s[i] == s[j] && dp[i + 1][j - 1] == 1)
{
dp[i][j] = 1;
start = i;
max = l;
}
}
}
return s.substr(start, max);
}
};
```
### B
```cpp
class Solution
{
public:
string longestPalindrome(string s)
{
if (s.length() == 1)
return s;
string rev = s;
string res;
std::reverse(rev.begin(), rev.end());
if (rev == s)
return s;
int len = 0;
for (int i = 0; i < s.length(); i++)
{
string temp;
for (int j = i; j < s.length(); j++)
{
temp = temp + s[j];
if (len >= temp.length())
continue;
else if (rev.find(temp) != -1)
{
string q = temp;
std::reverse(q.begin(), q.end());
if (q == temp)
{
len = temp.length();
res = temp;
}
}
else
break;
}
temp = "";
}
return res;
}
};
```
### C
```cpp
class Solution
{
public:
string longestPalindrome(string s)
{
string res = "";
string temp = "";
for (int i = 0; i < s.length(); i++)
{
for (int j = i; j < s.length(); j++)
{
temp = temp + s[j];
string tem = temp;
std::reverse(tem.begin(), tem.end());
if (temp == tem)
res = res.length() > temp.length() ? res : temp;
}
temp = "";
}
return res;
}
};
```
{
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"keywords": [
"leetcode",
"最大子序和"
],
"children": [],
"export": [
"solution.json"
],
"title": "最大子序和"
}
\ No newline at end of file
<p>给定一个整数数组 <code>nums</code> ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>nums = [-2,1,-3,4,-1,2,1,-5,4]<strong><br />输出:</strong>6<strong><br />解释:</strong>连续子数组 [4,-1,2,1] 的和最大,为 6 。</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>nums = [1]<strong><br />输出:</strong>1</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><strong>示例 5:</strong></p><pre><strong>输入:</strong>nums = [-100000]<strong><br />输出:</strong>-100000</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>1 <= nums.length <= 3 * 10<sup>4</sup></code></li> <li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li></ul><p> </p><p><strong>进阶:</strong>如果你已经实现复杂度为 <code>O(n)</code> 的解法,尝试使用更为精妙的 <strong>分治法</strong> 求解。</p>
\ No newline at end of file
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
int maxSubArray(vector<int> &nums)
{
int sum = 0, max_sum = INT_MIN;
for (int i = 0; i < nums.size(); i++)
{
if (sum < 0)
{
sum = nums[i];
}
else
{
sum += nums[i];
}
max_sum = max(sum, max_sum);
}
return max_sum;
}
};
\ No newline at end of file
{
"type": "code_options",
"author": "CSDN.net",
"source": "solution.md",
"exercise_id": "8d5d707a36a24e58a6314baa2a2be240"
}
\ No newline at end of file
# 最大子序和
<p>给定一个整数数组 <code>nums</code> ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>nums = [-2,1,-3,4,-1,2,1,-5,4]<strong><br />输出:</strong>6<strong><br />解释:</strong>连续子数组 [4,-1,2,1] 的和最大,为 6 。</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>nums = [1]<strong><br />输出:</strong>1</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><strong>示例 5:</strong></p><pre><strong>输入:</strong>nums = [-100000]<strong><br />输出:</strong>-100000</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>1 <= nums.length <= 3 * 10<sup>4</sup></code></li> <li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li></ul><p> </p><p><strong>进阶:</strong>如果你已经实现复杂度为 <code>O(n)</code> 的解法,尝试使用更为精妙的 <strong>分治法</strong> 求解。</p>
<p>以下错误的选项是?</p>
## aop
### before
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
```cpp
int main()
{
Solution sol;
vector<int> nums = {-2, 1, -3, 4, -1, 2, 1, -5, 4};
int res;
res = sol.maxSubArray(nums);
cout << res;
return 0;
}
```
## 答案
```cpp
class Solution
{
public:
int maxSubArray(vector<int> &nums)
{
if (nums.empty())
return 0;
int pre = 0, res = nums[0];
for (auto c : nums)
{
pre = max(pre, c);
res = max(pre, res);
}
return res;
}
};
```
## 选项
### A
```cpp
class Solution
{
public:
int maxSubArray(vector<int> &nums)
{
int sum = 0, max_sum = INT_MIN;
for (int i = 0; i < nums.size(); i++)
{
if (sum < 0)
{
sum = nums[i];
}
else
{
sum += nums[i];
}
max_sum = max(sum, max_sum);
}
return max_sum;
}
};
```
### B
```cpp
class Solution
{
public:
int maxSubArray(vector<int> &nums)
{
if (nums.size() == 0)
return NULL;
return fenzhifa(nums, 0, nums.size() - 1);
}
int fenzhifa(vector<int> &nums, int left, int right)
{
if (left > right)
return INT_MIN;
if (left == right)
return nums[left];
int mid = (left + right) / 2;
int l = fenzhifa(nums, 0, mid - 1);
int r = fenzhifa(nums, mid + 1, right);
int t = nums[mid];
int max_num = nums[mid];
for (int i = mid - 1; i >= left; i--)
{
t += nums[i];
max_num = max(max_num, t);
}
t = max_num;
for (int i = mid + 1; i <= right; i++)
{
t += nums[i];
max_num = max(max_num, t);
}
return max(max(r, l), max_num);
}
};
```
### C
```cpp
class Solution
{
public:
int maxSubArray(vector<int> &nums)
{
int maxsum = nums[0];
for (int i = 0; i < nums.size(); i++)
{
int sum = 0;
for (int j = i; j < nums.size(); j++)
{
sum = sum + nums[j];
if (maxsum < sum)
maxsum = sum;
}
}
return maxsum;
}
};
```
{
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"keywords": [
"leetcode",
"不同路径"
],
"children": [],
"export": [
"solution.json"
],
"title": "不同路径"
}
\ No newline at end of file
<p>一个机器人位于一个 <code>m x n</code><em> </em>网格的左上角 (起始点在下图中标记为 “Start” )。</p>
<p>机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为 “Finish” )。</p>
<p>问总共有多少条不同的路径?</p>
<p> </p>
<p><strong>示例 1:</strong></p><img
src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0062.Unique%20Paths/images/robot_maze.png" />
<pre><strong>输入:</strong>m = 3, n = 7<strong><br />输出:</strong>28</pre>
<p><strong>示例 2:</strong></p>
<pre><strong>输入:</strong>m = 3, n = 2<strong><br />输出:</strong>3<strong><br />解释:</strong>从左上角开始,总共有 3 条路径可以到达右下角。<br />1. 向右 -> 向下 -> 向下<br />2. 向下 -> 向下 -> 向右<br />3. 向下 -> 向右 -> 向下</pre>
<p><strong>示例 3:</strong></p>
<pre><strong>输入:</strong>m = 7, n = 3<strong><br />输出:</strong>28</pre>
<p><strong>示例 4:</strong></p>
<pre><strong>输入:</strong>m = 3, n = 3<strong><br />输出:</strong>6</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= m, n <= 100</code></li>
<li>题目数据保证答案小于等于 <code>2 * 10<sup>9</sup></code></li>
</ul>
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
static int uniquePaths(int m, int n)
{
int row, col;
int *grids = malloc(m * n * sizeof(int));
for (col = 0; col < m; col++)
{
grids[col] = 1;
}
for (row = 0; row < n; row++)
{
grids[row * m] = 1;
}
for (row = 1; row < n; row++)
{
for (col = 1; col < m; col++)
{
grids[row * m + col] = grids[row * m + col - 1] + grids[(row - 1) * m + col];
}
}
return grids[m * n - 1];
}
int main(int argc, char **argv)
{
if (argc != 3)
{
fprintf(stderr, "Usage: ./test m n\n");
exit(-1);
}
printf("%d\n", uniquePaths(atoi(argv[1]), atoi(argv[2])));
return 0;
}
\ No newline at end of file
{
"type": "code_options",
"author": "CSDN.net",
"source": "solution.md",
"exercise_id": "bc3eed79091d41219ac4bc3407bb8e78"
}
\ No newline at end of file
# 不同路径
<p>一个机器人位于一个 <code>m x n</code><em> </em>网格的左上角 (起始点在下图中标记为 “Start” )。</p>
<p>机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为 “Finish” )。</p>
<p>问总共有多少条不同的路径?</p>
<p> </p>
<p><strong>示例 1:</strong></p><img
src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0062.Unique%20Paths/images/robot_maze.png" />
<pre><strong>输入:</strong>m = 3, n = 7<strong><br />输出:</strong>28</pre>
<p><strong>示例 2:</strong></p>
<pre><strong>输入:</strong>m = 3, n = 2<strong><br />输出:</strong>3<strong><br />解释:</strong>从左上角开始,总共有 3 条路径可以到达右下角。<br />1. 向右 -> 向下 -> 向下<br />2. 向下 -> 向下 -> 向右<br />3. 向下 -> 向右 -> 向下</pre>
<p><strong>示例 3:</strong></p>
<pre><strong>输入:</strong>m = 7, n = 3<strong><br />输出:</strong>28</pre>
<p><strong>示例 4:</strong></p>
<pre><strong>输入:</strong>m = 3, n = 3<strong><br />输出:</strong>6</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= m, n <= 100</code></li>
<li>题目数据保证答案小于等于 <code>2 * 10<sup>9</sup></code></li>
</ul>
<p>以下错误的选项是?</p>
## aop
### before
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
```cpp
int main()
{
Solution sol;
int m = 3;
int n = 7;
int res;
res = sol.uniquePaths(m, n);
cout << res;
return 0;
}
```
## 答案
```cpp
class Solution
{
public:
int uniquePaths(int m, int n)
{
vector<vector<int>> path(m, vector<int>(n, 0));
for (int i = 0; i < n; i++)
path[0][i] = 1;
for (int i = 0; i < m; i++)
path[i][0] = 1;
for (int i = 1; i < n; i++)
for (int j = 1; j < m; j++)
path[j][i] = path[j - 1][i + 1] + path[j + 1][i - 1];
return path[m - 1][n - 1];
}
};
```
## 选项
### A
```cpp
typedef vector<int> BigInt;
class Solution
{
public:
int uniquePaths(int m, int n)
{
if (m == 0 || n == 0)
return 0;
if (m == 1 || n == 1)
return 1;
int m_ = m - 1 + n - 1;
int n_ = n - 1;
BigInt a = fac(m_);
int result = 0;
for (int i = n_; i >= 1; i--)
a = div(a, i);
for (int i = m_ - n_; i >= 1; i--)
a = div(a, i);
int k = a.size() - 1;
while (a[k] == 0)
k--;
for (int i = k; i >= 0; i--)
result = result * 10 + a[i];
return result;
}
BigInt fac(int n)
{
BigInt result;
result.push_back(1);
for (int factor = 1; factor <= n; ++factor)
{
long long carry = 0;
for (auto &item : result)
{
long long product = item * factor + carry;
item = product % 10;
carry = product / 10;
}
if (carry > 0)
{
while (carry > 0)
{
result.push_back(carry % 10);
carry /= 10;
}
}
}
return result;
}
BigInt div(BigInt a, int d)
{
int b = 0;
BigInt result;
int len = a.size();
for (int i = len - 1; i >= 0; i--)
{
b = b * 10 + a[i];
result.insert(result.begin(), b / d);
b = b % d;
}
return result;
}
};
```
### B
```cpp
class Solution
{
public:
int uniquePaths(int m, int n)
{
if (m <= 0 || n <= 0)
{
return 0;
}
vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0));
for (int i = 0; i < m; i++)
{
dp[i][0] = 1;
}
for (int i = 0; i < n; i++)
{
dp[0][i] = 1;
}
for (int i = 1; i < m; i++)
{
for (int j = 1; j < n; j++)
{
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
}
}
return dp[m - 1][n - 1];
}
};
```
### C
```cpp
class Solution
{
public:
int uniquePaths(int m, int n)
{
int N = m + n - 2;
int M = m < n ? m - 1 : n - 1;
long ans = 1;
for (int i = 1; i <= M; i++)
ans = ans * (N - i + 1) / i;
return ans;
}
};
```
{
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"keywords": [
"leetcode",
"不同路径 II"
],
"children": [],
"export": [
"solution.json"
],
"title": "不同路径 II"
}
\ No newline at end of file
<p>一个机器人位于一个 <em>m x n </em>网格的左上角 (起始点在下图中标记为“Start” )。</p>
<p>机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为“Finish”)。</p>
<p>现在考虑网格中有障碍物。那么从左上角到右下角将会有多少条不同的路径?</p>
<p><img src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0063.Unique%20Paths%20II/images/robot_maze.png"
style="height: 183px; width: 400px;" /></p>
<p>网格中的障碍物和空位置分别用 <code>1</code><code>0</code> 来表示。</p>
<p> </p>
<p><strong>示例 1:</strong></p><img alt=""
src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0063.Unique%20Paths%20II/images/robot1.jpg"
style="width: 242px; height: 242px;" />
<pre><strong>输入:</strong>obstacleGrid = [[0,0,0],[0,1,0],[0,0,0]]<strong><br />输出:</strong>2<strong><br />解释:</strong>3x3 网格的正中间有一个障碍物。从左上角到右下角一共有 2 条不同的路径:<br />1. 向右 -> 向右 -> 向下 -> 向下<br />2. 向下 -> 向下 -> 向右 -> 向右</pre>
<p><strong>示例 2:</strong></p><img alt=""
src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0063.Unique%20Paths%20II/images/robot2.jpg"
style="width: 162px; height: 162px;" />
<pre><strong>输入:</strong>obstacleGrid = [[0,1],[0,0]]<strong><br />输出:</strong>1</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>m == obstacleGrid.length</code></li>
<li><code>n == obstacleGrid[i].length</code></li>
<li><code>1 <= m, n <= 100</code></li>
<li><code>obstacleGrid[i][j]</code><code>0</code><code>1</code></li>
</ul>
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
static int uniquePathsWithObstacles(int **obstacleGrid, int obstacleGridRowSize, int obstacleGridColSize)
{
int row, col;
int reset = 0;
for (row = 0; row < obstacleGridRowSize; row++)
{
if (reset)
{
obstacleGrid[row][0] = 1;
}
else
{
if (obstacleGrid[row][0] == 1)
{
reset = 1;
}
}
}
reset = 0;
for (col = 0; col < obstacleGridColSize; col++)
{
if (reset)
{
obstacleGrid[0][col] = 1;
}
else
{
if (obstacleGrid[0][col] == 1)
{
reset = 1;
}
}
}
for (row = 0; row < obstacleGridRowSize; row++)
{
int *line = obstacleGrid[row];
for (col = 0; col < obstacleGridColSize; col++)
{
line[col] ^= 1;
}
}
for (row = 1; row < obstacleGridRowSize; row++)
{
int *last_line = obstacleGrid[row - 1];
int *line = obstacleGrid[row];
for (col = 1; col < obstacleGridColSize; col++)
{
if (line[col] != 0)
{
line[col] = line[col - 1] + last_line[col];
}
}
}
return obstacleGrid[obstacleGridRowSize - 1][obstacleGridColSize - 1];
}
int main(int argc, char **argv)
{
if (argc < 3)
{
fprintf(stderr, "Usage: ./test m n\n");
exit(-1);
}
int i, j, k = 3;
int row_size = atoi(argv[1]);
int col_size = atoi(argv[2]);
int **grids = malloc(row_size * sizeof(int *));
for (i = 0; i < row_size; i++)
{
grids[i] = malloc(col_size * sizeof(int));
int *line = grids[i];
for (j = 0; j < col_size; j++)
{
line[j] = atoi(argv[k++]);
printf("%d ", line[j]);
}
printf("\n");
}
printf("%d\n", uniquePathsWithObstacles(grids, row_size, col_size));
return 0;
}
\ No newline at end of file
{
"type": "code_options",
"author": "CSDN.net",
"source": "solution.md",
"exercise_id": "0908211c0523484193816c8ee5b39588"
}
\ 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>p</code>,请你来实现一个支持 <code>'.'</code> 和 <code>'*'</code> 的正则表达式匹配。</p><ul> <li><code>'.'</code> 匹配任意单个字符</li> <li><code>'*'</code> 匹配零个或多个前面的那一个元素</li></ul><p>所谓匹配,是要涵盖 <strong>整个 </strong>字符串 <code>s</code>的,而不是部分字符串。</p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>s = "aa" p = "a"<strong><br />输出:</strong>false<strong><br />解释:</strong>"a" 无法匹配 "aa" 整个字符串。</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>s = "aa" p = "a*"<strong><br />输出:</strong>true<strong><br />解释:</strong>因为 '*' 代表可以匹配零个或多个前面的那一个元素, 在这里前面的元素就是 'a'。因此,字符串 "aa" 可被视为 'a' 重复了一次。</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>s = "ab" p = ".*"<strong><br />输出:</strong>true<strong><br />解释:</strong>".*" 表示可匹配零个或多个('*')任意字符('.')。</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>s = "aab" p = "c*a*b"<strong><br />输出:</strong>true<strong><br />解释:</strong>因为 '*' 表示零个或多个,这里 'c' 为 0 个, 'a' 被重复一次。因此可以匹配字符串 "aab"。</pre><p><strong>示例 5:</strong></p><pre><strong>输入:</strong>s = "mississippi" p = "mis*is*p*."<strong><br />输出:</strong>false</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>0 <= s.length <= 20</code></li> <li><code>0 <= p.length <= 30</code></li> <li><code>s</code> 可能为空,且只包含从 <code>a-z</code> 的小写字母。</li> <li><code>p</code> 可能为空,且只包含从 <code>a-z</code> 的小写字母,以及字符 <code>.</code> 和 <code>*</code></li> <li>保证每次出现字符 <code>*</code> 时,前面都匹配到有效的字符</li></ul>
\ No newline at end of file
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册