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

add exercises

上级 0aeb20a8
<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>
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;
}
};
\ No newline at end of file
<p>给你 <code>n</code> 个非负整数 <code>a<sub>1</sub>,a<sub>2,</sub>...,a</code><sub><code>n</code></sub>每个数代表坐标中的一个点 <code>(i, a<sub>i</sub>)</code> 。在坐标内画 <code>n</code> 条垂直线,垂直线 <code>i</code> 的两个端点分别为 <code>(i, a<sub>i</sub>)</code><code>(i, 0)</code> 。找出其中的两条线,使得它们与 <code>x</code> 轴共同构成的容器可以容纳最多的水。</p><p><strong>说明:</strong>你不能倾斜容器。</p><p> </p><p><strong>示例 1:</strong></p><p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0011.Container%20With%20Most%20Water/images/question_11.jpg" style="height: 287px; width: 600px;" /></p><pre><strong>输入:</strong>[1,8,6,2,5,4,8,3,7]<strong><br />输出:</strong>49 <strong><br />解释:</strong>图中垂直线代表输入数组 [1,8,6,2,5,4,8,3,7]。在此情况下,容器能够容纳水(表示为蓝色部分)的最大值为 49。</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>height = [1,1]<strong><br />输出:</strong>1</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>height = [4,3,2,1,4]<strong><br />输出:</strong>16</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>height = [1,2,1]<strong><br />输出:</strong>2</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>n = height.length</code></li> <li><code>2 <= n <= 3 * 10<sup>4</sup></code></li> <li><code>0 <= height[i] <= 3 * 10<sup>4</sup></code></li></ul>
\ No newline at end of file
#define MAX(a, b) (((a) < (b)) ? (b) : (a))
#define MIN(a, b) (((a) > (b)) ? (b) : (a))
int maxArea(int *height, int heightSize)
{
int max = 0;
int i = 0, j = heightSize - 1;
int a;
while (i < j)
{
a = MIN(height[i], height[j]) * (j - i);
max = MAX(max, a);
if (height[i] > height[j])
--j;
else
++i;
}
return max;
}
\ No newline at end of file
<div class="notranslate">
<p>罗马数字包含以下七种字符:&nbsp;<code>I</code>&nbsp;<code>V</code>&nbsp;<code>X</code>&nbsp;<code>L</code><code>C</code><code>D</code>&nbsp;&nbsp;<code>M</code>
</p>
<pre><strong>字符</strong> <strong>数值</strong>
I 1
V 5
X 10
L 50
C 100
D 500
M 1000</pre>
<p>例如, 罗马数字 2 写做&nbsp;<code>II</code>&nbsp;,即为两个并列的 1。12
写做&nbsp;<code>XII</code>&nbsp;,即为&nbsp;<code>X</code>&nbsp;+&nbsp;<code>II</code>&nbsp;。 27
写做&nbsp;&nbsp;<code>XXVII</code>,
即为&nbsp;<code>XX</code>&nbsp;+&nbsp;<code>V</code>&nbsp;+&nbsp;<code>II</code>&nbsp;</p>
<p>通常情况下,罗马数字中小的数字在大的数字的右边。但也存在特例,例如 4 不写做&nbsp;<code>IIII</code>,而是&nbsp;<code>IV</code>。数字 1 在数字 5 的左边,所表示的数等于大数 5
减小数 1 得到的数值 4 。同样地,数字 9 表示为&nbsp;<code>IX</code>。这个特殊的规则只适用于以下六种情况:</p>
<ul>
<li><code>I</code>&nbsp;可以放在&nbsp;<code>V</code>&nbsp;(5) 和&nbsp;<code>X</code>&nbsp;(10) 的左边,来表示 4 和 9。</li>
<li><code>X</code>&nbsp;可以放在&nbsp;<code>L</code>&nbsp;(50) 和&nbsp;<code>C</code>&nbsp;(100) 的左边,来表示 40
&nbsp;90。&nbsp;</li>
<li><code>C</code>&nbsp;可以放在&nbsp;<code>D</code>&nbsp;(500) 和&nbsp;<code>M</code>&nbsp;(1000) 的左边,来表示&nbsp;400
&nbsp;900。</li>
</ul>
<p>给你一个整数,将其转为罗马数字。</p>
<p>&nbsp;</p>
<p><strong>示例&nbsp;1:</strong></p>
<pre><strong>输入:</strong>&nbsp;num = 3
<strong><br />输出:</strong> "III"</pre>
<p><strong>示例&nbsp;2:</strong></p>
<pre><strong>输入:</strong>&nbsp;num = 4
<strong><br />输出:</strong> "IV"</pre>
<p><strong>示例&nbsp;3:</strong></p>
<pre><strong>输入:</strong>&nbsp;num = 9
<strong><br />输出:</strong> "IX"</pre>
<p><strong>示例&nbsp;4:</strong></p>
<pre><strong>输入:</strong>&nbsp;num = 58
<strong><br />输出:</strong> "LVIII"
<strong><br />解释:</strong> L = 50, V = 5, III = 3.
</pre>
<p><strong>示例&nbsp;5:</strong></p>
<pre><strong>输入:</strong>&nbsp;num = 1994
<strong><br />输出:</strong> "MCMXCIV"
<strong><br />解释:</strong> M = 1000, CM = 900, XC = 90, IV = 4.</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= num &lt;= 3999</code></li>
</ul>
</div>
\ No newline at end of file
struct rmap
{
char *r;
int v;
} units[] = {
{"M", 1000},
{"CM", 900},
{"D", 500},
{"CD", 400},
{"C", 100},
{"XC", 90},
{"L", 50},
{"XL", 40},
{"X", 10},
{"IX", 9},
{"V", 5},
{"IV", 4},
{"I", 1}};
#include <string.h>
char result[64];
char *intToRoman(int num)
{
result[0] = 0;
int ri = 0;
int i = 0;
while (num)
{
if (num >= units[i].v)
{
strcat(result, units[i].r);
num -= units[i].v;
}
else
{
i++;
}
}
return result;
}
\ No newline at end of file
<div class="notranslate">
<p>罗马数字包含以下七种字符:&nbsp;<code>I</code>&nbsp;<code>V</code>&nbsp;<code>X</code>&nbsp;<code>L</code><code>C</code><code>D</code>&nbsp;&nbsp;<code>M</code>
</p>
<pre><strong>字符</strong> <strong>数值</strong>
I 1
V 5
X 10
L 50
C 100
D 500
M 1000</pre>
<p>例如, 罗马数字 2 写做&nbsp;<code>II</code>&nbsp;,即为两个并列的 1。12
写做&nbsp;<code>XII</code>&nbsp;,即为&nbsp;<code>X</code>&nbsp;+&nbsp;<code>II</code>&nbsp;。 27
写做&nbsp;&nbsp;<code>XXVII</code>,
即为&nbsp;<code>XX</code>&nbsp;+&nbsp;<code>V</code>&nbsp;+&nbsp;<code>II</code>&nbsp;</p>
<p>通常情况下,罗马数字中小的数字在大的数字的右边。但也存在特例,例如 4 不写做&nbsp;<code>IIII</code>,而是&nbsp;<code>IV</code>。数字 1 在数字 5 的左边,所表示的数等于大数 5
减小数 1 得到的数值 4 。同样地,数字 9 表示为&nbsp;<code>IX</code>。这个特殊的规则只适用于以下六种情况:</p>
<ul>
<li><code>I</code>&nbsp;可以放在&nbsp;<code>V</code>&nbsp;(5) 和&nbsp;<code>X</code>&nbsp;(10) 的左边,来表示 4 和 9。</li>
<li><code>X</code>&nbsp;可以放在&nbsp;<code>L</code>&nbsp;(50) 和&nbsp;<code>C</code>&nbsp;(100) 的左边,来表示 40
&nbsp;90。&nbsp;</li>
<li><code>C</code>&nbsp;可以放在&nbsp;<code>D</code>&nbsp;(500) 和&nbsp;<code>M</code>&nbsp;(1000) 的左边,来表示&nbsp;400
&nbsp;900。</li>
</ul>
<p>给你一个整数,将其转为罗马数字。</p>
<p>&nbsp;</p>
<p><strong>示例&nbsp;1:</strong></p>
<pre><strong>输入:</strong>&nbsp;num = 3
<strong><br />输出:</strong> "III"</pre>
<p><strong>示例&nbsp;2:</strong></p>
<pre><strong>输入:</strong>&nbsp;num = 4
<strong><br />输出:</strong> "IV"</pre>
<p><strong>示例&nbsp;3:</strong></p>
<pre><strong>输入:</strong>&nbsp;num = 9
<strong><br />输出:</strong> "IX"</pre>
<p><strong>示例&nbsp;4:</strong></p>
<pre><strong>输入:</strong>&nbsp;num = 58
<strong><br />输出:</strong> "LVIII"
<strong><br />解释:</strong> L = 50, V = 5, III = 3.
</pre>
<p><strong>示例&nbsp;5:</strong></p>
<pre><strong>输入:</strong>&nbsp;num = 1994
<strong><br />输出:</strong> "MCMXCIV"
<strong><br />解释:</strong> M = 1000, CM = 900, XC = 90, IV = 4.</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= num &lt;= 3999</code></li>
</ul>
</div>
\ No newline at end of file
struct rmap
{
char *r;
int v;
int l;
} units[] = {
{"M", 1000, 1},
{"CM", 900, 2},
{"D", 500, 1},
{"CD", 400, 2},
{"C", 100, 1},
{"XC", 90, 2},
{"L", 50, 1},
{"XL", 40, 2},
{"X", 10, 1},
{"IX", 9, 2},
{"V", 5, 1},
{"IV", 4, 2},
{"I", 1, 1}};
#include <string.h>
int romanToInt(char *s)
{
int len = strlen(s);
char *end = s + len;
int i = 0;
int r = 0;
while (i < 13)
{
if (end - s >= units[i].l && memcmp(s, units[i].r, units[i].l) == 0)
{
r += units[i].v;
s += units[i].l;
}
else
i++;
}
return r;
}
\ No newline at end of file
<p>编写一个函数来查找字符串数组中的最长公共前缀。</p><p>如果不存在公共前缀,返回空字符串 <code>""</code></p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>strs = ["flower","flow","flight"]<strong><br />输出:</strong>"fl"</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>strs = ["dog","racecar","car"]<strong><br />输出:</strong>""<strong><br />解释:</strong>输入不存在公共前缀。</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>0 <= strs.length <= 200</code></li> <li><code>0 <= strs[i].length <= 200</code></li> <li><code>strs[i]</code> 仅由小写英文字母组成</li></ul>
\ No newline at end of file
class Solution
{
public:
string longestCommonPrefix(vector<string> &strs)
{
string lcp;
if (strs.size() == 0)
return lcp;
int min_len = INT_MAX;
int min_idx = 0;
for (int i = 0; i < strs.size(); ++i)
{
auto &s = strs[i];
if (s.size() < min_len)
{
min_len = s.size();
min_idx = i;
}
}
auto &smin = strs[min_idx];
for (int i = 0; i < min_len; ++i)
{
char c = smin[i];
int j;
for (j = 0; j < strs.size(); ++j)
{
auto &cs = strs[j];
if (c != cs[i])
break;
}
if (j == strs.size())
lcp += c;
else
break;
}
return lcp;
}
};
\ 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
<p>给定一个包括&nbsp;<em>n</em> 个整数的数组&nbsp;<code>nums</code><em>&nbsp;</em>和 一个目标值&nbsp;<code>target</code>。找出&nbsp;<code>nums</code><em>&nbsp;</em>中的三个整数,使得它们的和与&nbsp;<code>target</code>&nbsp;最接近。返回这三个数的和。假定每组输入只存在唯一答案。</p><p>&nbsp;</p><p><strong>示例:</strong></p><pre><strong>输入:</strong>nums = [-1,2,1,-4], target = 1<strong><br />输出:</strong>2<strong><br />解释:</strong>与 target 最接近的和是 2 (-1 + 2 + 1 = 2) 。</pre><p>&nbsp;</p><p><strong>提示:</strong></p><ul> <li><code>3 &lt;= nums.length &lt;= 10^3</code></li> <li><code>-10^3&nbsp;&lt;= nums[i]&nbsp;&lt;= 10^3</code></li> <li><code>-10^4&nbsp;&lt;= target&nbsp;&lt;= 10^4</code></li></ul>
\ No newline at end of file
#include <cstdlib>
class Solution
{
public:
int threeSumClosest(vector<int> &nums, int target)
{
sort(nums.begin(), nums.end());
int cur, left, right;
cur = 0;
int closest = nums[0] + nums[1] + nums[2];
while (cur < nums.size() - 2)
{
left = cur + 1;
right = nums.size() - 1;
int n;
while (left < right)
{
n = nums[cur] + nums[left] + nums[right];
if (abs(target - n) < abs(target - closest))
{
closest = n;
}
if (n == target)
{
break;
}
else if (n > target)
{
int t = right - 1;
while (t > left && nums[t] == nums[right])
t--;
right = t;
}
else
{
int t = left + 1;
while (t < right && nums[t] == nums[left])
t++;
left = t;
}
}
int t = cur + 1;
while (t < nums.size() && nums[t] == nums[cur])
t++;
cur = t;
}
return closest;
}
};
\ No newline at end of file
<p>给定一个仅包含数字 <code>2-9</code> 的字符串,返回所有它能表示的字母组合。答案可以按 <strong>任意顺序</strong> 返回。</p><p>给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。</p><p><img src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0017.Letter%20Combinations%20of%20a%20Phone%20Number/images/17_telephone_keypad.png" style="width: 200px;" /></p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>digits = "23"<strong><br />输出:</strong>["ad","ae","af","bd","be","bf","cd","ce","cf"]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>digits = ""<strong><br />输出:</strong>[]</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>digits = "2"<strong><br />输出:</strong>["a","b","c"]</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>0 <= digits.length <= 4</code></li> <li><code>digits[i]</code> 是范围 <code>['2', '9']</code> 的一个数字。</li></ul>
\ No newline at end of file
class Solution
{
public:
vector<string> letterCombinations(string digits)
{
vector<string> nummap({" ",
"",
"abc",
"def",
"ghi",
"jkl",
"mno",
"pqrs",
"tuv",
"wxyz"});
vector<string> rs;
vector<string> empty;
if (digits.size() == 0)
return empty;
for (auto d : digits)
{
if (d == '0')
return empty;
if (d == '1')
return empty;
auto &s = nummap[d - '0'];
if (s.size() == 0)
continue;
if (rs.size() == 0)
for (auto c : s)
{
string t;
t.push_back(c);
rs.emplace_back(t);
}
else
{
vector<string> rn;
for (auto c : s)
{
for (auto r : rs)
{
r.push_back(c);
rn.emplace_back(std::move(r));
}
}
std::swap(rs, rn);
}
}
return rs;
}
};
\ 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
<p>给你一个链表,删除链表的倒数第 <code>n</code><em> </em>个结点,并且返回链表的头结点。</p><p><strong>进阶:</strong>你能尝试使用一趟扫描实现吗?</p><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0019.Remove%20Nth%20Node%20From%20End%20of%20List/images/remove_ex1.jpg" style="width: 542px; height: 222px;" /><pre><strong>输入:</strong>head = [1,2,3,4,5], n = 2<strong><br />输出:</strong>[1,2,3,5]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>head = [1], n = 1<strong><br />输出:</strong>[]</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>head = [1,2], n = 1<strong><br />输出:</strong>[1]</pre><p> </p><p><strong>提示:</strong></p><ul> <li>链表中结点的数目为 <code>sz</code></li> <li><code>1 <= sz <= 30</code></li> <li><code>0 <= Node.val <= 100</code></li> <li><code>1 <= n <= sz</code></li></ul>
\ No newline at end of file
struct ListNode
{
int val;
ListNode *next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode *next) : val(x), next(next) {}
};
#include <vector>
class Solution
{
public:
ListNode *removeNthFromEnd(ListNode *head, int n)
{
ListNode empty_node(0, head);
ListNode *p = &empty_node;
std::vector<ListNode *> pv;
while (p != nullptr)
{
pv.push_back(p);
p = p->next;
}
p = pv[pv.size() - 1 - n];
p->next = p->next->next;
return empty_node.next;
}
};
\ No newline at end of file
<p>给定一个只包括 <code>'('</code><code>')'</code><code>'{'</code><code>'}'</code><code>'['</code><code>']'</code> 的字符串 <code>s</code> ,判断字符串是否有效。</p><p>有效字符串需满足:</p><ol> <li>左括号必须用相同类型的右括号闭合。</li> <li>左括号必须以正确的顺序闭合。</li></ol><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>s = "()"<strong><br />输出:</strong>true</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>s = "()[]{}"<strong><br />输出:</strong>true</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>s = "(]"<strong><br />输出:</strong>false</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>s = "([)]"<strong><br />输出:</strong>false</pre><p><strong>示例 5:</strong></p><pre><strong>输入:</strong>s = "{[]}"<strong><br />输出:</strong>true</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>1 <= s.length <= 10<sup>4</sup></code></li> <li><code>s</code> 仅由括号 <code>'()[]{}'</code> 组成</li></ul>
\ No newline at end of file
#include <stack>
char ascii_tab[128];
class Solution
{
public:
bool isValid(string s)
{
if (s.size() == 0)
return true;
std::stack<char> st;
ascii_tab['('] = 11;
ascii_tab['{'] = 12;
ascii_tab['['] = 13;
ascii_tab[')'] = 21;
ascii_tab['}'] = 22;
ascii_tab[']'] = 23;
for (auto c : s)
{
char n = ascii_tab[c];
if (n < 20)
st.push(n);
else
{
if (st.empty())
return false;
if (n != st.top() + 10)
return false;
st.pop();
}
}
if (st.empty())
return true;
return false;
}
};
\ No newline at end of file
<p>给你两个 <strong>非空</strong> 的链表,表示两个非负的整数。它们每位数字都是按照 <strong>逆序</strong> 的方式存储的,并且每个节点只能存储 <strong>一位</strong> 数字。</p><p>请你将两个数相加,并以相同形式返回一个表示和的链表。</p><p>你可以假设除了数字 0 之外,这两个数都不会以 0 开头。</p><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0002.Add%20Two%20Numbers/images/addtwonumber1.jpg" style="width: 483px; height: 342px;" /><pre><strong>输入:</strong>l1 = [2,4,3], l2 = [5,6,4]<strong><br />输出:</strong>[7,0,8]<strong><br />解释:</strong>342 + 465 = 807.</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>l1 = [0], l2 = [0]<strong><br />输出:</strong>[0]</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]<strong><br />输出:</strong>[8,9,9,9,0,0,0,1]</pre><p> </p><p><strong>提示:</strong></p><ul> <li>每个链表中的节点数在范围 <code>[1, 100]</code></li> <li><code>0 <= Node.val <= 9</code></li> <li>题目数据保证列表表示的数字不含前导零</li></ul>
\ No newline at end of file
struct ListNode
{
int val;
struct ListNode *next;
};
struct ListNode *addTwoNumbers(struct ListNode *l1, struct ListNode *l2)
{
struct ListNode *pp = NULL, *p = l1;
struct ListNode *qp = NULL, *q = l2;
int carry = 0;
while (p != NULL && q != NULL)
{
p->val += q->val + carry;
carry = 0;
if (p->val >= 10)
{
carry = 1;
p->val -= 10;
}
pp = p;
p = p->next;
qp = q;
q = q->next;
}
if (q)
{
pp->next = p = q;
qp->next = NULL;
}
while (carry && p)
{
p->val += carry;
carry = 0;
if (p->val >= 10)
{
carry = 1;
p->val -= 10;
}
pp = p;
p = p->next;
}
if (carry)
{
struct ListNode *n = (struct ListNode *)malloc(sizeof(struct ListNode));
n->val = 1;
n->next = NULL;
pp->next = n;
}
return l1;
}
\ No newline at end of file
<p>将两个升序链表合并为一个新的 <strong>升序</strong> 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 </p><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0021.Merge%20Two%20Sorted%20Lists/images/merge_ex1.jpg" style="width: 662px; height: 302px;" /><pre><strong>输入:</strong>l1 = [1,2,4], l2 = [1,3,4]<strong><br />输出:</strong>[1,1,2,3,4,4]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>l1 = [], l2 = []<strong><br />输出:</strong>[]</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>l1 = [], l2 = [0]<strong><br />输出:</strong>[0]</pre><p> </p><p><strong>提示:</strong></p><ul> <li>两个链表的节点数目范围是 <code>[0, 50]</code></li> <li><code>-100 <= Node.val <= 100</code></li> <li><code>l1</code><code>l2</code> 均按 <strong>非递减顺序</strong> 排列</li></ul>
\ No newline at end of file
struct ListNode
{
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution
{
public:
ListNode *mergeTwoLists(ListNode *l1, ListNode *l2)
{
ListNode h(0, nullptr);
ListNode *p = &h;
while (l1 && l2)
{
ListNode **t;
if (l1->val < l2->val)
t = &l1;
else
t = &l2;
p->next = *t;
p = *t;
*t = (*t)->next;
}
if (l1)
p->next = l1;
else
p->next = l2;
return h.next;
}
};
\ 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
<p>给你一个链表数组,每个链表都已经按升序排列。</p><p>请你将所有链表合并到一个升序链表中,返回合并后的链表。</p><p>&nbsp;</p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>lists = [[1,4,5],[1,3,4],[2,6]]<strong><br />输出:</strong>[1,1,2,3,4,4,5,6]<strong><br />解释:</strong>链表数组如下:[ 1-&gt;4-&gt;5, 1-&gt;3-&gt;4, 2-&gt;6]将它们合并到一个有序链表中得到。1-&gt;1-&gt;2-&gt;3-&gt;4-&gt;4-&gt;5-&gt;6</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>lists = []<strong><br />输出:</strong>[]</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>lists = [[]]<strong><br />输出:</strong>[]</pre><p>&nbsp;</p><p><strong>提示:</strong></p><ul> <li><code>k == lists.length</code></li> <li><code>0 &lt;= k &lt;= 10^4</code></li> <li><code>0 &lt;= lists[i].length &lt;= 500</code></li> <li><code>-10^4 &lt;= lists[i][j] &lt;= 10^4</code></li> <li><code>lists[i]</code><strong>升序</strong> 排列</li> <li><code>lists[i].length</code> 的总和不超过 <code>10^4</code></li></ul>
\ No newline at end of file
#include <bits/stdc++.h>
using namespace std;
struct ListNode
{
int val;
ListNode *next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode *next) : val(x), next(next) {}
};
class Solution
{
public:
ListNode *mergeKLists(vector<ListNode *> &lists)
{
auto cmp = [](struct ListNode *n1, struct ListNode *n2)
{
return n1->val > n2->val;
} priority_queue<struct ListNode *, vector<struct ListNode *>, decltype(cmp)>
queue(cmp);
for (int i = 0; i < lists.size(); i++)
{
if (lists[i] != nullptr)
{
queue.push(lists[i]);
}
}
struct ListNode dummy, *p = &dummy;
;
while (!queue.empty())
{
ListNode *node = queue.top();
queue.pop();
p->next = node;
p = node;
if (node->next != nullptr)
{
queue.push(node->next);
}
}
return dummy.next;
}
};
\ No newline at end of file
<p>给定一个链表,两两交换其中相邻的节点,并返回交换后的链表。</p><p><strong>你不能只是单纯的改变节点内部的值</strong>,而是需要实际的进行节点交换。</p><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0024.Swap%20Nodes%20in%20Pairs/images/swap_ex1.jpg" style="width: 422px; height: 222px;" /><pre><strong>输入:</strong>head = [1,2,3,4]<strong><br />输出:</strong>[2,1,4,3]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>head = []<strong><br />输出:</strong>[]</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>head = [1]<strong><br />输出:</strong>[1]</pre><p> </p><p><strong>提示:</strong></p><ul> <li>链表中节点的数目在范围 <code>[0, 100]</code></li> <li><code>0 <= Node.val <= 100</code></li></ul><p> </p><p><strong>进阶:</strong>你能在不修改链表节点值的情况下解决这个问题吗?(也就是说,仅修改节点本身。)</p>
\ No newline at end of file
#include <bits/stdc++.h>
using namespace std;
struct ListNode
{
int val;
ListNode *next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode *next) : val(x), next(next) {}
};
class Solution
{
public:
ListNode *swapPairs(ListNode *head)
{
struct ListNode dummy, *prev = &dummy, *p = head;
dummy.next = head;
while (p != nullptr && p->next != nullptr)
{
struct ListNode *q = p->next;
p->next = q->next;
q->next = prev->next;
prev->next = q;
prev = p;
p = p->next;
}
return dummy.next;
}
};
\ No newline at end of file
<p>给你一个链表,每 <em></em>个节点一组进行翻转,请你返回翻转后的链表。</p><p><em></em>是一个正整数,它的值小于或等于链表的长度。</p><p>如果节点总数不是 <em></em>的整数倍,那么请将最后剩余的节点保持原有顺序。</p><p><strong>进阶:</strong></p><ul> <li>你可以设计一个只使用常数额外空间的算法来解决此问题吗?</li> <li><strong>你不能只是单纯的改变节点内部的值</strong>,而是需要实际进行节点交换。</li></ul><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0025.Reverse%20Nodes%20in%20k-Group/images/reverse_ex1.jpg" style="width: 542px; height: 222px;" /><pre><strong>输入:</strong>head = [1,2,3,4,5], k = 2<strong><br />输出:</strong>[2,1,4,3,5]</pre><p><strong>示例 2:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0025.Reverse%20Nodes%20in%20k-Group/images/reverse_ex2.jpg" style="width: 542px; height: 222px;" /><pre><strong>输入:</strong>head = [1,2,3,4,5], k = 3<strong><br />输出:</strong>[3,2,1,4,5]</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>head = [1,2,3,4,5], k = 1<strong><br />输出:</strong>[1,2,3,4,5]</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>head = [1], k = 1<strong><br />输出:</strong>[1]</pre><ul></ul><p><strong>提示:</strong></p><ul> <li>列表中节点的数量在范围 <code>sz</code></li> <li><code>1 <= sz <= 5000</code></li> <li><code>0 <= Node.val <= 1000</code></li> <li><code>1 <= k <= sz</code></li></ul>
\ No newline at end of file
#include <bits/stdc++.h>
using namespace std;
struct ListNode
{
int val;
ListNode *next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode *next) : val(x), next(next) {}
};
class Solution
{
public:
ListNode *reverseGroup(ListNode *head, int k)
{
int len = 0;
struct ListNode dummy, *prev = &dummy;
dummy.next = head;
for (; head != nullptr; head = head->next)
{
if (++len % k == 0)
{
struct ListNode *p = prev->next;
while (prev->next != head)
{
struct ListNode *q = p->next;
p->next = q->next;
q->next = prev->next;
prev->next = q;
}
prev = p;
head = p;
}
}
return dummy.next;
}
};
\ No newline at end of file
<div class="notranslate">
<p>给你一个有序数组 <code>nums</code> ,请你<strong><a href="http://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95">
原地</a></strong> 删除重复出现的元素,使每个元素 <strong>只出现一次</strong> ,返回删除后数组的新长度。</p>
<p>不要使用额外的数组空间,你必须在 <strong><a href="https://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95">原地
</a>修改输入数组 </strong>并在使用 O(1) 额外空间的条件下完成。</p>
<p>&nbsp;</p>
<p><strong>说明:</strong></p>
<p>为什么返回数值是整数,但输出的答案是数组呢?</p>
<p>请注意,输入数组是以<strong>「引用」</strong>方式传递的,这意味着在函数里修改输入数组对于调用者是可见的。</p>
<p>你可以想象内部操作如下:</p>
<pre>// <strong>nums</strong> 是以“引用”方式传递的。也就是说,不对实参做任何拷贝
int len = removeDuplicates(nums);
// 在函数里修改输入数组对于调用者是可见的。
// 根据你的函数返回的长度, 它会打印出数组中<strong> 该长度范围内</strong> 的所有元素。
for (int i = 0; i &lt; len; i++) {
&nbsp; &nbsp; print(nums[i]);
}
</pre>
&nbsp;
<p><strong>示例 1:</strong></p>
<pre><strong>输入:</strong>nums = [1,1,2]
<strong><br />输出:</strong>2, nums = [1,2]
<strong><br />解释:</strong>函数应该返回新的长度 <strong>2</strong> ,并且原数组 <em>nums </em>的前两个元素被修改为 <strong>1</strong>, <strong>2 </strong>。不需要考虑数组中超出新长度后面的元素。
</pre>
<p><strong>示例 2:</strong></p>
<pre><strong>输入:</strong>nums = [0,0,1,1,1,2,2,3,3,4]
<strong><br />输出:</strong>5, nums = [0,1,2,3,4]
<strong><br />解释:</strong>函数应该返回新的长度 <strong>5</strong> , 并且原数组 <em>nums </em>的前五个元素被修改为 <strong>0</strong>, <strong>1</strong>, <strong>2</strong>, <strong>3</strong>, <strong>4</strong> 。不需要考虑数组中超出新长度后面的元素。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>0 &lt;= nums.length &lt;= 3 * 10<sup>4</sup></code></li>
<li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>
<li><code>nums</code> 已按升序排列</li>
</ul>
<p>&nbsp;</p>
</div>
\ No newline at end of file
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
int removeDuplicates(vector<int> &nums)
{
if (nums.size() == 0)
{
return 0;
}
int count = 1;
for (int i = 1; i < nums.size(); i++)
{
if (nums[i - 1] != nums[i])
{
nums[count++] = nums[i];
}
}
return count;
}
};
\ No newline at end of file
<div class="notranslate">
<p>给你一个数组 <code>nums</code><em>&nbsp;</em>和一个值 <code>val</code>,你需要 <strong><a
href="https://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95">原地</a></strong>
移除所有数值等于&nbsp;<code>val</code><em>&nbsp;</em>的元素,并返回移除后数组的新长度。</p>
<p>不要使用额外的数组空间,你必须仅使用 <code>O(1)</code> 额外空间并 <strong><a
href="https://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95">原地 </a>修改输入数组</strong></p>
<p>元素的顺序可以改变。你不需要考虑数组中超出新长度后面的元素。</p>
<p>&nbsp;</p>
<p><strong>说明:</strong></p>
<p>为什么返回数值是整数,但输出的答案是数组呢?</p>
<p>请注意,输入数组是以<strong>「引用」</strong>方式传递的,这意味着在函数里修改输入数组对于调用者是可见的。</p>
<p>你可以想象内部操作如下:</p>
<pre>// <strong>nums</strong> 是以“引用”方式传递的。也就是说,不对实参作任何拷贝
int len = removeElement(nums, val);
// 在函数里修改输入数组对于调用者是可见的。
// 根据你的函数返回的长度, 它会打印出数组中<strong> 该长度范围内</strong> 的所有元素。
for (int i = 0; i &lt; len; i++) {
&nbsp; &nbsp; print(nums[i]);
}
</pre>
<p>&nbsp;</p>
<p><strong>示例 1:</strong></p>
<pre><strong>输入:</strong>nums = [3,2,2,3], val = 3
<strong><br />输出:</strong>2, nums = [2,2]
<strong><br />解释:</strong>函数应该返回新的长度 <strong>2</strong>, 并且 nums<em> </em>中的前两个元素均为 <strong>2</strong>。你不需要考虑数组中超出新长度后面的元素。例如,函数返回的新长度为 2 ,而 nums = [2,2,3,3] 或 nums = [2,2,0,0],也会被视作正确答案。
</pre>
<p><strong>示例 2:</strong></p>
<pre><strong>输入:</strong>nums = [0,1,2,2,3,0,4,2], val = 2
<strong><br />输出:</strong>5, nums = [0,1,4,0,3]
<strong><br />解释:</strong>函数应该返回新的长度 <strong>5</strong>, 并且 nums 中的前五个元素为 <strong>0</strong>, <strong>1</strong>, <strong>3</strong>, <strong>0</strong>, <strong>4</strong>。注意这五个元素可为任意顺序。你不需要考虑数组中超出新长度后面的元素。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>0 &lt;= nums.length &lt;= 100</code></li>
<li><code>0 &lt;= nums[i] &lt;= 50</code></li>
<li><code>0 &lt;= val &lt;= 100</code></li>
</ul>
</div>
\ No newline at end of file
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
int removeElement(vector<int> &nums, int val)
{
int count = 0;
for (int i = 0; i < nums.size(); i++)
{
if (nums[i] != val)
{
nums[count++] = nums[i];
}
}
return count;
}
};
\ No newline at end of file
<p>实现 <a href="https://baike.baidu.com/item/strstr/811469" target="_blank">strStr()</a> 函数。</p>
<p>给你两个字符串 <code>haystack</code><code>needle</code> ,请你在 <code>haystack</code> 字符串中找出 <code>needle</code>
字符串出现的第一个位置(下标从 0 开始)。如果不存在,则返回  <code>-1</code><strong> </strong></p>
<p> </p>
<p><strong>说明:</strong></p>
<p>当 <code>needle</code> 是空字符串时,我们应当返回什么值呢?这是一个在面试中很好的问题。</p>
<p>对于本题而言,当 <code>needle</code> 是空字符串时我们应当返回 0 。这与 C 语言的 <a href="https://baike.baidu.com/item/strstr/811469"
target="_blank">strstr()</a> 以及 Java 的 <a
href="https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#indexOf(java.lang.String)"
target="_blank">indexOf()</a> 定义相符。</p>
<p> </p>
<p><strong>示例 1:</strong></p>
<pre><strong>输入:</strong>haystack = "hello", needle = "ll"<strong><br />输出:</strong>2</pre>
<p><strong>示例 2:</strong></p>
<pre><strong>输入:</strong>haystack = "aaaaa", needle = "bba"<strong><br />输出:</strong>-1</pre>
<p><strong>示例 3:</strong></p>
<pre><strong>输入:</strong>haystack = "", needle = ""<strong><br />输出:</strong>0</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>0 <= haystack.length, needle.length <= 5 * 10<sup>4</sup></code></li>
<li><code>haystack</code><code>needle</code> 仅由小写英文字符组成</li>
</ul>
\ No newline at end of file
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
int strStr(string haystack, string needle)
{
return haystack.find(needle);
}
};
\ No newline at end of file
<p>给定两个整数,被除数&nbsp;<code>dividend</code>&nbsp;和除数&nbsp;<code>divisor</code>。将两数相除,要求不使用乘法、除法和 mod 运算符。</p><p>返回被除数&nbsp;<code>dividend</code>&nbsp;除以除数&nbsp;<code>divisor</code>&nbsp;得到的商。</p><p>整数除法的结果应当截去(<code>truncate</code>)其小数部分,例如:<code>truncate(8.345) = 8</code> 以及 <code>truncate(-2.7335) = -2</code></p><p>&nbsp;</p><p><strong>示例&nbsp;1:</strong></p><pre><strong>输入:</strong> dividend = 10, divisor = 3<strong><br />输出:</strong> 3<strong><br />解释: </strong>10/3 = truncate(3.33333..) = truncate(3) = 3</pre><p><strong>示例&nbsp;2:</strong></p><pre><strong>输入:</strong> dividend = 7, divisor = -3<strong><br />输出:</strong> -2<strong><br />解释:</strong> 7/-3 = truncate(-2.33333..) = -2</pre><p>&nbsp;</p><p><strong>提示:</strong></p><ul> <li>被除数和除数均为 32 位有符号整数。</li> <li>除数不为&nbsp;0。</li> <li>假设我们的环境只能存储 32 位有符号整数,其数值范围是 [&minus;2<sup>31</sup>,&nbsp; 2<sup>31&nbsp;</sup>&minus; 1]。本题中,如果除法结果溢出,则返回 2<sup>31&nbsp;</sup>&minus; 1。</li></ul>
\ No newline at end of file
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
int divide(int dividend, int divisor)
{
int signal = 1;
unsigned int dvd = dividend;
if (dividend < 0)
{
signal *= -1;
dvd = ~dvd + 1;
}
unsigned int dvs = divisor;
if (divisor < 0)
{
signal *= -1;
dvs = ~dvs + 1;
}
int shift = 0;
while (dvd > dvs << shift)
{
shift++;
}
unsigned int res = 0;
while (dvd >= dvs)
{
while (dvd < dvs << shift)
{
shift--;
}
res |= (unsigned int)1 << shift;
dvd -= dvs << shift;
}
if (signal == 1 && res >= INT_MAX)
{
return INT_MAX;
}
else
{
return res * signal;
}
}
};
\ No newline at end of file
<p>给定一个字符串&nbsp;<strong>s&nbsp;</strong>和一些长度相同的单词&nbsp;<strong>words。</strong>找出 <strong>s
</strong>中恰好可以由&nbsp;<strong>words </strong>中所有单词串联形成的子串的起始位置。</p>
<p>注意子串要与&nbsp;<strong>words </strong>中的单词完全匹配,中间不能有其他字符,但不需要考虑&nbsp;<strong>words&nbsp;</strong>中单词串联的顺序。</p>
<p>&nbsp;</p>
<p><strong>示例 1:</strong></p>
<pre><strong>输入: s =</strong> &quot;barfoothefoobarman&quot;,<strong> words = </strong>[&quot;foo&quot;,&quot;bar&quot;]<strong><br />输出:</strong>[0,9]<strong><br />解释:</strong>从索引 0 和 9 开始的子串分别是 &quot;barfoo&quot;&quot;foobar&quot; 。输出的顺序不重要, [9,0] 也是有效答案。</pre>
<p><strong>示例 2:</strong></p>
<pre><strong>输入: s =</strong> &quot;wordgoodgoodgoodbestword&quot;,<strong> words = </strong>[&quot;word&quot;,&quot;good&quot;,&quot;best&quot;,&quot;word&quot;]<strong><br />输出:</strong>[]</pre>
\ No newline at end of file
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
vector<int> findSubstring(string s, vector<string> &words)
{
vector<int> res;
if (s.empty() || words.empty())
{
return res;
}
unordered_map<string, int> ht;
for (const auto &w : words)
{
ht[w]++;
}
int len = words[0].length();
for (int i = 0, j = 0; i < s.length() - words.size() * len + 1; i++)
{
unordered_map<string, int> counting;
for (j = 0; j < words.size(); j++)
{
string word = s.substr(i + j * len, len);
if (++counting[word] > ht[word])
{
break;
}
}
if (j == words.size())
{
res.push_back(i);
}
}
return res;
}
};
\ No newline at end of file
<p>给定一个字符串,请你找出其中不含有重复字符的 <strong>最长子串 </strong>的长度。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入: </strong>s = "abcabcbb"<strong><br />输出: </strong>3 <strong><br />解释:</strong> 因为无重复字符的最长子串是 "abc",所以其长度为 3。</pre><p><strong>示例 2:</strong></p><pre><strong>输入: </strong>s = "bbbbb"<strong><br />输出: </strong>1<strong><br />解释: </strong>因为无重复字符的最长子串是 "b",所以其长度为 1。</pre><p><strong>示例 3:</strong></p><pre><strong>输入: </strong>s = "pwwkew"<strong><br />输出: </strong>3<strong><br />解释: </strong>因为无重复字符的最长子串是 "wke",所以其长度为 3。 
请注意,你的答案必须是 <strong>子串 </strong>的长度,"pwke" 是一个<em>子序列,</em>不是子串。</pre><p><strong>示例 4:</strong></p><pre><strong>输入: </strong>s = ""<strong><br />输出: </strong>0</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>0 <= s.length <= 5 * 10<sup>4</sup></code></li> <li><code>s</code> 由英文字母、数字、符号和空格组成</li></ul>
\ No newline at end of file
int hset[128];
int lengthOfLongestSubstring(char *s)
{
int i = 0, j = 0;
int m = 0;
memset(hset, 0, sizeof hset);
for (; s[j]; j++)
{
i = hset[s[j]] > i ? hset[s[j]] : i;
m = m > j - i + 1 ? m : j - i + 1;
hset[s[j]] = j + 1;
}
return m;
}
\ No newline at end of file
<p>实现获取 <strong>下一个排列</strong> 的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列。</p><p>如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列)。</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>修改,只允许使用额外常数空间。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>nums = [1,2,3]<strong><br />输出:</strong>[1,3,2]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>nums = [3,2,1]<strong><br />输出:</strong>[1,2,3]</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>nums = [1,1,5]<strong><br />输出:</strong>[1,5,1]</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>nums = [1]<strong><br />输出:</strong>[1]</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>1 <= nums.length <= 100</code></li> <li><code>0 <= nums[i] <= 100</code></li></ul>
\ No newline at end of file
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
void nextPermutation(vector<int> &nums)
{
if (nums.size() < 2)
{
return;
}
int i = nums.size() - 2;
while (i >= 0 && nums[i] >= nums[i + 1])
{
i--;
}
if (i >= 0)
{
int j = nums.size() - 1;
while (j >= 0 && nums[j] >= nums[i])
{
j--;
}
swap(nums.begin() + i, nums.begin() + j);
}
reverse(nums.begin() + i + 1, nums.end());
}
};
\ 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
<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
<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
<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
<p>请你判断一个 <code>9x9</code> 的数独是否有效。只需要<strong> 根据以下规则</strong> ,验证已经填入的数字是否有效即可。</p>
<ol>
<li>数字 <code>1-9</code> 在每一行只能出现一次。</li>
<li>数字 <code>1-9</code> 在每一列只能出现一次。</li>
<li>数字 <code>1-9</code> 在每一个以粗实线分隔的 <code>3x3</code> 宫内只能出现一次。(请参考示例图)</li>
</ol>
<p>数独部分空格内已填入了数字,空白格用 <code>'.'</code> 表示。</p>
<p><strong>注意:</strong></p>
<ul>
<li>一个有效的数独(部分已被填充)不一定是可解的。</li>
<li>只需要根据以上规则,验证已经填入的数字是否有效即可。</li>
</ul>
<p> </p>
<p><strong>示例 1:</strong></p><img
src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0036.Valid%20Sudoku/images/250px-sudoku-by-l2g-20050714svg.png"
style="height:250px; width:250px" />
<pre><strong>输入:</strong>board =
[["5","3",".",".","7",".",".",".","."]
,["6",".",".","1","9","5",".",".","."]
,[".","9","8",".",".",".",".","6","."]
,["8",".",".",".","6",".",".",".","3"]
,["4",".",".","8",".","3",".",".","1"]
,["7",".",".",".","2",".",".",".","6"]
,[".","6",".",".",".",".","2","8","."]
,[".",".",".","4","1","9",".",".","5"]
,[".",".",".",".","8",".",".","7","9"]]
<strong><br />输出:</strong>true
</pre>
<p><strong>示例 2:</strong></p>
<pre><strong>输入:</strong>board =
[["8","3",".",".","7",".",".",".","."]
,["6",".",".","1","9","5",".",".","."]
,[".","9","8",".",".",".",".","6","."]
,["8",".",".",".","6",".",".",".","3"]
,["4",".",".","8",".","3",".",".","1"]
,["7",".",".",".","2",".",".",".","6"]
,[".","6",".",".",".",".","2","8","."]
,[".",".",".","4","1","9",".",".","5"]
,[".",".",".",".","8",".",".","7","9"]]
<strong><br />输出:</strong>false
<strong><br />解释:</strong>除了第一行的第一个数字从<strong> 5</strong> 改为 <strong>8 </strong>以外,空格内其他数字均与 示例1 相同。 但由于位于左上角的 3x3 宫内有两个 8 存在, 因此这个数独是无效的。</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>board.length == 9</code></li>
<li><code>board[i].length == 9</code></li>
<li><code>board[i][j]</code> 是一位数字或者 <code>'.'</code></li>
</ul>
\ No newline at end of file
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
bool isValidSudoku(vector<vector<char>> &board)
{
for (int i = 0; i < board.size(); i++)
{
vector<bool> mark(10);
for (int j = 0; j < board.size(); j++)
{
if (!valid(board, mark, i, j))
{
return false;
}
}
}
for (int j = 0; j < board.size(); j++)
{
vector<bool> mark(10);
for (int i = 0; i < board.size(); i++)
{
if (!valid(board, mark, i, j))
{
return false;
}
}
}
for (int k = 0; k < board.size(); k++)
{
int sr = k / 3 * 3;
int sc = (k % 3) * 3;
vector<bool> mark(10);
for (int i = sr; i < sr + 3; i++)
{
for (int j = sc; j < sc + 3; j++)
{
if (!valid(board, mark, i, j))
{
return false;
}
}
}
}
return true;
}
private:
bool valid(vector<vector<char>> &board, vector<bool> &mark, int i, int j)
{
if (board[i][j] != '.')
{
int index = board[i][j] - '0';
if (mark[index])
{
return false;
}
else
{
mark[index] = 1;
}
}
return true;
}
};
\ No newline at end of file
<p>编写一个程序,通过填充空格来解决数独问题。</p>
<p>数独的解法需<strong> 遵循如下规则</strong></p>
<ol>
<li>数字 <code>1-9</code> 在每一行只能出现一次。</li>
<li>数字 <code>1-9</code> 在每一列只能出现一次。</li>
<li>数字 <code>1-9</code> 在每一个以粗实线分隔的 <code>3x3</code> 宫内只能出现一次。(请参考示例图)</li>
</ol>
<p>数独部分空格内已填入了数字,空白格用 <code>'.'</code> 表示。</p>
<p> </p>
<div class="top-view__1vxA">
<div class="original__bRMd">
<div>
<p><strong>示例:</strong></p><img
src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0037.Sudoku%20Solver/images/250px-sudoku-by-l2g-20050714svg.png" />
<pre><strong>输入:</strong>board =
[["5","3",".",".","7",".",".",".","."],
["6",".",".","1","9","5",".",".","."],
[".","9","8",".",".",".",".","6","."],
["8",".",".",".","6",".",".",".","3"],
["4",".",".","8",".","3",".",".","1"],
["7",".",".",".","2",".",".",".","6"],
[".","6",".",".",".",".","2","8","."],
[".",".",".","4","1","9",".",".","5"],
[".",".",".",".","8",".",".","7","9"]]
<strong><br />输出:</strong>
[["5","3","4","6","7","8","9","1","2"],
["6","7","2","1","9","5","3","4","8"],
["1","9","8","3","4","2","5","6","7"],
["8","5","9","7","6","1","4","2","3"],
["4","2","6","8","5","3","7","9","1"],
["7","1","3","9","2","4","8","5","6"],
["9","6","1","5","3","7","2","8","4"],
["2","8","7","4","1","9","6","3","5"],
["3","4","5","2","8","6","1","7","9"]]
<strong><br />解释:</strong>输入的数独如上图所示,唯一有效的解决方案如下所示:
<p> </p>
</pre>
<img src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0037.Sudoku%20Solver/images/250px-sudoku-by-l2g-20050714_solutionsvg.png"
style="height:250px; width:250px" />
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>board.length == 9</code></li>
<li><code>board[i].length == 9</code></li>
<li><code>board[i][j]</code> 是一位数字或者 <code>'.'</code></li>
<li>题目数据 <strong>保证</strong> 输入数独仅有一个解</li>
</ul>
</div>
</div>
</div>
\ No newline at end of file
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
void solveSudoku(vector<vector<char>> &board)
{
int size = board.size();
vector<vector<bool>> rows(size, vector<bool>(10));
vector<vector<bool>> cols(size, vector<bool>(10));
vector<vector<bool>> boxes(size, vector<bool>(10));
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
if (board[i][j] != '.')
{
int num = board[i][j] - '0';
int idx = i / 3 * 3 + j / 3;
rows[i][num] = true;
cols[j][num] = true;
boxes[idx][num] = true;
}
}
}
dfs(board, 0, rows, cols, boxes);
}
private:
bool valid(int num, int row, int col, int idx, vector<vector<bool>> &rows,
vector<vector<bool>> &cols, vector<vector<bool>> &boxes)
{
return !rows[row][num] && !cols[col][num] && !boxes[idx][num];
}
bool dfs(vector<vector<char>> &board, int size, vector<vector<bool>> &rows,
vector<vector<bool>> &cols, vector<vector<bool>> &boxes)
{
if (size == 9 * 9)
{
return true;
}
else
{
bool ok = false;
int row = size / 9;
int col = size % 9;
int idx = row / 3 * 3 + col / 3;
if (board[row][col] == '.')
{
for (int i = 1; i <= 9; i++)
{
if (valid(i, row, col, idx, rows, cols, boxes))
{
board[row][col] = i + '0';
rows[row][i] = true;
cols[col][i] = true;
boxes[idx][i] = true;
ok = dfs(board, size + 1, rows, cols, boxes);
if (!ok)
{
rows[row][i] = false;
cols[col][i] = false;
boxes[idx][i] = false;
board[row][col] = '.';
}
}
}
}
else
{
ok = dfs(board, size + 1, rows, cols, boxes);
}
return ok;
}
}
};
\ No newline at end of file
<div class="notranslate">
<p>给定一个正整数 <code>n</code> ,输出外观数列的第 <code>n</code> 项。</p>
<p>「外观数列」是一个整数序列,从数字 1 开始,序列中的每一项都是对前一项的描述。</p>
<p>你可以将其视作是由递归公式定义的数字字符串序列:</p>
<ul>
<li><code>countAndSay(1) = "1"</code></li>
<li><code>countAndSay(n)</code> 是对 <code>countAndSay(n-1)</code> 的描述,然后转换成另一个数字字符串。</li>
</ul>
<p>前五项如下:</p>
<pre>
1. 1
2. 11
3. 21
4. 1211
5. 111221
第一项是数字 1
描述前一项,这个数是 1 即 “ 一 个 1 ”,记作 "11"
描述前一项,这个数是 11 即 “ 二 个 1 ” ,记作 "21"
描述前一项,这个数是 21 即 “ 一 个 2 + 一 个 1 ” ,记作 "1211"
描述前一项,这个数是 1211 即 “ 一 个 1 + 一 个 2 + 二 个 1 ” ,记作 "111221"
</pre>
<p><strong>描述</strong> 一个数字字符串,首先要将字符串分割为 <strong>最小</strong> 数量的组,每个组都由连续的最多 <strong>相同字符</strong>
组成。然后对于每个组,先描述字符的数量,然后描述字符,形成一个描述组。要将描述转换为数字字符串,先将每组中的字符数量用数字替换,再将所有描述组连接起来。</p>
<p>例如,数字字符串 <code>"3322251"</code> 的描述如下图:</p>
<img alt=""
src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0038.Count%20and%20Say/images/countandsay.jpg"
style="width: 581px; height: 172px;" />
<ul>
</ul>
<p>&nbsp;</p>
<p><strong>示例 1:</strong></p>
<pre><strong>输入:</strong>n = 1
<strong><br />输出:</strong>"1"
<strong><br />解释:</strong>这是一个基本样例。
</pre>
<p><strong>示例 2:</strong></p>
<pre><strong>输入:</strong>n = 4
<strong><br />输出:</strong>"1211"
<strong><br />解释:</strong>
countAndSay(1) = "1"
countAndSay(2) = 读 "1" = 一 个 1 = "11"
countAndSay(3) = 读 "11" = 二 个 1 = "21"
countAndSay(4) = 读 "21" = 一 个 2 + 一 个 1 = "12" + "11" = "1211"
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= n &lt;= 30</code></li>
</ul>
</div>
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static void parse(char *input, char *output)
{
char *p = input;
char *q = output;
while (*p != '\0')
{
int count = 1;
while (p[0] == p[1])
{
count++;
p++;
}
int n = 0;
while (count > 0)
{
n += count % 10;
count /= 10;
}
while (n > 0)
{
*q++ = (n % 10) + '0';
n /= 10;
}
*q++ = p[0];
p++;
}
*q = '\0';
}
static char *countAndSay(int n)
{
if (n < 1)
{
return NULL;
}
char *result;
char *prev = malloc(10000);
char *next = malloc(10000);
strcpy(prev, "1");
if (n == 1)
{
return prev;
}
int i;
for (i = 2; i <= n; i++)
{
if (i & 0x1)
{
parse(next, prev);
result = prev;
}
else
{
parse(prev, next);
result = next;
}
}
return result;
}
int main(int argc, char **argv)
{
if (argc != 2)
{
fprintf(stderr, "Usage: ./test n\n");
exit(-1);
}
printf("%s\n", countAndSay(atoi(argv[1])));
return 0;
}
\ No newline at end of file
<p>给定一个<strong>无重复元素</strong>的数组&nbsp;<code>candidates</code>&nbsp;和一个目标数&nbsp;<code>target</code>&nbsp;,找出&nbsp;<code>candidates</code>&nbsp;中所有可以使数字和为&nbsp;<code>target</code>&nbsp;的组合。
</p>
<p><code>candidates</code>&nbsp;中的数字可以无限制重复被选取。</p>
<p><strong>说明:</strong></p>
<ul>
<li>所有数字(包括&nbsp;<code>target</code>)都是正整数。</li>
<li>解集不能包含重复的组合。&nbsp;</li>
</ul>
<p><strong>示例&nbsp;1:</strong></p>
<pre><strong>输入:</strong>candidates = [2,3,6,7], target = 7,<strong><br />输出:</strong>[[7],[2,2,3]]</pre>
<p><strong>示例&nbsp;2:</strong></p>
<pre><strong>输入:</strong>candidates = [2,3,5], target = 8,<strong><br />输出:</strong>[[2,2,2,2],[2,3,3],[3,5]]</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= candidates.length &lt;= 30</code></li>
<li><code>1 &lt;= candidates[i] &lt;= 200</code></li>
<li><code>candidate</code> 中的每个元素都是独一无二的。</li>
<li><code>1 &lt;= target &lt;= 500</code></li>
</ul>
\ No newline at end of file
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
vector<vector<int>> combinationSum(vector<int> &candidates, int target)
{
vector<vector<int>> res;
dfs(candidates, 0, target, res);
return res;
}
private:
vector<int> stack;
void dfs(vector<int> &candidates, int start, int target, vector<vector<int>> &res)
{
if (target < 0)
{
return;
}
else if (target == 0)
{
res.push_back(stack);
}
else
{
for (int i = start; i < candidates.size(); i++)
{
stack.push_back(candidates[i]);
dfs(candidates, i, target - candidates[i], res);
stack.pop_back();
}
}
}
};
\ No newline at end of file
<p>给定一个数组&nbsp;<code>candidates</code>&nbsp;和一个目标数&nbsp;<code>target</code>&nbsp;,找出&nbsp;<code>candidates</code>&nbsp;中所有可以使数字和为&nbsp;<code>target</code>&nbsp;的组合。
</p>
<p><code>candidates</code>&nbsp;中的每个数字在每个组合中只能使用一次。</p>
<p><strong>说明:</strong></p>
<ul>
<li>所有数字(包括目标数)都是正整数。</li>
<li>解集不能包含重复的组合。&nbsp;</li>
</ul>
<p><strong>示例&nbsp;1:</strong></p>
<pre><strong>输入:</strong> candidates =&nbsp;[10,1,2,7,6,1,5], target =&nbsp;8,<strong><br />所求解集为:</strong>[[1, 7],[1, 2, 5],[2, 6],[1, 1, 6]]</pre>
<p><strong>示例&nbsp;2:</strong></p>
<pre><strong>输入:</strong> candidates =&nbsp;[2,5,2,1,2], target =&nbsp;5,<strong><br />所求解集为:</strong>[[1,2,2],[5]]</pre>
\ No newline at end of file
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
vector<vector<int>> combinationSum2(vector<int> &candidates, int target)
{
vector<vector<int>> res;
sort(candidates.begin(), candidates.end());
dfs(candidates, 0, target, res);
return res;
}
private:
vector<int> stack;
void dfs(vector<int> &candidates, int start, int target, vector<vector<int>> &res)
{
if (target < 0)
{
return;
}
else if (target == 0)
{
res.push_back(stack);
}
else
{
int last = INT_MIN;
for (int i = start; i < candidates.size(); i++)
{
if (last != candidates[i])
{
stack.push_back(candidates[i]);
dfs(candidates, i + 1, target - candidates[i], res);
stack.pop_back();
}
last = candidates[i];
}
}
}
};
\ 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
<p>给你一个未排序的整数数组 <code>nums</code> ,请你找出其中没有出现的最小的正整数。</p><p> </p><p><strong>进阶:</strong>你可以实现时间复杂度为 <code>O(n)</code> 并且只使用常数级别额外空间的解决方案吗?</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>nums = [1,2,0]<strong><br />输出:</strong>3</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>nums = [3,4,-1,1]<strong><br />输出:</strong>2</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>nums = [7,8,9,11,12]<strong><br />输出:</strong>1</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>0 <= nums.length <= 300</code></li> <li><code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code></li></ul>
\ No newline at end of file
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
int firstMissingPositive(vector<int> &nums)
{
if (nums.size() == 0)
{
return 1;
}
int i = 0;
while (i < nums.size())
{
if (nums[i] > 0 && nums[i] != i + 1 && nums[i] - 1 < nums.size() && nums[nums[i] - 1] != nums[i])
{
swap(nums[i], nums[nums[i] - 1]);
}
else
{
i++;
}
}
for (i = 0; i < nums.size(); i++)
{
if (nums[i] != i + 1)
{
break;
}
}
return i + 1;
}
};
\ No newline at end of file
<p>给定 <em>n</em> 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。</p><p> </p><p><strong>示例 1:</strong></p><p><img src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0042.Trapping%20Rain%20Water/images/rainwatertrap.png" style="height: 161px; width: 412px;" /></p><pre><strong>输入:</strong>height = [0,1,0,2,1,0,1,3,2,1,2,1]<strong><br />输出:</strong>6<strong><br />解释:</strong>上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况下,可以接 6 个单位的雨水(蓝色部分表示雨水)。 </pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>height = [4,2,0,3,2,5]<strong><br />输出:</strong>9</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>n == height.length</code></li> <li><code>0 <= n <= 3 * 10<sup>4</sup></code></li> <li><code>0 <= height[i] <= 10<sup>5</sup></code></li></ul>
\ No newline at end of file
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
int trap(vector<int> &height)
{
int res = 0;
int left = 0, left_max = 0;
int right = height.size() - 1, right_max = 0;
while (left < right)
{
if (height[left] < height[right])
{
if (height[left] > left_max)
{
left_max = height[left];
}
else
{
res += left_max - height[left];
}
left++;
}
else
{
if (height[right] > right_max)
{
right_max = height[right];
}
else
{
res += right_max - height[right];
}
right--;
}
}
return res;
}
};
\ No newline at end of file
<p>给定两个以字符串形式表示的非负整数&nbsp;<code>num1</code>&nbsp;&nbsp;<code>num2</code>,返回&nbsp;<code>num1</code>&nbsp;&nbsp;<code>num2</code>&nbsp;的乘积,它们的乘积也表示为字符串形式。</p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong> num1 = &quot;2&quot;, num2 = &quot;3&quot;<strong><br />输出:</strong> &quot;6&quot;</pre><p><strong>示例&nbsp;2:</strong></p><pre><strong>输入:</strong> num1 = &quot;123&quot;, num2 = &quot;456&quot;<strong><br />输出:</strong> &quot;56088&quot;</pre><p><strong>说明:</strong></p><ol> <li><code>num1</code>&nbsp;&nbsp;<code>num2</code>&nbsp;的长度小于110。</li> <li><code>num1</code>&nbsp;<code>num2</code> 只包含数字&nbsp;<code>0-9</code></li> <li><code>num1</code>&nbsp;<code>num2</code>&nbsp;均不以零开头,除非是数字 0 本身。</li> <li><strong>不能使用任何标准库的大数类型(比如 BigInteger)</strong><strong>直接将输入转换为整数来处理</strong></li></ol>
\ No newline at end of file
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
string multiply(string num1, string num2)
{
string res(num1.length() + num2.length(), '0');
for (int i = num2.length() - 1; i >= 0; i--)
{
int j, carry = 0;
for (j = num1.length() - 1; j >= 0; j--)
{
carry += (num1[j] - '0') * (num2[i] - '0') + (res[i + j + 1] - '0');
res[i + j + 1] = carry % 10 + '0';
carry /= 10;
}
res[i + j + 1] = carry + '0';
}
int i;
for (i = 0; i < res.length() - 1; i++)
{
if (res[i] != '0')
{
break;
}
}
return res.substr(i);
}
};
\ 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
<p>给定一个非负整数数组,你最初位于数组的第一个位置。</p>
<p>数组中的每个元素代表你在该位置可以跳跃的最大长度。</p>
<p>你的目标是使用最少的跳跃次数到达数组的最后一个位置。</p>
<p><strong>示例:</strong></p>
<pre><strong>输入:</strong> [2,3,1,1,4]<strong><br />输出:</strong> 2<strong><br />解释:</strong> 跳到最后一个位置的最小跳跃数是 2。从下标为 0 跳到下标为 1 的位置,跳&nbsp;1&nbsp;步,然后跳&nbsp;3&nbsp;步到达数组的最后一个位置。</pre>
<p><strong>说明:</strong></p>
<p>假设你总是可以到达数组的最后一个位置。</p>
\ No newline at end of file
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
int jump(vector<int> &nums)
{
int steps = 0;
int lo = 0, hi = 0;
while (hi < nums.size() - 1)
{
int right = 0;
for (int i = lo; i <= hi; i++)
{
right = max(i + nums[i], right);
}
lo = hi + 1;
hi = right;
steps++;
}
return steps;
}
};
\ No newline at end of file
<p>给定一个<strong> 没有重复</strong> 数字的序列,返回其所有可能的全排列。</p><p><strong>示例:</strong></p><pre><strong>输入:</strong> [1,2,3]<strong><br />输出:</strong>[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]]</pre>
\ No newline at end of file
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
vector<vector<int>> permute(vector<int> &nums)
{
vector<vector<int>> res;
vector<bool> used(nums.size());
dfs(nums, used, res);
return res;
}
private:
vector<int> stack;
void dfs(vector<int> &nums, vector<bool> &used, vector<vector<int>> &res)
{
if (stack.size() == nums.size())
{
res.push_back(stack);
}
else
{
for (int i = 0; i < nums.size(); i++)
{
if (!used[i])
{
used[i] = true;
stack.push_back(nums[i]);
dfs(nums, used, res);
stack.pop_back();
used[i] = false;
}
}
}
}
};
\ No newline at end of file
<p>给定一个可包含重复数字的序列 <code>nums</code><strong>按任意顺序</strong> 返回所有不重复的全排列。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>nums = [1,1,2]<strong><br />输出:</strong>[[1,1,2], [1,2,1], [2,1,1]]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>nums = [1,2,3]<strong><br />输出:</strong>[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>1 <= nums.length <= 8</code></li> <li><code>-10 <= nums[i] <= 10</code></li></ul>
\ No newline at end of file
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
vector<vector<int>> permuteUnique(vector<int> &nums)
{
vector<vector<int>> res;
vector<bool> used(nums.size());
sort(nums.begin(), nums.end());
dfs(nums, used, res);
return res;
}
private:
vector<int> stack;
void dfs(vector<int> &nums, vector<bool> &used, vector<vector<int>> &res)
{
if (stack.size() == nums.size())
{
res.push_back(stack);
}
else
{
for (int i = 0; i < nums.size(); i++)
{
if (!used[i])
{
if (i > 0 && !used[i - 1] && nums[i - 1] == nums[i])
{
continue;
}
stack.push_back(nums[i]);
used[i] = true;
dfs(nums, used, res);
stack.pop_back();
used[i] = false;
}
}
}
}
};
\ 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
<p>给定一个字符串数组,将字母异位词组合在一起。字母异位词指字母相同,但排列不同的字符串。</p>
<p><strong>示例:</strong></p>
<pre><strong>输入:</strong>[eat&quot;, &quot;tea&quot;, &quot;tan&quot;, &quot;ate&quot;, &quot;nat&quot;, &quot;bat&quot;]<strong><br />输出:</strong>[[ate&quot;,&quot;eat&quot;,&quot;tea&quot;],[&quot;nat&quot;,&quot;tan&quot;],[&quot;bat&quot;]]</pre>
<p><strong>说明:</strong></p>
<ul>
<li>所有输入均为小写字母。</li>
<li>不考虑答案输出的顺序。</li>
</ul>
\ No newline at end of file
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
vector<vector<string>> groupAnagrams(vector<string> &strs)
{
vector<vector<string>> res;
unordered_map<string, vector<string>> ht;
for (const auto &str : strs)
{
int counts[26] = {0};
for (char c : str)
{
counts[c - 'a']++;
}
string key;
for (int i : counts)
{
key.push_back('#');
key.push_back(i + '0');
}
ht[key].push_back(str);
}
for (const auto &t : ht)
{
res.push_back(t.second);
}
return res;
}
};
\ No newline at end of file
<p>实现 <a href="https://www.cplusplus.com/reference/valarray/pow/" target="_blank">pow(<em>x</em>, <em>n</em>)</a> ,即计算 x 的 n 次幂函数(即,x<sup><span style="font-size:10.8333px">n</span></sup>)。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>x = 2.00000, n = 10<strong><br />输出:</strong>1024.00000</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>x = 2.10000, n = 3<strong><br />输出:</strong>9.26100</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>x = 2.00000, n = -2<strong><br />输出:</strong>0.25000<strong><br />解释:</strong>2<sup>-2</sup> = 1/2<sup>2</sup> = 1/4 = 0.25</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>-100.0 < x < 100.0</code></li> <li><code>-2<sup>31</sup> <= n <= 2<sup>31</sup>-1</code></li> <li><code>-10<sup>4</sup> <= x<sup>n</sup> <= 10<sup>4</sup></code></li></ul>
\ No newline at end of file
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
double myPow(double x, int n)
{
if (n == INT_MIN)
{
double t = dfs(x, -(n / 2));
return 1 / t * 1 / t;
}
else
{
return n < 0 ? 1 / dfs(x, -n) : dfs(x, n);
}
}
private:
double dfs(double x, int n)
{
if (n == 0)
{
return 1;
}
else if (n == 1)
{
return x;
}
else
{
double t = dfs(x, n / 2);
return (n % 2) ? (x * t * t) : (t * t);
}
}
};
\ 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
<p><strong>n 皇后问题</strong> 研究的是如何将 <code>n</code> 个皇后放置在 <code>n×n</code> 的棋盘上,并且使皇后彼此之间不能相互攻击。</p><p>给你一个整数 <code>n</code> ,返回所有不同的 <strong>n<em> </em>皇后问题</strong> 的解决方案。</p><div class="original__bRMd"><div><p>每一种解法包含一个不同的 <strong>n 皇后问题</strong> 的棋子放置方案,该方案中 <code>'Q'</code><code>'.'</code> 分别代表了皇后和空位。</p><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0051.N-Queens/images/queens.jpg" style="width: 600px; height: 268px;" /><pre><strong>输入:</strong>n = 4<strong><br />输出:</strong>[[".Q..","...Q","Q...","..Q."],["..Q.","Q...","...Q",".Q.."]]<strong><br />解释:</strong>如上图所示,4 皇后问题存在两个不同的解法。</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>n = 1<strong><br />输出:</strong>[["Q"]]</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>1 <= n <= 9</code></li> <li>皇后彼此不能相互攻击,也就是说:任何两个皇后都不能处于同一条横行、纵行或斜线上。</li></ul></div></div>
\ No newline at end of file
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
vector<vector<string>> solveNQueens(int n)
{
vector<vector<string>> res;
vector<int> stack(n);
vector<string> solution(n, string(n, '.'));
dfs(n, 0, stack, solution, res);
return res;
}
private:
void dfs(int n, int row, vector<int> &stack, vector<string> &solution, vector<vector<string>> &res)
{
if (row == n)
{
res.push_back(solution);
}
else
{
for (int i = 0; i < n; i++)
{
if (row == 0 || !conflict(stack, row, i))
{
solution[row][i] = 'Q';
stack[row] = i;
dfs(n, row + 1, stack, solution, res);
solution[row][i] = '.';
}
}
}
}
bool conflict(vector<int> &stack, int row, int col)
{
for (int i = 0; i < row; i++)
{
if (col == stack[i] || abs(row - i) == abs(col - stack[i]))
{
return true;
}
}
return false;
}
}
\ No newline at end of file
<div class="notranslate">
<p><strong>n&nbsp;皇后问题</strong> 研究的是如何将 <code>n</code>&nbsp;个皇后放置在 <code>n×n</code> 的棋盘上,并且使皇后彼此之间不能相互攻击。</p>
<p>给你一个整数 <code>n</code> ,返回 <strong>n 皇后问题</strong> 不同的解决方案的数量。</p>
<p>&nbsp;</p>
<div class="original__bRMd">
<div>
<p><strong>示例 1:</strong></p>
<img style="width: 600px; height: 268px;" src="https://assets.leetcode.com/uploads/2020/11/13/queens.jpg"
alt="">
<pre><strong>输入:</strong>n = 4
<strong><br />输出:</strong>2
<strong><br />解释:</strong>如上图所示,4 皇后问题存在两个不同的解法。
</pre>
<p><strong>示例 2:</strong></p>
<pre><strong>输入:</strong>n = 1
<strong><br />输出:</strong>1
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= n &lt;= 9</code></li>
<li>皇后彼此不能相互攻击,也就是说:任何两个皇后都不能处于同一条横行、纵行或斜线上。</li>
</ul>
</div>
</div>
</div>
\ No newline at end of file
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
int totalNQueens(int n)
{
vector<int> stack(n);
return dfs(n, 0, stack);
}
private:
int dfs(int n, int row, vector<int> &stack)
{
int count = 0;
if (row == n)
{
return count + 1;
}
else
{
for (int i = 0; i < n; i++)
{
if (row == 0 || !conflict(stack, row, i))
{
stack[row] = i;
count += dfs(n, row + 1, stack);
}
}
return count;
}
}
bool conflict(vector<int> &stack, int row, int col)
{
for (int i = 0; i < row; i++)
{
if (col == stack[i] || abs(row - i) == abs(col - stack[i]))
{
return true;
}
}
return false;
}
}
\ 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
<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
<p>给定一个非负整数数组 <code>nums</code> ,你最初位于数组的 <strong>第一个下标</strong></p><p>数组中的每个元素代表你在该位置可以跳跃的最大长度。</p><p>判断你是否能够到达最后一个下标。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>nums = [2,3,1,1,4]<strong><br />输出:</strong>true<strong><br />解释:</strong>可以先跳 1 步,从下标 0 到达下标 1, 然后再从下标 1 跳 3 步到达最后一个下标。</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>nums = [3,2,1,0,4]<strong><br />输出:</strong>false<strong><br />解释:</strong>无论怎样,总会到达下标为 3 的位置。但该下标的最大跳跃长度是 0 , 所以永远不可能到达最后一个下标。</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>1 <= nums.length <= 3 * 10<sup>4</sup></code></li> <li><code>0 <= nums[i] <= 10<sup>5</sup></code></li></ul>
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
static inline int max(int a, int b)
{
return a > b ? a : b;
}
static bool canJump(int *nums, int numsSize)
{
int i, pos = 0;
for (i = 0; i < numsSize - 1; i++)
{
if (pos < i || pos >= numsSize - 1)
{
break;
}
pos = max(i + nums[i], pos);
}
return pos >= numsSize - 1;
}
int main(int argc, char **argv)
{
int i, count = argc - 1;
int *nums = malloc(count * sizeof(int));
for (i = 0; i < count; i++)
{
nums[i] = atoi(argv[i + 1]);
}
printf("%s\n", canJump(nums, count) ? "true" : "false");
return 0;
}
\ No newline at end of file
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册