Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update lc problems #4097

Merged
merged 1 commit into from
Feb 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions solution/0200-0299/0254.Factor Combinations/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ tags:

<p>例如:</p>

<pre>8 = 2 x 2 x 2;
<pre>
8 = 2 x 2 x 2;
= 2 x 4.</pre>

<p>请实现一个函数,该函数接收一个整数 <em>n</em>&nbsp;并返回该整数所有的因子组合。</p>
Expand All @@ -34,18 +35,21 @@ tags:

<p><strong>示例 1:</strong></p>

<pre><strong>输入: </strong><code>1</code>
<pre>
<strong>输入: </strong><code>1</code>
<strong>输出: </strong>[]
</pre>

<p><strong>示例 2:</strong></p>

<pre><strong>输入: </strong><code>37</code>
<pre>
<strong>输入: </strong><code>37</code>
<strong>输出: </strong>[]</pre>

<p><strong>示例 3:</strong></p>

<pre><strong>输入: </strong><code>12</code>
<pre>
<strong>输入: </strong><code>12</code>
<strong>输出:</strong>
[
[2, 6],
Expand All @@ -55,7 +59,8 @@ tags:

<p><strong>示例 4: </strong></p>

<pre><strong>输入: </strong><code>32</code>
<pre>
<strong>输入: </strong><code>32</code>
<strong>输出:</strong>
[
[2, 16],
Expand All @@ -67,6 +72,14 @@ tags:
]
</pre>

<p>&nbsp;</p>

<p><b>提示:</b></p>

<ul>
<li><code>1 &lt;= n &lt;= 10<sup>7</sup></code></li>
</ul>

<!-- description:end -->

## 解法
Expand Down
2 changes: 1 addition & 1 deletion solution/0200-0299/0262.Trips and Users/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ banned is an ENUM (category) type of (&#39;Yes&#39;, &#39;No&#39;).

<p>The <strong>cancellation rate</strong> is computed by dividing the number of canceled (by client or driver) requests with unbanned users by the total number of requests with unbanned users on that day.</p>

<p>Write a solution to find the <strong>cancellation rate</strong> of requests with unbanned users (<strong>both client and driver must not be banned</strong>) each day between <code>&quot;2013-10-01&quot;</code> and <code>&quot;2013-10-03&quot;</code>. Round <code>Cancellation Rate</code> to <strong>two decimal</strong> points.</p>
<p>Write a solution to find the <strong>cancellation rate</strong> of requests with unbanned users (<strong>both client and driver must not be banned</strong>) each day between <code>&quot;2013-10-01&quot;</code> and <code>&quot;2013-10-03&quot;</code> with <strong>at least</strong> one trip. Round <code>Cancellation Rate</code> to <strong>two decimal</strong> points.</p>

<p>Return the result table in <strong>any order</strong>.</p>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ tags:
<pre>
<strong>输入:</strong>words = ["a", "banana", "app", "appl", "ap", "apply", "apple"]
<strong>输出:</strong>"apple"
<strong>解释:</strong>"apply" 和 "apple" 都能由词典中的单词组成。但是 "apple" 的字典序小于 "apply"
<strong>解释:</strong>"apply" 和 "apple" 都能由词典中的单词组成。但是 "apple" 的字典序小于 "apply"
</pre>

<p>&nbsp;</p>
Expand Down
14 changes: 7 additions & 7 deletions solution/0700-0799/0722.Remove Comments/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,20 @@ tags:
<strong>解释:</strong> 示例代码可以编排成这样:
/*Test program */
int main()
{
// variable declaration
{
// variable declaration
int a, b, c;
/* This is a test
multiline
comment for
multiline
comment for
testing */
a = b + c;
}
第 1 行和第 6-9 行的字符串 /* 表示块注释。第 4 行的字符串 // 表示行注释。
编排后:
编排后:
int main()
{

{
int a, b, c;
a = b + c;
}</pre>
Expand Down
12 changes: 6 additions & 6 deletions solution/0700-0799/0722.Remove Comments/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,20 @@ tags:
<strong>Explanation:</strong> The line by line code is visualized as below:
/*Test program */
int main()
{
// variable declaration
{
// variable declaration
int a, b, c;
/* This is a test
multiline
comment for
multiline
comment for
testing */
a = b + c;
}
The string /* denotes a block comment, including line 1 and lines 6-9. The string // denotes line 4 as comments.
The line by line output code is visualized as below:
int main()
{

{
int a, b, c;
a = b + c;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ tags:
<pre>
<strong>Input:</strong> s1 = &quot;abcdebdde&quot;, s2 = &quot;bde&quot;
<strong>Output:</strong> &quot;bcde&quot;
<strong>Explanation:</strong>
<strong>Explanation:</strong>
&quot;bcde&quot; is the answer because it occurs before &quot;bdde&quot; which has the same length.
&quot;deb&quot; is not a smaller window because the elements of s2 in the window must occur in order.
</pre>
Expand Down
2 changes: 1 addition & 1 deletion solution/0700-0799/0763.Partition Labels/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ tags:

<!-- description:start -->

<p>给你一个字符串 <code>s</code> 。我们要把这个字符串划分为尽可能多的片段,同一字母最多出现在一个片段中。</p>
<p>给你一个字符串 <code>s</code> 。我们要把这个字符串划分为尽可能多的片段,同一字母最多出现在一个片段中。例如,字符串&nbsp;<code>"ababcc"</code> 能够被分为 <code>["abab", "cc"]</code>,但类似&nbsp;<code>["aba", "bcc"]</code> 或&nbsp;<code>["ab", "ab", "cc"]</code> 的划分是非法的。</p>

<p>注意,划分结果需要满足:将所有划分结果按顺序连接,得到的字符串仍然是 <code>s</code> 。</p>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ tags:
<pre>
<strong>Input:</strong> nums1 = [1,3,5,4], nums2 = [1,2,3,7]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
<strong>Explanation:</strong>
Swap nums1[3] and nums2[3]. Then the sequences are:
nums1 = [1, 3, 5, 7] and nums2 = [1, 2, 3, 4]
which are both strictly increasing.
Expand Down
2 changes: 1 addition & 1 deletion solution/0800-0899/0850.Rectangle Area II/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ tags:

<!-- description:start -->

<p>给你一个轴对齐的二维数组&nbsp;<code>rectangles</code>&nbsp;。 对于&nbsp;<code>rectangle[i] = [x1, y1, x2, y2]</code>,其中(x1,y1)是矩形&nbsp;<code>i</code>&nbsp;左下角的坐标,<meta charset="UTF-8" />&nbsp;<code>(x<sub>i1</sub>, y<sub>i1</sub>)</code>&nbsp;是该矩形 <strong>左下角</strong> 的坐标,<meta charset="UTF-8" />&nbsp;<code>(x<sub>i2</sub>, y<sub>i2</sub>)</code>&nbsp;是该矩形&nbsp;<strong>右上角</strong> 的坐标。</p>
<p>给你一个轴对齐的二维数组&nbsp;<code>rectangles</code>&nbsp;。 对于&nbsp;<code>rectangle[i] = [x1, y1, x2, y2]</code>,其中&nbsp;<code>(x<sub>i1</sub>, y<sub>i1</sub>)</code>&nbsp;是该矩形 <strong>左下角</strong> 的坐标,<meta charset="UTF-8" />&nbsp;<code>(x<sub>i2</sub>, y<sub>i2</sub>)</code>&nbsp;是该矩形&nbsp;<strong>右上角</strong> 的坐标。</p>

<p>计算平面中所有&nbsp;<code>rectangles</code>&nbsp;所覆盖的 <strong>总面积 </strong>。任何被两个或多个矩形覆盖的区域应只计算 <strong>一次</strong> 。</p>

Expand Down
12 changes: 5 additions & 7 deletions solution/1400-1499/1408.String Matching in an Array/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,11 @@ tags:

<!-- description:start -->

<p>给你一个字符串数组 <code>words</code> ,数组中的每个字符串都可以看作是一个单词。请你按 <strong>任意</strong> 顺序返回 <code>words</code> 中是其他单词的子字符串的所有单词。</p>

<p>如果你可以删除 <code>words[j]</code>&nbsp;最左侧和/或最右侧的若干字符得到 <code>words[i]</code> ,那么字符串 <code>words[i]</code> 就是 <code>words[j]</code> 的一个子字符串。</p>
<p>给你一个字符串数组 <code>words</code> ,数组中的每个字符串都可以看作是一个单词。请你按 <strong>任意</strong> 顺序返回 <code>words</code> 中是其他单词的 <span data-keyword="substring-nonempty">子字符串</span> 的所有单词。</p>

<p>&nbsp;</p>

<p><strong>示例 1:</strong></p>
<p><strong class="example">示例 1:</strong></p>

<pre>
<strong>输入:</strong>words = ["mass","as","hero","superhero"]
Expand All @@ -35,15 +33,15 @@ tags:
["hero","as"] 也是有效的答案。
</pre>

<p><strong>示例 2:</strong></p>
<p><strong class="example">示例 2:</strong></p>

<pre>
<strong>输入:</strong>words = ["leetcode","et","code"]
<strong>输出:</strong>["et","code"]
<strong>解释:</strong>"et" 和 "code" 都是 "leetcode" 的子字符串。
</pre>

<p><strong>示例 3:</strong></p>
<p><strong class="example">示例 3:</strong></p>

<pre>
<strong>输入:</strong>words = ["blue","green","bu"]
Expand All @@ -58,7 +56,7 @@ tags:
<li><code>1 &lt;= words.length &lt;= 100</code></li>
<li><code>1 &lt;= words[i].length &lt;= 30</code></li>
<li><code>words[i]</code> 仅包含小写英文字母。</li>
<li>题目数据 <strong>保证</strong> 每个 <code>words[i]</code> 都是独一无二的。</li>
<li>题目数据 <strong>保证</strong> <code>words</code>&nbsp;的每个字符串都是独一无二的。</li>
</ul>

<!-- description:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ tags:

<!-- description:start -->

<p>Given an integer <code>n</code>, find a sequence that satisfies all of the following:</p>
<p>Given an integer <code>n</code>, find a sequence with elements in the range <code>[1, n]</code> that satisfies all of the following:</p>

<ul>
<li>The integer <code>1</code> occurs once in the sequence.</li>
Expand Down
6 changes: 6 additions & 0 deletions solution/1700-1799/1730.Shortest Path to Get Food/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ tags:
<strong>输出:</strong> 6
<strong>解释:</strong> 这里有多个食物。拿到下边的食物仅需走 6 步。</pre>

<p><strong class="example">示例 4:</strong></p>

<pre>
<b>输入:</b>grid = [["X","X","X","X","X","X","X","X"],["X","*","O","X","O","#","O","X"],["X","O","O","X","O","O","X","X"],["X","O","O","O","O","#","O","X"],["O","O","O","O","O","O","O","O"]]
<b>输出:</b>5</pre>

<p>&nbsp;</p>

<p><b>提示:</b></p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ tags:

<p>源数组中可能存在 <strong>重复项</strong> 。</p>

<p><strong>注意:</strong>我们称数组 <code>A</code> 在轮转 <code>x</code> 个位置后得到长度相同的数组 <code>B</code> ,当它们满足 <code>A[i] == B[(i+x) % A.length]</code> ,其中 <code>%</code> 为取余运算。</p>
<p><strong>注意:</strong>数组 <code>A</code> 在轮转 <code>x</code> 个位置后得到长度相同的数组 <code>B</code> ,使得对于每一个有效的下标&nbsp;<code>i</code>,满足&nbsp;<code>B[i] == A[(i+x) % A.length]</code>。</p>

<p>&nbsp;</p>

Expand Down
19 changes: 5 additions & 14 deletions solution/1800-1899/1800.Maximum Ascending Subarray Sum/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@ tags:

<!-- description:start -->

<p>给你一个正整数组成的数组 <code>nums</code> ,返回 <code>nums</code> 中一个 <strong>升序 </strong>子数组的最大可能元素和。</p>
<p>给你一个正整数组成的数组 <code>nums</code> ,返回 <code>nums</code> 中一个 <span data-keyword="strictly-increasing-array">严格递增子数组</span> 的最大可能元素和。</p>

<p>子数组是数组中的一个连续数字序列。</p>

<p>已知子数组 <code>[nums<sub>l</sub>, nums<sub>l+1</sub>, ..., nums<sub>r-1</sub>, nums<sub>r</sub>]</code> ,若对所有 <code>i</code>(<code>l <= i < r</code>),<code>nums<sub>i </sub> < nums<sub>i+1</sub></code> 都成立,则称这一子数组为 <strong>升序</strong> 子数组。注意,大小为 <code>1</code> 的子数组也视作 <strong>升序</strong> 子数组。</p>

<p> </p>
<p>&nbsp;</p>

<p><strong>示例 1:</strong></p>

Expand All @@ -50,20 +48,13 @@ tags:
<strong>解释:</strong>[10,11,12] 是元素和最大的升序子数组,最大元素和为 33 。
</pre>

<p><strong>示例 4:</strong></p>

<pre>
<strong>输入:</strong>nums = [100,10,1]
<strong>输出:</strong>100
</pre>

<p> </p>
<p>&nbsp;</p>

<p><strong>提示:</strong></p>

<ul>
<li><code>1 <= nums.length <= 100</code></li>
<li><code>1 <= nums[i] <= 100</code></li>
<li><code>1 &lt;= nums.length &lt;= 100</code></li>
<li><code>1 &lt;= nums[i] &lt;= 100</code></li>
</ul>

<!-- description:end -->
Expand Down
24 changes: 13 additions & 11 deletions solution/1800-1899/1852.Distinct Numbers in Each Subarray/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ tags:

<!-- description:start -->

<p>给你一个整数数组&nbsp;<code>nums</code>与一个整数 <code>k</code>,请你构造一个长度 <code>n-k+1</code> 的数组 <code>ans</code>,这个数组第<code>i</code>个元素 <code>ans[i]</code> 是每个长度为k的子数组 <code>nums[i:i+k-1] = [nums[i], nums[i+1], ..., nums[i+k-1]]</code>中数字的种类数。</p>
<p>给你一个长度为&nbsp;<code>n</code>&nbsp;的整数数组&nbsp;<code>nums</code>&nbsp;与一个整数 <code>k</code>。你的任务是找到&nbsp;<code>nums</code>&nbsp;<strong>所有</strong>&nbsp;长度为&nbsp;<code>k</code>&nbsp;的子数组中&nbsp;<strong>不同</strong>&nbsp;元素的数量。</p>

<p>返回这个数组 <code>ans</code>。</p>
<p>返回一个数组 <code>ans</code>,其中&nbsp;<code>ans[i]</code>&nbsp;是对于每个索引&nbsp;<code>0 &lt;= i &lt; n - k</code>,<code>nums[i..(i + k - 1)]</code>&nbsp;中不同元素的数量。</p>

<p>&nbsp;</p>

<p>&nbsp;</p>

Expand All @@ -30,11 +32,11 @@ tags:
<strong>输入:</strong> nums = [1,2,3,2,2,1,3], k = 3
<strong>输出:</strong> [3,2,2,2,3]
<b>解释</b>:每个子数组的数字种类计算方法如下:
- nums[0:2] = [1,2,3] 所以 ans[0] = 3
- nums[1:3] = [2,3,2] 所以 ans[1] = 2
- nums[2:4] = [3,2,2] 所以 ans[2] = 2
- nums[3:5] = [2,2,1] 所以 ans[3] = 2
- nums[4:6] = [2,1,3] 所以 ans[4] = 3
- nums[0..2] = [1,2,3] 所以 ans[0] = 3
- nums[1..3] = [2,3,2] 所以 ans[1] = 2
- nums[2..4] = [3,2,2] 所以 ans[2] = 2
- nums[3..5] = [2,2,1] 所以 ans[3] = 2
- nums[4..6] = [2,1,3] 所以 ans[4] = 3
</pre>

<p><strong>示例&nbsp;2:</strong></p>
Expand All @@ -43,10 +45,10 @@ tags:
<strong>输入:</strong> nums = [1,1,1,1,2,3,4], k = 4
<strong>输出:</strong> [1,2,3,4]
<strong>解释: </strong>每个子数组的数字种类计算方法如下:
- nums[0:3] = [1,1,1,1] 所以 ans[0] = 1
- nums[1:4] = [1,1,1,2] 所以 ans[1] = 2
- nums[2:5] = [1,1,2,3] 所以 ans[2] = 3
- nums[3:6] = [1,2,3,4] 所以 ans[3] = 4
- nums[0..3] = [1,1,1,1] 所以 ans[0] = 1
- nums[1..4] = [1,1,1,2] 所以 ans[1] = 2
- nums[2..5] = [1,1,2,3] 所以 ans[2] = 3
- nums[3..6] = [1,2,3,4] 所以 ans[3] = 4
</pre>

<p>&nbsp;</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,15 @@ tags:

我们观察发现,对于第 $i$ 个人来说,他能看到的人一定是按从左到右高度严格单调递增的。

因此,我们可以倒序遍历数组 $heights$,用一个从栈顶到栈底单调递增的栈 $stk$ 记录已经遍历过的人的高度。
因此,我们可以倒序遍历数组 $\textit{heights}$,用一个从栈顶到栈底单调递增的栈 $\textit{stk}$ 记录已经遍历过的人的高度。

对于第 $i$ 个人,如果栈不为空并且栈顶元素小于 $heights[i]$,累加当前第 $i$ 个人能看到的人数,然后将栈顶元素出栈,直到栈为空或者栈顶元素大于等于 $heights[i]$。如果此时栈不为空,说明栈顶元素大于等于 $heights[i]$,那么第 $i$ 个人能看到的人数还要再加 $1$。
对于第 $i$ 个人,如果栈不为空并且栈顶元素小于 $\textit{heights}[i]$,累加当前第 $i$ 个人能看到的人数,然后将栈顶元素出栈,直到栈为空或者栈顶元素大于等于 $\textit{heights}[i]$。如果此时栈不为空,说明栈顶元素大于等于 $\textit{heights}[i]$,那么第 $i$ 个人能看到的人数还要再加 $1$。

接下来,我们将 $heights[i]$ 入栈,继续遍历下一个人。
接下来,我们将 $\textit{heights}[i]$ 入栈,继续遍历下一个人。

遍历结束后,返回答案数组 $ans$。
遍历结束后,返回答案数组 $\textit{ans}$。

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 $heights$ 的长度。
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 $\textit{heights}$ 的长度。

相似题目:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,19 @@ Person 5 can see no one since nobody is to the right of them.

<!-- solution:start -->

### Solution 1
### Solution 1: Monotonic Stack

We observe that for the $i$-th person, the people he can see must be strictly increasing in height from left to right.

Therefore, we can traverse the array $\textit{heights}$ in reverse order, using a stack $\textit{stk}$ that is monotonically increasing from top to bottom to record the heights of the people we have traversed.

For the $i$-th person, if the stack is not empty and the top element of the stack is less than $\textit{heights}[i]$, we increment the count of people the $i$-th person can see, then pop the top element of the stack, until the stack is empty or the top element of the stack is greater than or equal to $\textit{heights}[i]$. If the stack is not empty at this point, it means the top element of the stack is greater than or equal to $\textit{heights}[i]$, so we increment the count of people the $i$-th person can see by 1.

Next, we push $\textit{heights}[i]$ onto the stack and continue to the next person.

After traversing, we return the answer array $\textit{ans}$.

The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $\textit{heights}$.

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,15 @@ class Solution {
public:
int getLucky(string s, int k) {
string t;
for (char c : s) t += to_string(c - 'a' + 1);
for (char c : s) {
t += to_string(c - 'a' + 1);
}
s = t;
while (k--) {
int t = 0;
for (char c : s) t += c - '0';
for (char c : s) {
t += c - '0';
}
s = to_string(t);
}
return stoi(s);
Expand Down
Loading
Loading