提交 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
<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
<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
<p>给你一个字符串 <code>s</code>,由若干单词组成,单词之间用空格隔开。返回字符串中最后一个单词的长度。如果不存在最后一个单词,请返回 0 。</p><p><strong>单词</strong> 是指仅由字母组成、不包含任何空格字符的最大子字符串。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>s = "Hello World"<strong><br />输出:</strong>5</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>s = " "<strong><br />输出:</strong>0</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 <stdio.h>
#include <stdlib.h>
int lengthOfLastWord(char *s)
{
int len = 0;
while (*s != '\0')
{
if (s[-1] == ' ' && s[0] != ' ')
{
len = 1;
}
else if (*s != ' ')
{
len++;
}
s++;
}
return len;
}
int main(int argc, char **argv)
{
if (argc != 2)
{
fprintf(stderr, "Usage: ./test word\n");
exit(-1);
}
printf("%d\n", lengthOfLastWord(argv[1]));
return 0;
}
\ 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
<p>给出集合 <code>[1,2,3,...,n]</code>,其所有元素共有 <code>n!</code> 种排列。</p><p>按大小顺序列出所有排列情况,并一一标记,当 <code>n = 3</code> 时, 所有排列如下:</p><ol> <li><code>"123"</code></li> <li><code>"132"</code></li> <li><code>"213"</code></li> <li><code>"231"</code></li> <li><code>"312"</code></li> <li><code>"321"</code></li></ol><p>给定 <code>n</code> 和 <code>k</code>,返回第 <code>k</code> 个排列。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>n = 3, k = 3<strong><br />输出:</strong>"213"</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>n = 4, k = 9<strong><br />输出:</strong>"2314"</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>n = 3, k = 1<strong><br />输出:</strong>"123"</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>1 <= n <= 9</code></li> <li><code>1 <= k <= n!</code></li></ul>
\ No newline at end of file
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static char *getPermutation(int n, int k)
{
int i;
char *result = malloc(n + 1);
bool *used = malloc(n * sizeof(bool));
memset(used, false, n * sizeof(bool));
int total = 1;
for (i = 1; i <= n; i++)
{
total *= i;
}
k = k - 1;
for (i = 0; i < n; i++)
{
total /= (n - i);
int gid = k / total;
k %= total;
int x = -1;
int count = 0;
while (count <= gid)
{
x = (x + 1) % n;
if (!used[x])
{
count++;
}
}
used[x] = true;
result[i] = x + 1 + '0';
}
result[n] = '\0';
return result;
}
int main(int argc, char **argv)
{
if (argc != 3)
{
fprintf(stderr, "Usage: ./test n, k\n");
exit(-1);
}
printf("%s\n", getPermutation(atoi(argv[1]), atoi(argv[2])));
return 0;
}
\ No newline at end of file
<div class="notranslate">
<p>将一个给定字符串 <code>s</code> 根据给定的行数 <code>numRows</code> ,以从上往下、从左到右进行&nbsp;Z 字形排列。</p>
<p>比如输入字符串为 <code>"PAYPALISHIRING"</code>&nbsp;行数为 <code>3</code> 时,排列如下:</p>
<pre>
P A H N
A P L S I I G
Y I R</pre>
<p>之后,你的输出需要从左往右逐行读取,产生出一个新的字符串,比如:<code>"PAHNAPLSIIGYIR"</code></p>
<p>请你实现这个将字符串进行指定行数变换的函数:</p>
<pre>string convert(string s, int numRows);</pre>
<p>&nbsp;</p>
<p><strong>示例 1:</strong></p>
<pre><strong>输入:</strong>s = "PAYPALISHIRING", numRows = 3
<strong><br />输出:</strong>"PAHNAPLSIIGYIR"
</pre>
<strong>示例 2:</strong>
<pre><strong>输入:</strong>s = "PAYPALISHIRING", numRows = 4
<strong><br />输出:</strong>"PINALSIGYAHRPI"
<strong><br />解释:</strong>
P I N
A L S I G
Y A H R
P I
</pre>
<p><strong>示例 3:</strong></p>
<pre><strong>输入:</strong>s = "A", numRows = 1
<strong><br />输出:</strong>"A"
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= s.length &lt;= 1000</code></li>
<li><code>s</code> 由英文字母(小写和大写)、<code>','</code><code>'.'</code> 组成</li>
<li><code>1 &lt;= numRows &lt;= 1000</code></li>
</ul>
</div>
\ No newline at end of file
class Solution
{
public:
string convert(string s, int numRows)
{
if (numRows == 1)
return s;
int len = s.size();
if (len <= numRows)
return s;
int cycle_len = 2 * numRows - 2;
int full_cycles = len / cycle_len;
int left = len % cycle_len;
string r;
int i;
for (i = 0; i < full_cycles; ++i)
{
r += s[i * cycle_len];
}
if (left)
r += s[i * cycle_len];
for (i = 0; i < numRows - 2; ++i)
{
int j;
for (j = 0; j < full_cycles; ++j)
{
r += s[j * cycle_len + i + 1];
r += s[j * cycle_len + i + 1 + cycle_len - 2 * (i + 1)];
}
if (left)
{
if (j * cycle_len + i + 1 < len)
r += s[j * cycle_len + i + 1];
if (j * cycle_len + i + 1 + cycle_len - 2 * (i + 1) < len)
r += s[j * cycle_len + i + 1 + cycle_len - 2 * (i + 1)];
}
}
for (i = 0; i < full_cycles; ++i)
r += s[i * cycle_len + numRows - 1];
if (left >= numRows)
r += s[i * cycle_len + numRows - 1];
return r;
}
};
\ No newline at end of file
<p>给你一个链表的头节点 <code>head</code> ,旋转链表,将链表每个节点向右移动 <code>k</code><em> </em>个位置。</p><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0061.Rotate%20List/images/rotate1.jpg" style="width: 600px; height: 254px;" /><pre><strong>输入:</strong>head = [1,2,3,4,5], k = 2<strong><br />输出:</strong>[4,5,1,2,3]</pre><p><strong>示例 2:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0061.Rotate%20List/images/roate2.jpg" style="width: 472px; height: 542px;" /><pre><strong>输入:</strong>head = [0,1,2], k = 4<strong><br />输出:</strong>[2,0,1]</pre><p> </p><p><strong>提示:</strong></p><ul> <li>链表中节点的数目在范围 <code>[0, 500]</code></li> <li><code>-100 <= Node.val <= 100</code></li> <li><code>0 <= k <= 2 * 10<sup>9</sup></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 *rotateRight(ListNode *head, int k)
{
if (head == nullptr)
{
return head;
}
int len = 0;
ListNode dummy;
dummy.next = head;
ListNode *tail = &dummy;
while (tail->next != nullptr)
{
len++;
tail = tail->next;
}
ListNode *prev = &dummy;
ListNode *p = head;
k = k % len;
for (int i = 0; i < len - k; i++)
{
prev = p;
p = p->next;
}
if (p != nullptr)
{
prev->next = tail->next;
tail->next = head;
head = p;
}
return head;
}
};
\ 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
<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
<p>给定一个包含非负整数的 <code><em>m</em> x <em>n</em></code> 网格 <code>grid</code> ,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小。</p><p><strong>说明:</strong>每次只能向下或者向右移动一步。</p><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0064.Minimum%20Path%20Sum/images/minpath.jpg" style="width: 242px; height: 242px;" /><pre><strong>输入:</strong>grid = [[1,3,1],[1,5,1],[4,2,1]]<strong><br />输出:</strong>7<strong><br />解释:</strong>因为路径 1→3→1→1→1 的总和最小。</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>grid = [[1,2,3],[4,5,6]]<strong><br />输出:</strong>12</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 <= m, n <= 200</code></li> <li><code>0 <= grid[i][j] <= 100</code></li></ul>
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static inline int min(int a, int b)
{
return a < b ? a : b;
}
int minPathSum(int **grid, int gridRowSize, int gridColSize)
{
int i, j;
int **dp = malloc(gridRowSize * sizeof(int *));
for (i = 0; i < gridRowSize; i++)
{
dp[i] = malloc(gridColSize * sizeof(int));
}
dp[0][0] = grid[0][0];
int sum = dp[0][0];
for (i = 1; i < gridRowSize; i++)
{
sum += grid[i][0];
dp[i][0] = sum;
}
sum = dp[0][0];
for (i = 1; i < gridColSize; i++)
{
sum += grid[0][i];
dp[0][i] = sum;
}
for (i = 1; i < gridRowSize; i++)
{
for (j = 1; j < gridColSize; j++)
{
dp[i][j] = grid[i][j] + min(dp[i - 1][j], dp[i][j - 1]);
}
}
return dp[gridRowSize - 1][gridColSize - 1];
}
int main(int argc, char **argv)
{
int i, j;
int row = argc - 1;
int col = strlen(argv[1]);
int **grid = malloc(row * sizeof(int *));
for (i = 0; i < row; i++)
{
grid[i] = malloc(col * sizeof(int));
for (j = 0; j < col; j++)
{
grid[i][j] = argv[i + 1][j] - '0';
printf("%d ", grid[i][j]);
}
printf("\n");
}
printf("%d\n", minPathSum(grid, row, col));
return 0;
}
\ No newline at end of file
<p><strong>有效数字</strong>(按顺序)可以分成以下几个部分:</p><ol> <li>一个 <strong>小数</strong> 或者 <strong>整数</strong></li> <li>(可选)一个 <code>'e'</code><code>'E'</code> ,后面跟着一个 <strong>整数</strong></li></ol><p><strong>小数</strong>(按顺序)可以分成以下几个部分:</p><ol> <li>(可选)一个符号字符(<code>'+'</code><code>'-'</code></li> <li>下述格式之一: <ol> <li>至少一位数字,后面跟着一个点 <code>'.'</code></li> <li>至少一位数字,后面跟着一个点 <code>'.'</code> ,后面再跟着至少一位数字</li> <li>一个点 <code>'.'</code> ,后面跟着至少一位数字</li> </ol> </li></ol><p><strong>整数</strong>(按顺序)可以分成以下几个部分:</p><ol> <li>(可选)一个符号字符(<code>'+'</code><code>'-'</code></li> <li>至少一位数字</li></ol><p>部分有效数字列举如下:</p><ul> <li><code>["2", "0089", "-0.1", "+3.14", "4.", "-.9", "2e10", "-90E3", "3e+7", "+6e-1", "53.5e93", "-123.456e789"]</code></li></ul><p>部分无效数字列举如下:</p><ul> <li><code>["abc", "1a", "1e", "e3", "99e2.5", "--6", "-+3", "95a54e53"]</code></li></ul><p>给你一个字符串 <code>s</code> ,如果 <code>s</code> 是一个 <strong>有效数字</strong> ,请返回 <code>true</code></p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>s = "0"<strong><br />输出:</strong>true</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>s = "e"<strong><br />输出:</strong>false</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>s = "."<strong><br />输出:</strong>false</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>s = ".1"<strong><br />输出:</strong>true</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>1 <= s.length <= 20</code></li> <li><code>s</code> 仅含英文字母(大写和小写),数字(<code>0-9</code>),加号 <code>'+'</code> ,减号 <code>'-'</code> ,或者点 <code>'.'</code></li></ul>
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <ctype.h>
static bool isNumber(const char *s)
{
while (*s == ' ')
++s;
bool if_find_num = false;
if (*s == '-' || *s == '+')
++s;
while (isdigit(*s))
{
if_find_num = true;
++s;
}
if (*s == '.')
++s;
while (isdigit(*s))
{
if_find_num = true;
++s;
}
if (if_find_num == true && *s == 'e')
{
++s;
if (*s == '+' || *s == '-')
++s;
if_find_num = false;
while (isdigit(*s))
{
if_find_num = true;
++s;
}
}
while (*s == ' ')
++s;
return *s == '\0' && if_find_num == true;
}
int main(int argc, char **argv)
{
if (argc != 2)
{
fprintf(stderr, "Usage: ./test number\n");
exit(-1);
}
printf("%s\n", isNumber(argv[1]) ? "true" : "false");
return 0;
}
\ No newline at end of file
<p>给定一个由 <strong>整数 </strong>组成的<strong> 非空</strong> 数组所表示的非负整数,在该数的基础上加一。</p><p>最高位数字存放在数组的首位, 数组中每个元素只存储<strong>单个</strong>数字。</p><p>你可以假设除了整数 0 之外,这个整数不会以零开头。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>digits = [1,2,3]<strong><br />输出:</strong>[1,2,4]<strong><br />解释:</strong>输入数组表示数字 123。</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>digits = [4,3,2,1]<strong><br />输出:</strong>[4,3,2,2]<strong><br />解释:</strong>输入数组表示数字 4321。</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>digits = [0]<strong><br />输出:</strong>[1]</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>1 <= digits.length <= 100</code></li> <li><code>0 <= digits[i] <= 9</code></li></ul>
\ No newline at end of file
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
vector<int> plusOne(vector<int> &digits)
{
int carry = 1;
vector<int> res;
for (int i = digits.size() - 1; i >= 0; i--)
{
int d = digits[i] + carry;
res.push_back(d % 10);
carry = d / 10;
}
if (carry > 0)
{
res.push_back(carry);
}
reverse(res.begin(), res.end());
return res;
}
};
\ No newline at end of file
<p>给你两个二进制字符串,返回它们的和(用二进制表示)。</p><p>输入为 <strong>非空 </strong>字符串且只包含数字&nbsp;<code>1</code>&nbsp;&nbsp;<code>0</code></p><p>&nbsp;</p><p><strong>示例&nbsp;1:</strong></p><pre><strong>输入:</strong> a = &quot;11&quot;, b = &quot;1&quot;<strong><br />输出:</strong> &quot;100&quot;</pre><p><strong>示例&nbsp;2:</strong></p><pre><strong>输入:</strong> a = &quot;1010&quot;, b = &quot;1011&quot;<strong><br />输出:</strong> &quot;10101&quot;</pre><p>&nbsp;</p><p><strong>提示:</strong></p><ul> <li>每个字符串仅由字符 <code>&#39;0&#39;</code><code>&#39;1&#39;</code> 组成。</li> <li><code>1 &lt;= a.length, b.length &lt;= 10^4</code></li> <li>字符串如果不是 <code>&quot;0&quot;</code> ,就都不含前导零。</li></ul>
\ No newline at end of file
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
string addBinary(string a, string b)
{
string res;
int carry = 0;
int i = a.length() - 1;
int j = b.length() - 1;
for (; i >= 0 && j >= 0; i--, j--)
{
if (a[i] == '1' && b[j] == '1')
{
if (carry > 0)
{
res.push_back('1');
}
else
{
res.push_back('0');
}
carry = 1;
}
else if (a[i] == '0' && b[j] == '0')
{
if (carry > 0)
{
res.push_back('1');
}
else
{
res.push_back('0');
}
carry = 0;
}
else
{
if (carry > 0)
{
res.push_back('0');
carry = 1;
}
else
{
res.push_back('1');
carry = 0;
}
}
}
while (i >= 0)
{
if (a[i--] == '1')
{
if (carry > 0)
{
res.push_back('0');
carry = 1;
}
else
{
res.push_back('1');
carry = 0;
}
}
else
{
res.push_back(carry + '0');
carry = 0;
}
}
while (j >= 0)
{
if (b[j--] == '1')
{
if (carry > 0)
{
res.push_back('0');
carry = 1;
}
else
{
res.push_back('1');
carry = 0;
}
}
else
{
res.push_back(carry + '0');
carry = 0;
}
}
if (carry > 0)
{
res.push_back('1');
}
reverse(res.begin(), res.end());
return res;
}
};
\ No newline at end of file
<div class="notranslate">
<p>给定一个单词数组和一个长度&nbsp;<em>maxWidth</em>,重新排版单词,使其成为每行恰好有&nbsp;<em>maxWidth</em>&nbsp;个字符,且左右两端对齐的文本。</p>
<p>你应该使用“贪心算法”来放置给定的单词;也就是说,尽可能多地往每行中放置单词。必要时可用空格&nbsp;<code>' '</code>&nbsp;填充,使得每行恰好有 <em>maxWidth</em>&nbsp;个字符。
</p>
<p>要求尽可能均匀分配单词间的空格数量。如果某一行单词间的空格不能均匀分配,则左侧放置的空格数要多于右侧的空格数。</p>
<p>文本的最后一行应为左对齐,且单词之间不插入<strong>额外的</strong>空格。</p>
<p><strong>说明:</strong></p>
<ul>
<li>单词是指由非空格字符组成的字符序列。</li>
<li>每个单词的长度大于 0,小于等于&nbsp;<em>maxWidth</em></li>
<li>输入单词数组 <code>words</code>&nbsp;至少包含一个单词。</li>
</ul>
<p><strong>示例:</strong></p>
<pre><strong>输入:</strong>
words = ["This", "is", "an", "example", "of", "text", "justification."]
maxWidth = 16
<strong><br />输出:</strong>
[
&nbsp; &nbsp;"This &nbsp; &nbsp;is &nbsp; &nbsp;an",
&nbsp; &nbsp;"example &nbsp;of text",
&nbsp; &nbsp;"justification. &nbsp;"
]
</pre>
<p><strong>示例&nbsp;2:</strong></p>
<pre><strong>输入:</strong>
words = ["What","must","be","acknowledgment","shall","be"]
maxWidth = 16
<strong><br />输出:</strong>
[
&nbsp; "What &nbsp; must &nbsp; be",
&nbsp; "acknowledgment &nbsp;",
&nbsp; "shall be &nbsp; &nbsp; &nbsp; &nbsp;"
]
<strong><br />解释: </strong>注意最后一行的格式应为 "shall be " 而不是 "shall be"
因为最后一行应为左对齐,而不是左右两端对齐,第二行同样为左对齐,这是因为这行只包含一个单词。
</pre>
<p><strong>示例&nbsp;3:</strong></p>
<pre><strong>输入:</strong>
words = ["Science","is","what","we","understand","well","enough","to","explain",
&nbsp; "to","a","computer.","Art","is","everything","else","we","do"]
maxWidth = 20
<strong><br />输出:</strong>
[
&nbsp; "Science &nbsp;is &nbsp;what we",
"understand &nbsp; &nbsp; &nbsp;well",
&nbsp; "enough to explain to",
&nbsp; "a &nbsp;computer. &nbsp;Art is",
&nbsp; "everything &nbsp;else &nbsp;we",
&nbsp; "do &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"
]
</pre>
</div>
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static void line_fill(char *line, int len, char **words, int *word_lens, int max_size,
int even_spaces, int remain_spaces, int start, int end)
{
int i, j;
char *p = line;
for (i = start; i < end; i++)
{
memcpy(p, words[i], word_lens[i]);
p += word_lens[i];
if (i < end - 1)
{
for (j = 0; j < even_spaces; j++)
{
*p++ = ' ';
}
if (remain_spaces > 0)
{
*p++ = ' ';
remain_spaces--;
}
}
}
while (p - line < max_size)
{
*p++ = ' ';
}
*p++ = '\0';
}
static char **fullJustify(char **words, int wordsSize, int maxWidth, int *returnSize)
{
int i, j, k, cap = 100, count = 0;
char **lines = malloc(cap * sizeof(char *));
char *buf = malloc(cap * (maxWidth + 1));
for (i = 0; i < cap; i++)
{
lines[i] = buf + i * (maxWidth + 1);
}
int *word_lens = malloc(wordsSize * sizeof(int));
for (i = 0; i < wordsSize; i++)
{
word_lens[i] = strlen(words[i]);
}
int wc = 0;
int len = 0;
int start = 0;
int chars = 0;
for (i = 0, j = 0; i < wordsSize; i++)
{
if (len + word_lens[i] > maxWidth)
{
int even_spaces = wc == 1 ? 0 : (maxWidth - chars) / (wc - 1);
int remain_spaces = wc == 1 ? 0 : (maxWidth - chars) % (wc - 1);
line_fill(lines[count], len, words, word_lens, maxWidth, even_spaces, remain_spaces, start, i);
count++;
wc = 1;
len = word_lens[i] + 1;
chars = word_lens[i];
start = i;
}
else if (len + word_lens[i] == maxWidth)
{
chars += word_lens[i];
int even_spaces = wc == 0 ? 0 : (maxWidth - chars) / wc;
int remain_spaces = wc == 0 ? 0 : (maxWidth - chars) % wc;
line_fill(lines[count], len, words, word_lens, maxWidth, even_spaces, remain_spaces, start, i + 1);
count++;
wc = 0;
len = 0;
chars = 0;
start = i + 1;
}
else
{
chars += word_lens[i];
len += word_lens[i] + 1;
wc++;
}
}
if (wc > 0)
{
char *p = lines[count];
for (i = start; i < start + wc; i++)
{
memcpy(p, words[i], word_lens[i]);
p += word_lens[i];
if (i < start + wc - 1)
{
*p++ = ' ';
}
}
while (p - lines[count] < maxWidth)
{
*p++ = ' ';
}
*p++ = '\0';
count++;
}
*returnSize = count;
return lines;
}
int main(int argc, char **argv)
{
if (argc <= 2)
{
fprintf(stderr, "Usage: ./test maxsize words...\n");
exit(-1);
}
int i, count;
char **lines = fullJustify(argv + 2, argc - 2, atoi(argv[1]), &count);
for (i = 0; i < count; i++)
{
printf("%s\n", lines[i]);
}
return 0;
}
\ No newline at end of file
<p>实现&nbsp;<code>int sqrt(int x)</code>&nbsp;函数。</p>
<p>计算并返回&nbsp;<em>x</em>&nbsp;的平方根,其中&nbsp;<em>x </em>是非负整数。</p>
<p>由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去。</p>
<p><strong>示例 1:</strong></p>
<pre><strong>输入:</strong> 4<strong><br />输出:</strong> 2</pre>
<p><strong>示例 2:</strong></p>
<pre><strong>输入:</strong> 8<strong><br />输出:</strong> 2<strong><br />说明:</strong> 8 的平方根是 2.82842..., 由于返回类型是整数,小数部分将被舍去。</pre>
\ No newline at end of file
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
int mySqrt(int x)
{
if (x == 0)
{
return 0;
}
unsigned int lo = 1, hi = x;
unsigned int mid = (lo + hi) / 2;
for (;;)
{
if (mid > x / mid)
{
hi = mid;
}
else
{
if (mid + 1 > x / (mid + 1))
{
break;
}
else
{
lo = mid;
}
}
mid = (lo + hi) / 2;
}
return mid;
}
};
\ No newline at end of file
<p>假设你正在爬楼梯。需要 <em>n</em>&nbsp;阶你才能到达楼顶。</p><p>每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢?</p><p><strong>注意:</strong>给定 <em>n</em> 是一个正整数。</p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong> 2<strong><br />输出:</strong> 2<strong><br />解释:</strong> 有两种方法可以爬到楼顶。1. 1 阶 + 1 阶2. 2 阶</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong> 3<strong><br />输出:</strong> 3<strong><br />解释:</strong> 有三种方法可以爬到楼顶。1. 1 阶 + 1 阶 + 1 阶2. 1 阶 + 2 阶3. 2 阶 + 1 阶</pre>
\ No newline at end of file
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
int climbStairs(int n)
{
int a = 1;
int b = 2;
int c = 0;
for (int i = 3; i <= n; i++)
{
c = a + b;
a = b;
b = c;
}
return n == 1 ? a : (n == 2 ? b : c);
}
};
\ No newline at end of file
<p>给你一个 32 位的有符号整数 <code>x</code> ,返回将 <code>x</code> 中的数字部分反转后的结果。</p><p>如果反转后整数超过 32 位的有符号整数的范围 <code>[−2<sup>31</sup>,  2<sup>31 </sup>− 1]</code> ,就返回 0。</p><strong>假设环境不允许存储 64 位整数(有符号或无符号)。</strong><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>x = 123<strong><br />输出:</strong>321</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>x = -123<strong><br />输出:</strong>-321</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>x = 120<strong><br />输出:</strong>21</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>x = 0<strong><br />输出:</strong>0</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>-2<sup>31</sup> <= x <= 2<sup>31</sup> - 1</code></li></ul>
\ No newline at end of file
int reverse(int x)
{
long long int r = 0;
while (x)
{
r = r * 10 + (x % 10);
x /= 10;
}
if (r > 2147483647)
return 0;
if (r < -2147483648)
return 0;
return (int)r;
}
\ No newline at end of file
<p>给你一个字符串 <code>path</code> ,表示指向某一文件或目录的 Unix 风格 <strong>绝对路径 </strong>(以 <code>'/'</code> 开头),请你将其转化为更加简洁的规范路径。</p><p class="MachineTrans-lang-zh-CN">在 Unix 风格的文件系统中,一个点(<code>.</code>)表示当前目录本身;此外,两个点 (<code>..</code>) 表示将目录切换到上一级(指向父目录);两者都可以是复杂相对路径的组成部分。任意多个连续的斜杠(即,<code>'//'</code>)都被视为单个斜杠 <code>'/'</code> 。 对于此问题,任何其他格式的点(例如,<code>'...'</code>)均被视为文件/目录名称。</p><p>请注意,返回的 <strong>规范路径</strong> 必须遵循下述格式:</p><ul> <li>始终以斜杠 <code>'/'</code> 开头。</li> <li>两个目录名之间必须只有一个斜杠 <code>'/'</code></li> <li>最后一个目录名(如果存在)<strong>不能 </strong><code>'/'</code> 结尾。</li> <li>此外,路径仅包含从根目录到目标文件或目录的路径上的目录(即,不含 <code>'.'</code><code>'..'</code>)。</li></ul><p>返回简化后得到的 <strong>规范路径</strong></p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>path = "/home/"<strong><br />输出:</strong>"/home"<strong><br />解释:</strong>注意,最后一个目录名后面没有斜杠。 </pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>path = "/../"<strong><br />输出:</strong>"/"<strong><br />解释:</strong>从根目录向上一级是不可行的,因为根目录是你可以到达的最高级。</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>path = "/home//foo/"<strong><br />输出:</strong>"/home/foo"<strong><br />解释:</strong>在规范路径中,多个连续斜杠需要用一个斜杠替换。</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>path = "/a/./b/../../c/"<strong><br />输出:</strong>"/c"</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>1 <= path.length <= 3000</code></li> <li><code>path</code> 由英文字母,数字,<code>'.'</code><code>'/'</code><code>'_'</code> 组成。</li> <li><code>path</code> 是一个有效的 Unix 风格绝对路径。</li></ul>
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static char *simplifyPath(char *path)
{
int len = strlen(path);
if (len == 0)
{
return path;
}
char *p = path;
int *indexes = malloc(len * sizeof(int));
int depth = 0;
int name_start = 1;
while (*p != '\0')
{
if (*p == '/')
{
if (p > path && *(p - 1) != '/' && *(p - 1) != '.')
{
name_start = 1;
}
}
else if (*p == '.')
{
if (*(p + 1) == '\0' || *(p + 1) == '/')
{
p += 1;
}
else if (*(p + 1) == '.' && (*(p + 2) == '\0' || *(p + 2) == '/'))
{
if (depth > 0)
{
depth--;
name_start = 1;
}
p += 2;
}
else
{
indexes[depth++] = p - path;
while (*p != '/' && *p != '\0')
{
p++;
}
}
if (*p == '\0')
{
break;
}
}
else
{
if (name_start && depth >= 0)
{
indexes[depth++] = p - path;
name_start = 0;
}
}
p++;
}
int i;
char *result = malloc(len + 1);
char *q = result;
if (depth <= 0)
{
*q++ = '/';
}
else
{
for (i = 0; i < depth; i++)
{
p = path + indexes[i];
*q++ = '/';
while (*p != '/')
{
*q++ = *p++;
}
}
}
*q = '\0';
return result;
}
int main(int argc, char **argv)
{
if (argc != 2)
{
fprintf(stderr, "Usage: ./test path\n");
exit(-1);
}
printf("%s\n", simplifyPath(argv[1]));
return 0;
}
\ No newline at end of file
<p>给你两个单词 <code>word1</code> 和 <code>word2</code>,请你计算出将 <code>word1</code> 转换成 <code>word2</code><em> </em>所使用的最少操作数 。</p><p>你可以对一个单词进行如下三种操作:</p><ul> <li>插入一个字符</li> <li>删除一个字符</li> <li>替换一个字符</li></ul><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>word1 = "horse", word2 = "ros"<strong><br />输出:</strong>3<strong><br />解释:</strong>horse -> rorse (将 'h' 替换为 'r')rorse -> rose (删除 'r')rose -> ros (删除 'e')</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>word1 = "intention", word2 = "execution"<strong><br />输出:</strong>5<strong><br />解释:</strong>intention -> inention (删除 't')inention -> enention (将 'i' 替换为 'e')enention -> exention (将 'n' 替换为 'x')exention -> exection (将 'n' 替换为 'c')exection -> execution (插入 'u')</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>0 <= word1.length, word2.length <= 500</code></li> <li><code>word1</code><code>word2</code> 由小写英文字母组成</li></ul>
\ No newline at end of file
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
int minDistance(string word1, string word2)
{
int l1 = word1.length();
int l2 = word2.length();
vector<int> dp(l2 + 1);
for (int i = 0; i <= l2; i++)
{
dp[i] = i;
}
int up = 0;
for (int i = 1; i <= l1; i++)
{
int left_up = dp[0];
dp[0] = i;
for (int j = 1; j <= l2; j++)
{
up = dp[j];
if (word1[i - 1] == word2[j - 1])
{
dp[j] = left_up;
}
else
{
dp[j] = 1 + min(left_up, min(up, dp[j - 1]));
}
left_up = up;
}
}
return dp[l2];
}
};
\ No newline at end of file
<p>给定一个 <code><em>m</em> x <em>n</em></code> 的矩阵,如果一个元素为 <strong>0 </strong>,则将其所在行和列的所有元素都设为 <strong>0</strong> 。请使用 <strong><a href="http://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95" target="_blank">原地</a></strong> 算法<strong></strong></p><p><strong>进阶:</strong></p><ul> <li>一个直观的解决方案是使用  <code>O(<em>m</em><em>n</em>)</code> 的额外空间,但这并不是一个好的解决方案。</li> <li>一个简单的改进方案是使用 <code>O(<em>m</em> + <em>n</em>)</code> 的额外空间,但这仍然不是最好的解决方案。</li> <li>你能想出一个仅使用常量空间的解决方案吗?</li></ul><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0073.Set%20Matrix%20Zeroes/images/mat1.jpg" style="width: 450px; height: 169px;" /><pre><strong>输入:</strong>matrix = [[1,1,1],[1,0,1],[1,1,1]]<strong><br />输出:</strong>[[1,0,1],[0,0,0],[1,0,1]]</pre><p><strong>示例 2:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0073.Set%20Matrix%20Zeroes/images/mat2.jpg" style="width: 450px; height: 137px;" /><pre><strong>输入:</strong>matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]]<strong><br />输出:</strong>[[0,0,0,0],[0,4,5,0],[0,3,1,0]]</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[0].length</code></li> <li><code>1 <= m, n <= 200</code></li> <li><code>-2<sup>31</sup> <= matrix[i][j] <= 2<sup>31</sup> - 1</code></li></ul>
\ No newline at end of file
#include <bits/stdc++.h>
using namespace std;
public:
void setZeroes(vector<vector<int>> &matrix)
{
bool bRow = false, bCol = false;
for (int row = 0; row < matrix.size(); row++)
{
for (int col = 0; col < matrix[row].size(); col++)
{
if (matrix[row][col] == 0)
{
if (row == 0)
{
bRow = true;
}
if (col == 0)
{
bCol = true;
}
matrix[0][col] = matrix[row][0] = 0;
}
}
}
for (int row = 1; row < matrix.size(); row++)
{
for (int col = 1; col < matrix[row].size(); col++)
{
if (matrix[0][col] == 0 || matrix[row][0] == 0)
{
matrix[row][col] = 0;
}
}
}
if (bRow)
{
for (auto &m : matrix[0])
{
m = 0;
}
}
if (bCol)
{
for (int row = 0; row < matrix.size(); row++)
{
matrix[row][0] = 0;
}
}
}
}
;
\ No newline at end of file
<p>编写一个高效的算法来判断 <code>m x n</code> 矩阵中,是否存在一个目标值。该矩阵具有如下特性:</p><ul> <li>每行中的整数从左到右按升序排列。</li> <li>每行的第一个整数大于前一行的最后一个整数。</li></ul><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0074.Search%20a%202D%20Matrix/images/mat.jpg" style="width: 322px; height: 242px;" /><pre><strong>输入:</strong>matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3<strong><br />输出:</strong>true</pre><p><strong>示例 2:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0074.Search%20a%202D%20Matrix/images/mat2.jpg" style="width: 322px; height: 242px;" /><pre><strong>输入:</strong>matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13<strong><br />输出:</strong>false</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[i].length</code></li> <li><code>1 <= m, n <= 100</code></li> <li><code>-10<sup>4</sup> <= matrix[i][j], target <= 10<sup>4</sup></code></li></ul>
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
static int binary_search(int *nums, int len, int target)
{
int low = -1;
int high = len;
while (low + 1 < high)
{
int mid = low + (high - low) / 2;
if (target > nums[mid])
{
low = mid;
}
else
{
high = mid;
}
}
if (high == len || nums[high] != target)
{
return -high - 1;
}
else
{
return high;
}
}
static bool searchMatrix(int **matrix, int matrixRowSize, int matrixColSize, int target)
{
if (matrixRowSize == 0 || matrixColSize == 0)
{
return false;
}
if (target < matrix[0][0] || target > matrix[matrixRowSize - 1][matrixColSize - 1])
{
return false;
}
int row = 0;
int *nums = NULL;
if (matrixRowSize > 0)
{
nums = malloc(matrixRowSize * sizeof(int));
for (row = 0; row < matrixRowSize; row++)
{
nums[row] = matrix[row][0];
}
row = binary_search(nums, matrixRowSize, target);
if (row >= 0)
{
return true;
}
else
{
row = -row - 1;
if (row == 0)
{
return false;
}
else
{
row--;
}
}
}
int col = binary_search(matrix[row], matrixColSize, target);
return col >= 0;
}
int main(int argc, char **argv)
{
int row = 3;
int col = 4;
int **mat = malloc(row * sizeof(int *));
mat[0] = malloc(col * sizeof(int));
mat[0][0] = 1;
mat[0][1] = 3;
mat[0][2] = 5;
mat[0][3] = 7;
mat[1] = malloc(col * sizeof(int));
mat[1][0] = 10;
mat[1][1] = 11;
mat[1][2] = 16;
mat[1][3] = 20;
mat[2] = malloc(col * sizeof(int));
mat[2][0] = 23;
mat[2][1] = 30;
mat[2][2] = 34;
mat[2][3] = 50;
printf("%s\n", searchMatrix(mat, row, col, atoi(argv[1])) ? "true" : "false");
return 0;
}
\ No newline at end of file
<p>给定一个包含红色、白色和蓝色,一共 <code>n</code><em> </em>个元素的数组,<strong><a href="https://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95" target="_blank">原地</a></strong>对它们进行排序,使得相同颜色的元素相邻,并按照红色、白色、蓝色顺序排列。</p><p>此题中,我们使用整数 <code>0</code>、 <code>1</code><code>2</code> 分别表示红色、白色和蓝色。</p><ul></ul><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>nums = [2,0,2,1,1,0]<strong><br />输出:</strong>[0,0,1,1,2,2]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>nums = [2,0,1]<strong><br />输出:</strong>[0,1,2]</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>nums = [0]<strong><br />输出:</strong>[0]</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>nums = [1]<strong><br />输出:</strong>[1]</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>n == nums.length</code></li> <li><code>1 <= n <= 300</code></li> <li><code>nums[i]</code><code>0</code><code>1</code><code>2</code></li></ul><p> </p><p><strong>进阶:</strong></p><ul> <li>你可以不使用代码库中的排序函数来解决这道题吗?</li> <li>你能想出一个仅使用常数空间的一趟扫描算法吗?</li></ul>
\ No newline at end of file
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
void sortColors(vector<int> &nums)
{
int i = 0, j = nums.size() - 1;
while (i < j)
{
if (nums[i] == 0)
{
i++;
continue;
}
if (nums[j] != 0)
{
j--;
continue;
}
swap(nums[i], nums[j]);
}
j = nums.size() - 1;
while (i < j)
{
if (nums[i] == 1)
{
i++;
continue;
}
if (nums[j] != 1)
{
j--;
continue;
}
swap(nums[i], nums[j]);
}
}
};
\ No newline at end of file
<p>给你一个字符串 <code>s</code> 、一个字符串 <code>t</code> 。返回 <code>s</code> 中涵盖 <code>t</code> 所有字符的最小子串。如果 <code>s</code> 中不存在涵盖 <code>t</code> 所有字符的子串,则返回空字符串 <code>""</code></p><p><strong>注意:</strong>如果 <code>s</code> 中存在这样的子串,我们保证它是唯一的答案。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>s = "ADOBECODEBANC", t = "ABC"<strong><br />输出:</strong>"BANC"</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>s = "a", t = "a"<strong><br />输出:</strong>"a"</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>1 <= s.length, t.length <= 10<sup>5</sup></code></li> <li><code>s</code><code>t</code> 由英文字母组成</li></ul><p> </p><strong>进阶:</strong>你能设计一个在 <code>o(n)</code> 时间内解决此问题的算法吗?
\ No newline at end of file
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
string minWindow(string s, string t)
{
vector<int> count(128);
for (char c : t)
{
count[c]++;
}
int l = 0, r = 0;
int need_to_meet = t.length();
int start, min_len = INT_MAX;
while (r < s.length())
{
if (--count[s[r++]] >= 0)
{
need_to_meet--;
}
while (need_to_meet == 0)
{
if (r - l < min_len)
{
start = l;
min_len = r - l;
}
if (++count[s[l++]] > 0)
{
need_to_meet++;
}
}
}
return min_len == INT_MAX ? "" : s.substr(start, min_len);
}
};
\ No newline at end of file
<p>给定两个整数 <em>n</em><em>k</em>,返回 1 ... <em>n </em>中所有可能的 <em>k</em> 个数的组合。</p>
<p><strong>示例:</strong></p>
<pre><strong>输入:</strong>&nbsp;n = 4, k = 2<strong><br />输出:</strong>[[2,4],[3,4],[2,3],[1,2],[1,3],[1,4],]</pre>
\ No newline at end of file
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
vector<vector<int>> combine(int n, int k)
{
vector<vector<int>> res;
dfs(n, k, 1, res);
return res;
}
private:
vector<int> stack;
void dfs(int n, int k, int start, vector<vector<int>> &res)
{
if (stack.size() == k)
{
res.push_back(stack);
}
else
{
for (int i = start; i <= n; i++)
{
stack.push_back(i);
dfs(n, k, i + 1, res);
stack.pop_back();
}
}
}
};
\ No newline at end of file
<p>给你一个整数数组 <code>nums</code> ,数组中的元素 <strong>互不相同</strong> 。返回该数组所有可能的子集(幂集)。</p><p>解集 <strong>不能</strong> 包含重复的子集。你可以按 <strong>任意顺序</strong> 返回解集。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>nums = [1,2,3]<strong><br />输出:</strong>[[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>nums = [0]<strong><br />输出:</strong>[[],[0]]</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>1 <= nums.length <= 10</code></li> <li><code>-10 <= nums[i] <= 10</code></li> <li><code>nums</code> 中的所有元素 <strong>互不相同</strong></li></ul>
\ No newline at end of file
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
vector<vector<int>> subsets(vector<int> &nums)
{
vector<vector<int>> res;
dfs(nums, 0, res);
return res;
}
private:
vector<int> stack;
void dfs(vector<int> &nums, int start, vector<vector<int>> &res)
{
res.push_back(stack);
for (int i = start; i < nums.size(); i++)
{
stack.push_back(nums[i]);
dfs(nums, i + 1, res);
stack.pop_back();
}
}
};
\ No newline at end of file
<p>给定一个 <code>m x n</code> 二维字符网格 <code>board</code> 和一个字符串单词 <code>word</code> 。如果 <code>word</code> 存在于网格中,返回 <code>true</code> ;否则,返回 <code>false</code></p><p>单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。</p><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0079.Word%20Search/images/word2.jpg" style="width: 322px; height: 242px;" /><pre><strong>输入:</strong>board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"<strong><br />输出:</strong>true</pre><p><strong>示例 2:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0079.Word%20Search/images/word-1.jpg" style="width: 322px; height: 242px;" /><pre><strong>输入:</strong>board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "SEE"<strong><br />输出:</strong>true</pre><p><strong>示例 3:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0079.Word%20Search/images/word3.jpg" style="width: 322px; height: 242px;" /><pre><strong>输入:</strong>board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCB"<strong><br />输出:</strong>false</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>m == board.length</code></li> <li><code>n = board[i].length</code></li> <li><code>1 <= m, n <= 6</code></li> <li><code>1 <= word.length <= 15</code></li> <li><code>board</code><code>word</code> 仅由大小写英文字母组成</li></ul><p> </p><p><strong>进阶:</strong>你可以使用搜索剪枝的技术来优化解决方案,使其在 <code>board</code> 更大的情况下可以更快解决问题?</p>
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
static bool dfs(char *word, char **board, bool *used,
int row, int col, int row_size, int col_size)
{
if (board[row][col] != *word)
{
return false;
}
used[row * col_size + col] = true;
if (*(word + 1) == '\0')
{
return true;
}
bool result = false;
if (row > 0 && !used[(row - 1) * col_size + col])
{
result = dfs(word + 1, board, used, row - 1, col, row_size, col_size);
}
if (!result && row < row_size - 1 && !used[(row + 1) * col_size + col])
{
result = dfs(word + 1, board, used, row + 1, col, row_size, col_size);
}
if (!result && col > 0 && !used[row * col_size + col - 1])
{
result = dfs(word + 1, board, used, row, col - 1, row_size, col_size);
}
if (!result && col < col_size - 1 && !used[row * col_size + col + 1])
{
result = dfs(word + 1, board, used, row, col + 1, row_size, col_size);
}
used[row * col_size + col] = false;
return result;
}
static bool exist(char **board, int boardRowSize, int boardColSize, char *word)
{
int i, j;
int len = strlen(word);
if (len > boardRowSize * boardColSize)
{
return false;
}
bool *used = malloc(boardRowSize * boardColSize);
for (i = 0; i < boardRowSize; i++)
{
for (j = 0; j < boardColSize; j++)
{
memset(used, false, boardRowSize * boardColSize);
if (dfs(word, board, used, i, j, boardRowSize, boardColSize))
{
return true;
}
}
}
return false;
}
int main(int argc, char **argv)
{
if (argc < 3)
{
fprintf(stderr, "Usage: ./test word row1 row2...\n");
exit(-1);
}
printf("%s\n", exist(argv + 2, argc - 2, strlen(argv[2]), argv[1]) ? "true" : "false");
return 0;
}
\ No newline at end of file
<p>给你一个有序数组 <code>nums</code> ,请你<strong><a href="http://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95"
target="_blank"> 原地</a></strong> 删除重复出现的元素,使每个元素 <strong>最多出现两次</strong> ,返回删除后数组的新长度。</p>
<p>不要使用额外的数组空间,你必须在 <strong><a href="https://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95"
target="_blank">原地 </a>修改输入数组 </strong>并在使用 O(1) 额外空间的条件下完成。</p>
<p> </p>
<p><strong>说明:</strong></p>
<p>为什么返回数值是整数,但输出的答案是数组呢?</p>
<p>请注意,输入数组是以<strong>「引用」</strong>方式传递的,这意味着在函数里修改输入数组对于调用者是可见的。</p>
<p>你可以想象内部操作如下:</p>
<pre>
// <strong>nums</strong> 是以“引用”方式传递的。也就是说,不对实参做任何拷贝
int len = removeDuplicates(nums);// 在函数里修改输入数组对于调用者是可见的。
// 根据你的函数返回的长度, 它会打印出数组中<strong> 该长度范围内</strong> 的所有元素。
for (int i = 0; i < len; i++) {
    print(nums[i]);
}</pre>
<p> </p>
<p><strong>示例 1:</strong></p>
<pre><strong>输入:</strong>nums = [1,1,1,2,2,3]<strong><br />输出:</strong>5, nums = [1,1,2,2,3]<strong><br />解释:</strong>函数应返回新长度 length = <strong>5</strong>, 并且原数组的前五个元素被修改为 <strong>1, 1, 2, 2,</strong> <strong>3 </strong>。 不需要考虑数组中超出新长度后面的元素。</pre>
<p><strong>示例 2:</strong></p>
<pre><strong>输入:</strong>nums = [0,0,1,1,1,1,2,3,3]<strong><br />输出:</strong>7, nums = [0,0,1,1,2,3,3]<strong><br />解释:</strong>函数应返回新长度 length = <strong>7</strong>, 并且原数组的前五个元素被修改为 <strong>0</strong>, <strong>0</strong>, <strong>1</strong>, <strong>1</strong>, <strong>2</strong>, <strong>3</strong>, <strong>3 。</strong> 不需要考虑数组中超出新长度后面的元素。</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= nums.length <= 3 * 10<sup>4</sup></code></li>
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>nums</code> 已按升序排列</li>
</ul>
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
static int removeDuplicates(int *nums, int numsSize)
{
if (numsSize == 0)
{
return 0;
}
int i;
int len = 0;
int count = 1;
for (i = 1; i < numsSize; i++)
{
if (nums[len] == nums[i])
{
if (count < 2)
{
count++;
nums[++len] = nums[i];
}
}
else
{
count = 1;
nums[++len] = nums[i];
}
}
return len + 1;
}
int main(int argc, char **argv)
{
int i, count = argc - 1;
int *nums = malloc(count * sizeof(int));
for (i = 0; i < count; i++)
{
nums[i] = atoi(argv[i + 1]);
}
count = removeDuplicates(nums, count);
for (i = 0; i < count; i++)
{
printf("%d ", nums[i]);
}
printf("\n");
}
\ No newline at end of file
<div class="notranslate">
<p>请你来实现一个&nbsp;<code>myAtoi(string s)</code>&nbsp;函数,使其能将字符串转换成一个 32 位有符号整数(类似 C/C++ 中的 <code>atoi</code> 函数)。</p>
<p>函数&nbsp;<code>myAtoi(string s)</code> 的算法如下:</p>
<ul>
<li>读入字符串并丢弃无用的前导空格</li>
<li>检查下一个字符(假设还未到字符末尾)为正还是负号,读取该字符(如果有)。 确定最终结果是负数还是正数。 如果两者都不存在,则假定结果为正。</li>
<li>读入下一个字符,直到到达下一个非数字字符或到达输入的结尾。字符串的其余部分将被忽略。</li>
<li>将前面步骤读入的这些数字转换为整数(即,"123" -&gt; 123, "0032" -&gt; 32)。如果没有读入数字,则整数为 <code>0</code> 。必要时更改符号(从步骤 2 开始)。</li>
<li>如果整数数超过 32 位有符号整数范围 <code>[−2<sup>31</sup>,&nbsp; 2<sup>31&nbsp;</sup>− 1]</code>
,需要截断这个整数,使其保持在这个范围内。具体来说,小于 <code>−2<sup>31</sup></code> 的整数应该被固定为 <code>−2<sup>31</sup></code> ,大于
<code>2<sup>31&nbsp;</sup>− 1</code> 的整数应该被固定为 <code>2<sup>31&nbsp;</sup>− 1</code>
</li>
<li>返回整数作为最终结果。</li>
</ul>
<p><strong>注意:</strong></p>
<ul>
<li>本题中的空白字符只包括空格字符 <code>' '</code></li>
<li>除前导空格或数字后的其余字符串外,<strong>请勿忽略</strong> 任何其他字符。</li>
</ul>
<p>&nbsp;</p>
<p><strong>示例&nbsp;1:</strong></p>
<pre><strong>输入:</strong>s = "42"
<strong><br />输出:</strong>42
<strong><br />解释:</strong>加粗的字符串为已经读入的字符,插入符号是当前读取的字符。
第 1 步:"42"(当前没有读入字符,因为没有前导空格)
^
第 2 步:"42"(当前没有读入字符,因为这里不存在 '-' 或者 '+')
^
第 3 步:"<strong>42</strong>"(读入 "42")
^
解析得到整数 42 。
由于 "42" 在范围 [-2<sup>31</sup>, 2<sup>31</sup> - 1] 内,最终结果为 42 。</pre>
<p><strong>示例&nbsp;2:</strong></p>
<pre><strong>输入:</strong>s = " -42"
<strong><br />输出:</strong>-42
<strong><br />解释:</strong>
第 1 步:" -42"(读入前导空格,但忽视掉)
^
第 2 步:" -42"(读入 '-' 字符,所以结果应该是负数)
^
第 3 步:" -<strong>42</strong>"(读入 "42")
^
解析得到整数 -42 。
由于 "-42" 在范围 [-2<sup>31</sup>, 2<sup>31</sup> - 1] 内,最终结果为 -42 。
</pre>
<p><strong>示例&nbsp;3:</strong></p>
<pre><strong>输入:</strong>s = "4193 with words"
<strong><br />输出:</strong>4193
<strong><br />解释:</strong>
第 1 步:"4193 with words"(当前没有读入字符,因为没有前导空格)
^
第 2 步:"4193 with words"(当前没有读入字符,因为这里不存在 '-' 或者 '+')
^
第 3 步:"<strong>4193</strong> with words"(读入 "4193";由于下一个字符不是一个数字,所以读入停止)
^
解析得到整数 4193 。
由于 "4193" 在范围 [-2<sup>31</sup>, 2<sup>31</sup> - 1] 内,最终结果为 4193 。
</pre>
<p><strong>示例&nbsp;4:</strong></p>
<pre><strong>输入:</strong>s = "words and 987"
<strong><br />输出:</strong>0
<strong><br />解释:</strong>
第 1 步:"words and 987"(当前没有读入字符,因为没有前导空格)
^
第 2 步:"words and 987"(当前没有读入字符,因为这里不存在 '-' 或者 '+')
^
第 3 步:"words and 987"(由于当前字符 'w' 不是一个数字,所以读入停止)
^
解析得到整数 0 ,因为没有读入任何数字。
由于 0 在范围 [-2<sup>31</sup>, 2<sup>31</sup> - 1] 内,最终结果为 0 。</pre>
<p><strong>示例&nbsp;5:</strong></p>
<pre><strong>输入:</strong>s = "-91283472332"
<strong><br />输出:</strong>-2147483648
<strong><br />解释:</strong>
第 1 步:"-91283472332"(当前没有读入字符,因为没有前导空格)
^
第 2 步:"-91283472332"(读入 '-' 字符,所以结果应该是负数)
^
第 3 步:"-<strong>91283472332</strong>"(读入 "91283472332")
^
解析得到整数 -91283472332 。
由于 -91283472332 小于范围 [-2<sup>31</sup>, 2<sup>31</sup> - 1] 的下界,最终结果被截断为 -2<sup>31</sup> = -2147483648 。</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>0 &lt;= s.length &lt;= 200</code></li>
<li><code>s</code> 由英文字母(大写和小写)、数字(<code>0-9</code>)、<code>' '</code><code>'+'</code><code>'-'</code>
<code>'.'</code> 组成
</li>
</ul>
</div>
\ No newline at end of file
int myAtoi(char *str)
{
int i = 0;
int sign = 0;
while (str[i] && str[i] == ' ')
i++;
if (str[i] == NULL)
return 0;
if (str[i] == '-')
{
sign = 1;
i++;
}
else if (str[i] == '+')
{
sign = 0;
i++;
}
else if (str[i] < '0')
return 0;
else if (str[i] > '9')
return 0;
long long int r = 0;
while (str[i])
{
if (str[i] < '0')
break;
else if (str[i] > '9')
break;
else
r = r * 10 + str[i++] - '0';
if (r > INT_MAX)
break;
}
r = sign ? -r : r;
if (r < INT_MIN)
return INT_MIN;
if (r > INT_MAX)
return INT_MAX;
return (int)r;
}
\ No newline at end of file
<p>已知存在一个按非降序排列的整数数组 <code>nums</code> ,数组中的值不必互不相同。</p>
<p>在传递给函数之前,<code>nums</code> 在预先未知的某个下标 <code>k</code><code>0 <= k < nums.length</code>)上进行了 <strong>旋转
</strong>,使数组变为 <code>[nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]]</code>(下标 <strong>从 0
开始</strong> 计数)。例如, <code>[0,1,2,4,4,4,5,6,6,7]</code> 在下标 <code>5</code> 处经旋转后可能变为
<code>[4,5,6,6,7,0,1,2,4,4]</code>
</p>
<p>给你 <strong>旋转后</strong> 的数组 <code>nums</code> 和一个整数 <code>target</code> ,请你编写一个函数来判断给定的目标值是否存在于数组中。如果
<code>nums</code> 中存在这个目标值 <code>target</code> ,则返回 <code>true</code> ,否则返回 <code>false</code>
</p>
<p> </p>
<p><strong>示例 1:</strong></p>
<pre><strong>输入:</strong>nums = [2,5,6,0,0,1,2], target = 0<strong><br />输出:</strong>true</pre>
<p><strong>示例 2:</strong></p>
<pre><strong>输入:</strong>nums = [2,5,6,0,0,1,2], target = 3<strong><br />输出:</strong>false</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5000</code></li>
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
<li>题目数据保证 <code>nums</code> 在预先未知的某个下标上进行了旋转</li>
<li><code>-10<sup>4</sup> <= target <= 10<sup>4</sup></code></li>
</ul>
<p> </p>
<p><strong>进阶:</strong></p>
<ul>
<li>这是 <a
href="https://leetcode-cn.com/problems/search-in-rotated-sorted-array/description/">搜索旋转排序数组</a> 的延伸题目,本题中的 <code>nums</code> 
可能包含重复元素。</li>
<li>这会影响到程序的时间复杂度吗?会有怎样的影响,为什么?</li>
</ul>
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
static bool search(int *nums, int numsSize, int target)
{
int lo = 0;
int hi = numsSize - 1;
while (lo <= hi)
{
int mid = lo + (hi - lo) / 2;
if (nums[mid] == target)
{
return true;
}
if (nums[lo] == nums[mid] && nums[mid] == nums[hi])
{
lo++;
hi--;
}
else if (nums[lo] <= nums[mid])
{
if (nums[lo] <= target && target < nums[mid])
{
hi = mid - 1;
}
else
{
lo = mid + 1;
}
}
else
{
if (nums[mid] < target && target <= nums[hi])
{
lo = mid + 1;
}
else
{
hi = mid - 1;
}
}
}
return false;
}
int main(int argc, char **argv)
{
int i;
int target = atoi(argv[1]);
int size = argc - 2;
int *nums = malloc(size * sizeof(int));
for (i = 0; i < argc - 2; i++)
{
nums[i] = atoi(argv[i + 2]);
}
printf("%d\n", search(nums, size, target));
return 0;
}
\ No newline at end of file
<p>存在一个按升序排列的链表,给你这个链表的头节点 <code>head</code> ,请你删除链表中所有存在数字重复情况的节点,只保留原始链表中 <strong>没有重复出现</strong><em> </em>的数字。</p><p>返回同样按升序排列的结果链表。</p><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0082.Remove%20Duplicates%20from%20Sorted%20List%20II/images/linkedlist1.jpg" style="width: 500px; height: 142px;" /><pre><strong>输入:</strong>head = [1,2,3,3,4,4,5]<strong><br />输出:</strong>[1,2,5]</pre><p><strong>示例 2:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0082.Remove%20Duplicates%20from%20Sorted%20List%20II/images/linkedlist2.jpg" style="width: 500px; height: 205px;" /><pre><strong>输入:</strong>head = [1,1,1,2,3]<strong><br />输出:</strong>[2,3]</pre><p> </p><p><strong>提示:</strong></p><ul> <li>链表中节点数目在范围 <code>[0, 300]</code></li> <li><code>-100 <= Node.val <= 100</code></li> <li>题目数据保证链表已经按升序排列</li></ul>
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
struct ListNode
{
int val;
struct ListNode *next;
};
struct ListNode *deleteDuplicates(struct ListNode *head)
{
struct ListNode dummy;
struct ListNode *p, *q, *prev;
prev = &dummy;
dummy.next = head;
p = q = head;
while (p != NULL)
{
while (q != NULL && q->val == p->val)
{
q = q->next;
}
if (p->next == q)
{
prev = p;
}
else
{
prev->next = q;
}
p = q;
}
return dummy.next;
}
int main(int argc, char **argv)
{
int i;
struct ListNode *head = NULL;
struct ListNode *prev = NULL;
struct ListNode *p;
for (i = 0; i < argc - 1; i++)
{
p = malloc(sizeof(*p));
p->val = atoi(argv[i + 1]);
p->next = NULL;
if (head == NULL)
{
head = p;
prev = head;
}
else
{
prev->next = p;
prev = p;
}
}
p = deleteDuplicates(head);
while (p != NULL)
{
printf("%d ", p->val);
p = p->next;
}
printf("\n");
return 0;
}
\ No newline at end of file
<p>存在一个按升序排列的链表,给你这个链表的头节点 <code>head</code> ,请你删除所有重复的元素,使每个元素 <strong>只出现一次</strong></p><p>返回同样按升序排列的结果链表。</p><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0083.Remove%20Duplicates%20from%20Sorted%20List/images/list1.jpg" style="width: 302px; height: 242px;" /><pre><strong>输入:</strong>head = [1,1,2]<strong><br />输出:</strong>[1,2]</pre><p><strong>示例 2:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0083.Remove%20Duplicates%20from%20Sorted%20List/images/list2.jpg" style="width: 542px; height: 222px;" /><pre><strong>输入:</strong>head = [1,1,2,3,3]<strong><br />输出:</strong>[1,2,3]</pre><p> </p><p><strong>提示:</strong></p><ul> <li>链表中节点数目在范围 <code>[0, 300]</code></li> <li><code>-100 <= Node.val <= 100</code></li> <li>题目数据保证链表已经按升序排列</li></ul>
\ No newline at end of file
#include <bits/stdc++.h>
using namespace std;
struct ListNode
{
int val;
ListNode *next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode *next) : val(x), next(next) {}
};
class Solution
{
public:
ListNode *deleteDuplicates(ListNode *head)
{
if (head == nullptr)
{
return nullptr;
}
ListNode *prev = head;
ListNode *p = prev->next;
while (p != nullptr)
{
if (p->val != prev->val)
{
prev->next = p;
prev = p;
}
p = p->next;
}
prev->next = p;
return head;
}
};
\ No newline at end of file
<p>给定 <em>n</em> 个非负整数,用来表示柱状图中各个柱子的高度。每个柱子彼此相邻,且宽度为 1 。</p><p>求在该柱状图中,能够勾勒出来的矩形的最大面积。</p><p>&nbsp;</p><p><img src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0084.Largest%20Rectangle%20in%20Histogram/images/histogram.png"></p><p><small>以上是柱状图的示例,其中每个柱子的宽度为 1,给定的高度为&nbsp;<code>[2,1,5,6,2,3]</code></small></p><p>&nbsp;</p><p><img src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0084.Largest%20Rectangle%20in%20Histogram/images/histogram_area.png"></p><p><small>图中阴影部分为所能勾勒出的最大矩形面积,其面积为&nbsp;<code>10</code>&nbsp;个单位。</small></p><p>&nbsp;</p><p><strong>示例:</strong></p><pre><strong>输入:</strong> [2,1,5,6,2,3]<strong><br />输出:</strong> 10</pre>
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
static int largestRectangleArea(int *heights, int heightsSize)
{
int *indexes = malloc(heightsSize * sizeof(int));
int *left = malloc(heightsSize * sizeof(int));
int *right = malloc(heightsSize * sizeof(int));
int i, pos = 0;
for (i = 0; i < heightsSize; i++)
{
while (pos > 0 && heights[indexes[pos - 1]] >= heights[i])
{
pos--;
}
left[i] = pos == 0 ? -1 : indexes[pos - 1];
indexes[pos++] = i;
}
pos = 0;
for (i = heightsSize - 1; i >= 0; i--)
{
while (pos > 0 && heights[indexes[pos - 1]] >= heights[i])
{
pos--;
}
right[i] = pos == 0 ? heightsSize : indexes[pos - 1];
indexes[pos++] = i;
}
int max_area = 0;
for (i = 0; i < heightsSize; i++)
{
int area = heights[i] * (right[i] - left[i] - 1);
max_area = area > max_area ? area : max_area;
}
return max_area;
}
int main(void)
{
int nums[] = {2, 1, 5, 6, 2, 3};
int count = sizeof(nums) / sizeof(*nums);
printf("%d\n", largestRectangleArea(nums, count));
return 0;
}
\ No newline at end of file
<p>给定一个仅包含 <code>0</code><code>1</code> 、大小为 <code>rows x cols</code> 的二维二进制矩阵,找出只包含 <code>1</code> 的最大矩形,并返回其面积。</p><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0085.Maximal%20Rectangle/images/maximal.jpg" style="width: 402px; height: 322px;" /><pre><strong>输入:</strong>matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]<strong><br />输出:</strong>6<strong><br />解释:</strong>最大矩形如上图所示。</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>matrix = []<strong><br />输出:</strong>0</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>matrix = [["0"]]<strong><br />输出:</strong>0</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>matrix = [["1"]]<strong><br />输出:</strong>1</pre><p><strong>示例 5:</strong></p><pre><strong>输入:</strong>matrix = [["0","0"]]<strong><br />输出:</strong>0</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>rows == matrix.length</code></li> <li><code>cols == matrix[0].length</code></li> <li><code>0 <= row, cols <= 200</code></li> <li><code>matrix[i][j]</code><code>'0'</code><code>'1'</code></li></ul>
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
static inline int max(int a, int b)
{
return a > b ? a : b;
}
static int area_calc(int *heights, int size)
{
int *indexes = malloc(size * sizeof(int));
int *lhist = malloc(size * sizeof(int));
int *rhist = malloc(size * sizeof(int));
int i, pos = 0;
for (i = 0; i < size; i++)
{
while (pos > 0 && heights[indexes[pos - 1]] >= heights[i])
{
pos--;
}
lhist[i] = pos == 0 ? -1 : indexes[pos - 1];
indexes[pos++] = i;
}
pos = 0;
for (i = size - 1; i >= 0; i--)
{
while (pos > 0 && heights[indexes[pos - 1]] >= heights[i])
{
pos--;
}
rhist[i] = pos == 0 ? size : indexes[pos - 1];
indexes[pos++] = i;
}
int max_area = 0;
for (i = 0; i < size; i++)
{
int area = heights[i] * (rhist[i] - lhist[i] - 1);
max_area = max(area, max_area);
}
return max_area;
}
static int maximalRectangle(char **matrix, int matrixRowSize, int matrixColSize)
{
int i, j, max_area = 0;
int *heights = malloc(matrixColSize * sizeof(int));
memset(heights, 0, matrixColSize * sizeof(int));
for (i = 0; i < matrixRowSize; i++)
{
for (j = 0; j < matrixColSize; j++)
{
heights[j] = matrix[i][j] == '1' ? heights[j] + 1 : 0;
}
max_area = max(max_area, area_calc(heights, matrixColSize));
}
return max_area;
}
int main(int argc, char **argv)
{
if (argc < 2)
{
fprintf(stderr, "Usage: ./test row1 row2...\n");
exit(-1);
}
int i, j;
int row_size = argc - 1;
int col_size = strlen(argv[1]);
for (i = 0; i < row_size; i++)
{
printf("%s\n", argv[i + 1]);
}
printf("%d\n", maximalRectangle(argv + 1, argc - 1, strlen(argv[1])));
return 0;
}
\ No newline at end of file
<p>给你一个链表的头节点 <code>head</code> 和一个特定值<em> </em><code>x</code> ,请你对链表进行分隔,使得所有 <strong>小于</strong> <code>x</code> 的节点都出现在 <strong>大于或等于</strong> <code>x</code> 的节点之前。</p><p>你应当 <strong>保留</strong> 两个分区中每个节点的初始相对位置。</p><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0086.Partition%20List/images/partition.jpg" style="width: 662px; height: 222px;" /><pre><strong>输入:</strong>head = [1,4,3,2,5,2], x = 3<strong><br />输出</strong>:[1,2,2,4,3,5]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>head = [2,1], x = 2<strong><br />输出</strong>:[1,2]</pre><p> </p><p><strong>提示:</strong></p><ul> <li>链表中节点的数目在范围 <code>[0, 200]</code></li> <li><code>-100 <= Node.val <= 100</code></li> <li><code>-200 <= x <= 200</code></li></ul>
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
struct ListNode
{
int val;
struct ListNode *next;
};
struct ListNode *partition(struct ListNode *head, int x)
{
struct ListNode dummy;
struct ListNode *prev1 = &dummy, *pivot;
dummy.next = head;
for (pivot = head; pivot != NULL; pivot = pivot->next)
{
if (pivot->val >= x)
{
break;
}
prev1 = pivot;
}
struct ListNode *p = pivot->next;
struct ListNode *prev2 = pivot;
while (p != NULL)
{
if (p->val < x)
{
prev2->next = p->next;
p->next = prev1->next;
prev1->next = p;
prev1 = p;
p = prev2->next;
}
else
{
prev2 = p;
p = p->next;
}
}
return dummy.next;
}
int main(int argc, char **argv)
{
if (argc < 2)
{
fprintf(stderr, "Usage: ./test target n1 n2 n3...\n");
exit(-1);
}
int i, target = atoi(argv[1]);
struct ListNode *head = NULL;
struct ListNode *prev = NULL;
struct ListNode *p;
for (i = 0; i < argc - 2; i++)
{
p = malloc(sizeof(*p));
p->val = atoi(argv[i + 2]);
p->next = NULL;
if (head == NULL)
{
head = p;
prev = head;
}
else
{
prev->next = p;
prev = p;
}
}
p = partition(head, target);
while (p != NULL)
{
printf("%d ", p->val);
p = p->next;
}
printf("\n");
return 0;
}
\ No newline at end of file
<div class="notranslate">使用下面描述的算法可以扰乱字符串 <code>s</code> 得到字符串 <code>t</code>
<ol>
<li>如果字符串的长度为 1 ,算法停止</li>
<li>如果字符串的长度 &gt; 1 ,执行下述步骤:
<ul>
<li>在一个随机下标处将字符串分割成两个非空的子字符串。即,如果已知字符串 <code>s</code> ,则可以将其分成两个子字符串 <code>x</code><code>y</code>
,且满足 <code>s = x + y</code></li>
<li><strong>随机</strong> 决定是要「交换两个子字符串」还是要「保持这两个子字符串的顺序不变」。即,在执行这一步骤之后,<code>s</code> 可能是
<code>s = x + y</code> 或者 <code>s = y + x</code>
</li>
<li><code>x</code><code>y</code> 这两个子字符串上继续从步骤 1 开始递归执行此算法。</li>
</ul>
</li>
</ol>
<p>给你两个 <strong>长度相等</strong> 的字符串 <code>s1</code><em>
</em>&nbsp;<code>s2</code>,判断&nbsp;<code>s2</code><em>&nbsp;</em>是否是&nbsp;<code>s1</code><em>&nbsp;</em>的扰乱字符串。如果是,返回
<code>true</code> ;否则,返回 <code>false</code>
</p>
<p>&nbsp;</p>
<p><strong>示例 1:</strong></p>
<pre><strong>输入:</strong>s1 = "great", s2 = "rgeat"
<strong><br />输出:</strong>true
<strong><br />解释:</strong>s1 上可能发生的一种情形是:
"great" --&gt; "gr/eat" // 在一个随机下标处分割得到两个子字符串
"gr/eat" --&gt; "gr/eat" // 随机决定:「保持这两个子字符串的顺序不变」
"gr/eat" --&gt; "g/r / e/at" // 在子字符串上递归执行此算法。两个子字符串分别在随机下标处进行一轮分割
"g/r / e/at" --&gt; "r/g / e/at" // 随机决定:第一组「交换两个子字符串」,第二组「保持这两个子字符串的顺序不变」
"r/g / e/at" --&gt; "r/g / e/ a/t" // 继续递归执行此算法,将 "at" 分割得到 "a/t"
"r/g / e/ a/t" --&gt; "r/g / e/ a/t" // 随机决定:「保持这两个子字符串的顺序不变」
算法终止,结果字符串和 s2 相同,都是 "rgeat"
这是一种能够扰乱 s1 得到 s2 的情形,可以认为 s2 是 s1 的扰乱字符串,返回 true
</pre>
<p><strong>示例 2:</strong></p>
<pre><strong>输入:</strong>s1 = "abcde", s2 = "caebd"
<strong><br />输出:</strong>false
</pre>
<p><strong>示例 3:</strong></p>
<pre><strong>输入:</strong>s1 = "a", s2 = "a"
<strong><br />输出:</strong>true
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>s1.length == s2.length</code></li>
<li><code>1 &lt;= s1.length &lt;= 30</code></li>
<li><code>s1</code><code>s2</code> 由小写英文字母组成</li>
</ul>
</div>
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
static bool scramble(char *s1, int low1, int high1, char *s2, int low2, int high2)
{
if (high1 - low1 != high2 - low2)
{
return false;
}
else if (!memcmp(s1 + low1, s2 + low2, high1 - low1 + 1))
{
return true;
}
else
{
int i, c1[128] = {0}, c2[128] = {0};
for (i = low1; i <= high1; i++)
{
c1[s1[i]]++;
}
for (i = low2; i <= high2; i++)
{
c2[s2[i]]++;
}
if (memcmp(c1, c2, 128 * sizeof(int)))
{
return false;
}
else
{
int len = high1 - low1 + 1;
for (i = 1; i < len; i++)
{
if (scramble(s1, low1, low1 + i - 1, s2, low2, low2 + i - 1) &&
scramble(s1, low1 + i, high1, s2, low2 + i, high2))
{
return true;
}
if (scramble(s1, low1, low1 + i - 1, s2, high2 - i + 1, high2) &&
scramble(s1, low1 + i, high1, s2, low2, high2 - i))
{
return true;
}
}
return false;
}
}
}
static bool isScramble(char *s1, char *s2)
{
return scramble(s1, 0, strlen(s1) - 1, s2, 0, strlen(s2) - 1);
}
int main(int argc, char **argv)
{
if (argc != 3)
{
fprintf(stderr, "Usage: ./test s1 s2\n");
exit(-1);
}
printf("%s\n", isScramble(argv[1], argv[2]) ? "true" : "false");
return 0;
}
\ No newline at end of file
<p>给你两个有序整数数组 <code>nums1</code><em> </em><code>nums2</code>,请你将 <code>nums2</code><em> </em>合并到 <code>nums1</code><em> </em><em></em>使 <code>nums1</code><em> </em>成为一个有序数组。</p><p>初始化 <code>nums1</code><code>nums2</code> 的元素数量分别为 <code>m</code><code>n</code><em> </em>。你可以假设 <code>nums1</code><em> </em>的空间大小等于 <code>m + n</code>,这样它就有足够的空间保存来自 <code>nums2</code> 的元素。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3<strong><br />输出:</strong>[1,2,2,3,5,6]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>nums1 = [1], m = 1, nums2 = [], n = 0<strong><br />输出:</strong>[1]</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>nums1.length == m + n</code></li> <li><code>nums2.length == n</code></li> <li><code>0 <= m, n <= 200</code></li> <li><code>1 <= m + n <= 200</code></li> <li><code>-10<sup>9</sup> <= nums1[i], nums2[i] <= 10<sup>9</sup></code></li></ul>
\ No newline at end of file
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
void merge(vector<int> &nums1, int m, vector<int> &nums2, int n)
{
int i = m - 1;
int j = n - 1;
int k = nums1.size() - 1;
while (i >= 0 && j >= 0)
{
if (nums1[i] < nums2[j])
{
nums1[k--] = nums2[j--];
}
else
{
nums1[k--] = nums1[i--];
}
}
while (j >= 0)
{
nums1[k--] = nums2[j--];
}
}
};
\ No newline at end of file
<p>格雷编码是一个二进制数字系统,在该系统中,两个连续的数值仅有一个位数的差异。</p>
<p>给定一个代表编码总位数的非负整数<em> n</em>,打印其格雷编码序列。即使有多个不同答案,你也只需要返回其中一种。</p>
<p>格雷编码序列必须以 0 开头。</p>
<p>&nbsp;</p>
<p><strong>示例 1:</strong></p>
<pre><strong>输入:</strong>&nbsp;2<strong><br />输出:</strong>&nbsp;[0,1,3,2]<strong><br />解释:</strong>00 - 001 - 111 - 310 - 2对于给定的&nbsp;<em>n</em>,其格雷编码序列并不唯一。例如,[0,2,3,1]&nbsp;也是一个有效的格雷编码序列。00 - 010 - 211 - 301 - 1</pre>
<p><strong>示例&nbsp;2:</strong></p>
<pre><strong>输入:</strong>&nbsp;0<strong><br />输出:</strong>&nbsp;[0]<strong><br />解释:</strong> 我们定义格雷编码序列必须以 0 开头。给定编码总位数为 <em>n</em> 的格雷编码序列,其长度为 2<sup>n</sup>。当 <em>n</em> = 0 时,长度为 2<sup>0</sup> = 1。因此,当 <em>n</em> = 0 时,其格雷编码序列为 [0]。</pre>
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
int *grayCode(int n, int *returnSize)
{
if (n < 0)
{
return NULL;
}
int i, count = 1 << n;
int *codes = malloc(count * sizeof(int));
for (i = 0; i < count; i++)
{
codes[i] = (i >> 1) ^ i;
}
*returnSize = 1 << n;
return codes;
}
int main(int argc, char **argv)
{
if (argc != 2)
{
fprintf(stderr, "Usage: ./test n\n");
exit(-1);
}
int i, count;
int *list = grayCode(atoi(argv[1]), &count);
for (i = 0; i < count; i++)
{
printf("%d ", list[i]);
}
printf("\n");
return 0;
}
\ No newline at end of file
<p>给你一个整数数组 <code>nums</code> ,其中可能包含重复元素,请你返回该数组所有可能的子集(幂集)。</p><p>解集 <strong>不能</strong> 包含重复的子集。返回的解集中,子集可以按 <strong>任意顺序</strong> 排列。</p><div class="original__bRMd"><div><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>nums = [1,2,2]<strong><br />输出:</strong>[[],[1],[1,2],[1,2,2],[2],[2,2]]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>nums = [0]<strong><br />输出:</strong>[[],[0]]</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>1 <= nums.length <= 10</code></li> <li><code>-10 <= nums[i] <= 10</code></li></ul></div></div>
\ No newline at end of file
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
vector<vector<int>> subsetsWithDup(vector<int> &nums)
{
vector<vector<int>> res;
sort(nums.begin(), nums.end());
dfs(nums, 0, res);
return res;
}
private:
vector<int> stack;
void dfs(vector<int> &nums, int start, vector<vector<int>> &res)
{
res.push_back(stack);
int last = INT_MIN;
for (int i = start; i < nums.size(); i++)
{
if (last != nums[i])
{
stack.push_back(nums[i]);
dfs(nums, i + 1, res);
stack.pop_back();
}
last = nums[i];
}
}
};
\ No newline at end of file
<p>给你一个整数 <code>x</code> ,如果 <code>x</code> 是一个回文整数,返回 <code>true</code> ;否则,返回 <code>false</code></p><p>回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。例如,<code>121</code> 是回文,而 <code>123</code> 不是。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>x = 121<strong><br />输出:</strong>true</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>x = -121<strong><br />输出:</strong>false<strong><br />解释:</strong>从左向右读, 为 -121 。 从右向左读, 为 121- 。因此它不是一个回文数。</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>x = 10<strong><br />输出:</strong>false<strong><br />解释:</strong>从右向左读, 为 01 。因此它不是一个回文数。</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>x = -101<strong><br />输出:</strong>false</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>-2<sup>31</sup> <= x <= 2<sup>31</sup> - 1</code></li></ul><p> </p><p><strong>进阶:</strong>你能不将整数转为字符串来解决这个问题吗?</p>
\ No newline at end of file
bool isPalindrome(int x)
{
if (x < 0)
return false;
char r[11];
int n = snprintf(r, 11, "%d", x);
int i;
for (i = 0; i < n / 2; i++)
{
if (r[i] != r[n - i - 1])
return false;
}
return true;
}
\ No newline at end of file
<p>一条包含字母 <code>A-Z</code> 的消息通过以下映射进行了 <strong>编码</strong></p>
<pre>'A' -> 1'B' -> 2...'Z' -> 26</pre>
<p><strong>解码</strong> 已编码的消息,所有数字必须基于上述映射的方法,反向映射回字母(可能有多种方法)。例如,<code>"11106"</code> 可以映射为:</p>
<ul>
<li><code>"AAJF"</code> ,将消息分组为 <code>(1 1 10 6)</code></li>
<li><code>"KJF"</code> ,将消息分组为 <code>(11 10 6)</code></li>
</ul>
<p>注意,消息不能分组为  <code>(1 11 06)</code> ,因为 <code>"06"</code> 不能映射为 <code>"F"</code> ,这是由于 <code>"6"</code>
<code>"06"</code> 在映射中并不等价。
</p>
<p>给你一个只含数字的 <strong>非空 </strong>字符串 <code>s</code> ,请计算并返回 <strong>解码</strong> 方法的 <strong>总数</strong></p>
<p>题目数据保证答案肯定是一个 <strong>32 位</strong> 的整数。</p>
<p> </p>
<p><strong>示例 1:</strong></p>
<pre><strong>输入:</strong>s = "12"<strong><br />输出:</strong>2<strong><br />解释:</strong>它可以解码为 "AB"(1 2)或者 "L"(12)。</pre>
<p><strong>示例 2:</strong></p>
<pre><strong>输入:</strong>s = "226"<strong><br />输出:</strong>3<strong><br />解释:</strong>它可以解码为 "BZ" (2 26), "VF" (22 6), 或者 "BBF" (2 2 6) 。</pre>
<p><strong>示例 3:</strong></p>
<pre><strong>输入:</strong>s = "0"<strong><br />输出:</strong>0<strong><br />解释:</strong>没有字符映射到以 0 开头的数字。含有 0 的有效映射是 'J' -> "10" 和 'T'-> "20" 。由于没有字符,因此没有有效的方法对此进行解码,因为所有数字都需要映射。</pre>
<p><strong>示例 4:</strong></p>
<pre><strong>输入:</strong>s = "06"<strong><br />输出:</strong>0<strong><br />解释:</strong>"06" 不能映射到 "F" ,因为字符串含有前导 0("6" 和 "06" 在映射中并不等价)。</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= s.length <= 100</code></li>
<li><code>s</code> 只包含数字,并且可能包含前导零。</li>
</ul>
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static int numDecodings(char *s)
{
int len = strlen(s);
if (len == 0)
{
return 0;
}
int a = 1;
int b = s[0] == '0' ? 0 : a;
int c = b;
for (int i = 2; i <= len; i++)
{
c = s[i - 1] == '0' ? 0 : b;
int num = (s[i - 2] - '0') * 10 + (s[i - 1] - '0');
if (num >= 10 && num <= 26)
{
c += a;
}
a = b;
b = c;
}
return c;
}
int main(int argc, char **argv)
{
if (argc != 2)
{
fprintf(stderr, "Usage: ./test number\n");
exit(-1);
}
printf("%d\n", numDecodings(argv[1]));
return 0;
}
\ No newline at end of file
给你单链表的头指针 <code>head</code> 和两个整数 <code>left</code><code>right</code> ,其中 <code>left <= right</code> 。请你反转从位置 <code>left</code> 到位置 <code>right</code> 的链表节点,返回 <strong>反转后的链表</strong><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0092.Reverse%20Linked%20List%20II/images/rev2ex2.jpg" style="width: 542px; height: 222px;" /><pre><strong>输入:</strong>head = [1,2,3,4,5], left = 2, right = 4<strong><br />输出:</strong>[1,4,3,2,5]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>head = [5], left = 1, right = 1<strong><br />输出:</strong>[5]</pre><p> </p><p><strong>提示:</strong></p><ul> <li>链表中节点数目为 <code>n</code></li> <li><code>1 <= n <= 500</code></li> <li><code>-500 <= Node.val <= 500</code></li> <li><code>1 <= left <= right <= n</code></li></ul><p> </p><p><strong>进阶:</strong> 你可以使用一趟扫描完成反转吗?</p>
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
struct ListNode
{
int val;
struct ListNode *next;
};
static struct ListNode *reverseBetween(struct ListNode *head, int m, int n)
{
int i;
struct ListNode dummy;
struct ListNode *prev = &dummy;
prev->next = head;
for (i = 1; i < m; i++)
{
prev = prev->next;
}
struct ListNode *p = prev->next;
for (i = m; i < n; i++)
{
struct ListNode *q = p->next;
p->next = q->next;
q->next = prev->next;
prev->next = q;
}
return dummy.next;
}
int main(int argc, char **argv)
{
if (argc < 3)
{
fprintf(stderr, "Usage: ./test m n 1 2 3...\n");
exit(-1);
}
int i, count = argc - 3;
struct ListNode dummy;
struct ListNode *prev = &dummy;
struct ListNode *p;
for (i = 0; i < count; i++)
{
p = malloc(sizeof(*p));
p->val = atoi(argv[i + 3]);
p->next = NULL;
prev->next = p;
prev = p;
}
int m = atoi(argv[1]);
int n = atoi(argv[2]);
struct ListNode *head = reverseBetween(dummy.next, m, n);
for (p = head; p != NULL; p = p->next)
{
printf("%d ", p->val);
}
printf("\n");
return 0;
}
\ No newline at end of file
<p>给定一个只包含数字的字符串,用以表示一个 IP 地址,返回所有可能从 <code>s</code> 获得的 <strong>有效 IP 地址 </strong>。你可以按任何顺序返回答案。</p><p><strong>有效 IP 地址</strong> 正好由四个整数(每个整数位于 0 到 255 之间组成,且不能含有前导 <code>0</code>),整数之间用 <code>'.'</code> 分隔。</p><p>例如:"0.1.2.201" 和 "192.168.1.1" 是 <strong>有效</strong> IP 地址,但是 "0.011.255.245"、"192.168.1.312" 和 "192.168@1.1" 是 <strong>无效</strong> IP 地址。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>s = "25525511135"<strong><br />输出:</strong>["255.255.11.135","255.255.111.35"]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>s = "0000"<strong><br />输出:</strong>["0.0.0.0"]</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>s = "1111"<strong><br />输出:</strong>["1.1.1.1"]</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>s = "010010"<strong><br />输出:</strong>["0.10.0.10","0.100.1.0"]</pre><p><strong>示例 5:</strong></p><pre><strong>输入:</strong>s = "101023"<strong><br />输出:</strong>["1.0.10.23","1.0.102.3","10.1.0.23","10.10.2.3","101.0.2.3"]</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>0 <= s.length <= 3000</code></li> <li><code>s</code> 仅由数字组成</li></ul>
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
static bool valid(char *ip, int len)
{
if (len > 1 && ip[0] == '0')
{
return false;
}
if (len == 3)
{
int n = (ip[0] - '0') * 100 + (ip[1] - '0') * 10 + (ip[2] - '0');
if (n > 255)
{
return false;
}
}
return true;
}
#define WIDTH 4
static void dfs(char *s, int start, char *stack, int num, char **results, int *count)
{
int i, j;
if (num == 4)
{
if (s[start] == '\0')
{
results[*count] = malloc(3 * 4 + 3 + 1);
char *p = results[*count];
for (j = 0; j < num; j++)
{
char *q = stack + j * WIDTH;
while ((*p++ = *q++) != '\0')
{
}
if (j != 3)
{
*(p - 1) = '.';
}
}
(*count)++;
}
}
else
{
char *p = stack + num * WIDTH;
char *q = p;
for (i = start; s[i] != '\0' && i < start + 3; i++)
{
*q++ = s[i];
*q = '\0';
if (!valid(p, q - p))
{
return;
}
dfs(s, i + 1, stack, num + 1, results, count);
if (num + 1 < 4)
{
memset(stack + (num + 1) * WIDTH, 0, WIDTH);
}
}
}
}
static char **restoreIpAddresses(char *s, int *returnSize)
{
int count = 0;
char **results = malloc(100 * sizeof(char *));
char addr[16] = {'\0'};
dfs(s, 0, addr, 0, results, &count);
*returnSize = count;
return results;
}
int main(int argc, char **argv)
{
if (argc != 2)
{
fprintf(stderr, "Usage: ./test num\n");
exit(-1);
}
int i, count = 0;
char **list = restoreIpAddresses(argv[1], &count);
for (i = 0; i < count; i++)
{
printf("%s\n", list[i]);
}
}
\ No newline at end of file
<p>给定一个二叉树的根节点 <code>root</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/0094.Binary%20Tree%20Inorder%20Traversal/images/inorder_1.jpg" style="width: 202px; height: 324px;" /><pre><strong>输入:</strong>root = [1,null,2,3]<strong><br />输出:</strong>[1,3,2]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>root = []<strong><br />输出:</strong>[]</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>root = [1]<strong><br />输出:</strong>[1]</pre><p><strong>示例 4:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0094.Binary%20Tree%20Inorder%20Traversal/images/inorder_5.jpg" style="width: 202px; height: 202px;" /><pre><strong>输入:</strong>root = [1,2]<strong><br />输出:</strong>[2,1]</pre><p><strong>示例 5:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0094.Binary%20Tree%20Inorder%20Traversal/images/inorder_4.jpg" style="width: 202px; height: 202px;" /><pre><strong>输入:</strong>root = [1,null,2]<strong><br />输出:</strong>[1,2]</pre><p> </p><p><strong>提示:</strong></p><ul> <li>树中节点数目在范围 <code>[0, 100]</code></li> <li><code>-100 <= Node.val <= 100</code></li></ul><p> </p><p><strong>进阶:</strong> 递归算法很简单,你可以通过迭代算法完成吗?</p>
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
struct TreeNode
{
int val;
struct TreeNode *left;
struct TreeNode *right;
};
static void traverse(struct TreeNode *node, int *result, int *count)
{
if (node == NULL)
{
return;
}
traverse(node->left, result, count);
result[*count] = node->val;
(*count)++;
traverse(node->right, result, count);
}
static int *inorderTraversal(struct TreeNode *root, int *returnSize)
{
if (root == NULL)
{
*returnSize = 0;
return NULL;
}
int count = 0;
int *result = malloc(5000 * sizeof(int));
traverse(root, result, &count);
*returnSize = count;
return result;
}
int main()
{
int count = 0;
inorderTraversal(NULL, &count);
return 0;
}
\ No newline at end of file
<div class="notranslate">
<p>给你一个整数 <code>n</code> ,请你生成并返回所有由 <code>n</code> 个节点组成且节点值从 <code>1</code><code>n</code> 互不相同的不同
<strong>二叉搜索树</strong><em> </em>。可以按 <strong>任意顺序</strong> 返回答案。
</p>
<p>&nbsp;</p>
<div class="original__bRMd">
<div>
<p><strong>示例 1:</strong></p>
<img style="width: 600px; height: 148px;"
src="https://assets.leetcode.com/uploads/2021/01/18/uniquebstn3.jpg" alt="">
<pre><strong>输入:</strong>n = 3
<strong><br />输出:</strong>[[1,null,2,null,3],[1,null,3,2],[2,1,3],[3,1,null,null,2],[3,2,null,1]]
</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;= 8</code></li>
</ul>
</div>
</div>
</div>
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
struct TreeNode
{
int val;
struct TreeNode *left;
struct TreeNode *right;
};
static struct TreeNode *dfs(int low, int high, int *count)
{
int i, j, k;
if (low > high)
{
*count = 0;
return NULL;
}
else if (low == high)
{
struct TreeNode *node = malloc(sizeof(*node));
node->val = low;
node->left = NULL;
node->right = NULL;
*count = 1;
return node;
}
else
{
*count = 0;
int capacity = 5;
struct TreeNode *roots = malloc(capacity * sizeof(struct TreeNode));
for (i = low; i <= high; i++)
{
int left_cnt, right_cnt;
struct TreeNode *left_subs = dfs(low, i - 1, &left_cnt);
struct TreeNode *right_subs = dfs(i + 1, high, &right_cnt);
if (left_cnt == 0)
left_cnt = 1;
if (right_cnt == 0)
right_cnt = 1;
if (*count + (left_cnt * right_cnt) >= capacity)
{
capacity *= 2;
capacity += left_cnt * right_cnt;
roots = realloc(roots, capacity * sizeof(struct TreeNode));
}
for (j = 0; j < left_cnt; j++)
{
for (k = 0; k < right_cnt; k++)
{
roots[*count].val = i;
roots[*count].left = left_subs == NULL ? NULL : &left_subs[j];
roots[*count].right = right_subs == NULL ? NULL : &right_subs[k];
(*count)++;
}
}
}
return roots;
}
}
static struct TreeNode **generateTrees(int n, int *returnSize)
{
int i, count = 0;
struct TreeNode *roots = dfs(1, n, &count);
struct TreeNode **results = malloc(count * sizeof(struct TreeNode *));
for (i = 0; i < count; i++)
{
results[i] = &roots[i];
}
*returnSize = count;
return results;
}
static void dump(struct TreeNode *node)
{
printf("%d ", node->val);
if (node->left != NULL)
{
dump(node->left);
}
else
{
printf("# ");
}
if (node->right != NULL)
{
dump(node->right);
}
else
{
printf("# ");
}
}
int main(int argc, char **argv)
{
if (argc != 2)
{
fprintf(stderr, "Usage: ./test n\n");
exit(-1);
}
int i, count = 0;
struct TreeNode **results = generateTrees(atoi(argv[1]), &count);
for (i = 0; i < count; i++)
{
dump(results[i]);
printf("\n");
}
return 0;
}
\ No newline at end of file
<div class="notranslate">
<p>给你一个整数 <code>n</code> ,求恰由 <code>n</code> 个节点组成且节点值从 <code>1</code><code>n</code> 互不相同的 <strong>二叉搜索树</strong>
有多少种?返回满足题意的二叉搜索树的种数。</p>
<p>&nbsp;</p>
<p><strong>示例 1:</strong></p>
<img style="width: 600px; height: 148px;" src="https://assets.leetcode.com/uploads/2021/01/18/uniquebstn3.jpg"
alt="">
<pre><strong>输入:</strong>n = 3
<strong><br />输出:</strong>5
</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;= 19</code></li>
</ul>
</div>
\ No newline at end of file
#include <stdc++.h>
using namespace std;
class Solution
{
public:
int numTrees(int n)
{
vector<int> sum(n + 1);
sum[0] = 1;
for (int i = 1; i <= n; i++)
{
for (int j = 0; j < i; j++)
{
sum[i] += sum[j] * sum[i - j - 1];
}
}
return sum[n];
}
}
\ No newline at end of file
<p>给定三个字符串 <code>s1</code><code>s2</code><code>s3</code>,请你帮忙验证 <code>s3</code> 是否是由 <code>s1</code> 和 <code>s2</code><em> </em><strong>交错 </strong>组成的。</p><p>两个字符串 <code>s</code><code>t</code> <strong>交错</strong> 的定义与过程如下,其中每个字符串都会被分割成若干 <strong>非空</strong> 子字符串:</p><ul> <li><code>s = s<sub>1</sub> + s<sub>2</sub> + ... + s<sub>n</sub></code></li> <li><code>t = t<sub>1</sub> + t<sub>2</sub> + ... + t<sub>m</sub></code></li> <li><code>|n - m| <= 1</code></li> <li><strong>交错</strong><code>s<sub>1</sub> + t<sub>1</sub> + s<sub>2</sub> + t<sub>2</sub> + s<sub>3</sub> + t<sub>3</sub> + ...</code> 或者 <code>t<sub>1</sub> + s<sub>1</sub> + t<sub>2</sub> + s<sub>2</sub> + t<sub>3</sub> + s<sub>3</sub> + ...</code></li></ul><p><strong>提示:</strong><code>a + b</code> 意味着字符串 <code>a</code><code>b</code> 连接。</p><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0097.Interleaving%20String/images/interleave.jpg" style="width: 561px; height: 203px;" /><pre><strong>输入:</strong>s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac"<strong><br />输出:</strong>true</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>s1 = "aabcc", s2 = "dbbca", s3 = "aadbbbaccc"<strong><br />输出:</strong>false</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>s1 = "", s2 = "", s3 = ""<strong><br />输出:</strong>true</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>0 <= s1.length, s2.length <= 100</code></li> <li><code>0 <= s3.length <= 200</code></li> <li><code>s1</code><code>s2</code>、和 <code>s3</code> 都由小写英文字母组成</li></ul>
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
static bool isInterleave(char *s1, char *s2, char *s3)
{
int i, j;
int len1 = strlen(s1);
int len2 = strlen(s2);
int len3 = strlen(s3);
if (len1 + len2 != len3)
{
return false;
}
bool *table = malloc((len1 + 1) * (len2 + 1) * sizeof(bool));
bool **dp = malloc((len1 + 1) * sizeof(bool *));
for (i = 0; i < len1 + 1; i++)
{
dp[i] = &table[i * (len2 + 1)];
}
dp[0][0] = true;
for (i = 1; i < len1 + 1; i++)
{
dp[i][0] = dp[i - 1][0] && s1[i - 1] == s3[i - 1];
}
for (i = 1; i < len2 + 1; i++)
{
dp[0][i] = dp[0][i - 1] && s2[i - 1] == s3[i - 1];
}
for (i = 1; i < len1 + 1; i++)
{
for (j = 1; j < len2 + 1; j++)
{
bool up = dp[i - 1][j] && s1[i - 1] == s3[i + j - 1];
bool left = dp[i][j - 1] && s2[j - 1] == s3[i + j - 1];
dp[i][j] = up || left;
}
}
return dp[len1][len2];
}
int main(int argc, char **argv)
{
if (argc != 4)
{
fprintf(stderr, "Usage: ./test s1 s2 s3\n");
exit(-1);
}
printf("%s\n", isInterleave(argv[1], argv[2], argv[3]) ? "true" : "false");
return 0;
}
\ No newline at end of file
<div class="notranslate">
<p>给你一个二叉树的根节点 <code>root</code> ,判断其是否是一个有效的二叉搜索树。</p>
<p><strong>有效</strong> 二叉搜索树定义如下:</p>
<ul>
<li>节点的左子树只包含<strong> 小于 </strong>当前节点的数。</li>
<li>节点的右子树只包含 <strong>大于</strong> 当前节点的数。</li>
<li>所有左子树和右子树自身必须也是二叉搜索树。</li>
</ul>
<p>&nbsp;</p>
<p><strong>示例 1:</strong></p>
<img style="width: 302px; height: 182px;" src="https://assets.leetcode.com/uploads/2020/12/01/tree1.jpg" alt="">
<pre><strong>输入:</strong>root = [2,1,3]
<strong><br />输出:</strong>true
</pre>
<p><strong>示例 2:</strong></p>
<img style="width: 422px; height: 292px;" src="https://assets.leetcode.com/uploads/2020/12/01/tree2.jpg" alt="">
<pre><strong>输入:</strong>root = [5,1,4,null,null,3,6]
<strong><br />输出:</strong>false
<strong><br />解释:</strong>根节点的值是 5 ,但是右子节点的值是 4 。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li>树中节点数目范围在<code>[1, 10<sup>4</sup>]</code></li>
<li><code>-2<sup>31</sup> &lt;= Node.val &lt;= 2<sup>31</sup> - 1</code></li>
</ul>
</div>
\ No newline at end of file
#include <bits/stdc++.h>
using namespace std;
struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
class Solution
{
public:
bool isValidBST(TreeNode *root)
{
stack<TreeNode *> stk;
int prev = INT_MIN;
bool first = true;
while (!stk.empty() || root != nullptr)
{
if (root != nullptr)
{
stk.push(root);
root = root->left;
}
else
{
root = stk.top();
stk.pop();
if (!first && prev >= root->val)
{
return false;
}
first = false;
prev = root->val;
root = root->right;
}
}
return true;
}
};
\ No newline at end of file
<p>给你二叉搜索树的根节点 <code>root</code> ,该树中的两个节点被错误地交换。请在不改变其结构的情况下,恢复这棵树。</p><p><strong>进阶:</strong>使用 O(<em>n</em>) 空间复杂度的解法很容易实现。你能想出一个只使用常数空间的解决方案吗?</p><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0099.Recover%20Binary%20Search%20Tree/images/recover1.jpg" style="width: 422px; height: 302px;" /><pre><strong>输入:</strong>root = [1,3,null,null,2]<strong><br />输出:</strong>[3,1,null,null,2]<strong><br />解释:</strong>3 不能是 1 左孩子,因为 3 > 1 。交换 1 和 3 使二叉搜索树有效。</pre><p><strong>示例 2:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0099.Recover%20Binary%20Search%20Tree/images/recover2.jpg" style="width: 581px; height: 302px;" /><pre><strong>输入:</strong>root = [3,1,4,null,null,2]<strong><br />输出:</strong>[2,1,4,null,null,3]<strong><br />解释:</strong>2 不能在 3 的右子树中,因为 2 < 3 交换 2 3 使二叉搜索树有效。</pre><p> </p><p><strong>提示:</strong></p><ul> <li>树上节点的数目在范围 <code>[2, 1000]</code></li> <li><code>-2<sup>31</sup> <= Node.val <= 2<sup>31</sup> - 1</code></li></ul>
\ No newline at end of file
#include <bits/stdc++.h>
using namespace std;
struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
class Solution
{
public:
void recoverTree(TreeNode *root)
{
dfs(root);
int tmp = p0_->val;
p0_->val = p1_->val;
p1_->val = tmp;
}
private:
int wrong_ = 0;
TreeNode *prev_ = nullptr;
TreeNode *p0_ = nullptr;
TreeNode *p1_ = nullptr;
void dfs(TreeNode *root)
{
if (root == nullptr || wrong_ == 2)
{
return;
}
dfs(root->left);
if (prev_ != nullptr && prev_->val > root->val)
{
if (++wrong_ == 1)
{
p0_ = prev_;
p1_ = root;
}
else if (wrong_ == 2)
{
p1_ = root;
}
}
prev_ = root;
dfs(root->right);
}
};
\ No newline at end of file
<p>给你两棵二叉树的根节点 <code>p</code><code>q</code> ,编写一个函数来检验这两棵树是否相同。</p><p>如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。</p><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0100-0199/0100.Same%20Tree/images/ex1.jpg" style="width: 622px; height: 182px;" /><pre><strong>输入:</strong>p = [1,2,3], q = [1,2,3]<strong><br />输出:</strong>true</pre><p><strong>示例 2:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0100-0199/0100.Same%20Tree/images/ex2.jpg" style="width: 382px; height: 182px;" /><pre><strong>输入:</strong>p = [1,2], q = [1,null,2]<strong><br />输出:</strong>false</pre><p><strong>示例 3:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0100-0199/0100.Same%20Tree/images/ex3.jpg" style="width: 622px; height: 182px;" /><pre><strong>输入:</strong>p = [1,2,1], q = [1,1,2]<strong><br />输出:</strong>false</pre><p> </p><p><strong>提示:</strong></p><ul> <li>两棵树上的节点数目都在范围 <code>[0, 100]</code></li> <li><code>-10<sup>4</sup> <= Node.val <= 10<sup>4</sup></code></li></ul>
\ No newline at end of file
#include <bits/stdc++.h>
using namespace std;
struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
class Solution
{
public:
bool isSameTree(TreeNode *p, TreeNode *q)
{
if (p == nullptr && q == nullptr)
{
return true;
}
if (p == nullptr || q == nullptr)
{
return false;
}
if (p->val != q->val)
{
return false;
}
return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
}
};
\ 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
bool isMatch(char *s, char *p)
{
if (!p || p[0] == NULL)
return (!s || s[0] == NULL);
bool head_match = (s && s[0] && (s[0] == p[0] || p[0] == '.'));
if (p[1] && p[1] == '*')
{
return (head_match && isMatch(s + 1, p)) || isMatch(s, p + 2);
}
else
{
return head_match && isMatch(s + 1, p + 1);
}
}
\ No newline at end of file
import os
import shutil
def leetcode_helper():
data_dir = 'data/3.算法高阶/1.leetcode'
dailycode_exercises_dir = '/Users/zhangzc/Desktop/workplace/daily-code-data/data/input/dailycode/leetcode/exercises'
dirs_ = os.listdir(data_dir)
dirs = []
for dir in dirs_:
dir = os.path.join(data_dir, dir)
if os.path.isdir(dir):
dirs.append(dir)
for dir in dirs:
assert os.path.isdir(dir)
exercises_id = dir.split('/')[-1].split('_')[0]
if 0 <= int(exercises_id) and int(exercises_id) < 100:
desc_src_path = os.path.join(os.path.join(dailycode_exercises_dir, exercises_id), '{}_desc.html'.format(exercises_id))
cpp_code_src_path = os.path.join(os.path.join(dailycode_exercises_dir, exercises_id),'{}.cpp'.format(exercises_id))
desc_dst_path = os.path.join(dir, 'desc.html')
cpp_code_dst_path = os.path.join(dir, 'solution.cpp')
print(cpp_code_src_path)
print(cpp_code_dst_path)
shutil.copy(desc_src_path, desc_dst_path)
shutil.copy(cpp_code_src_path, cpp_code_dst_path)
else:
continue
leetcode_helper()
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册