Skip to content

Commit ac96dab

Browse files
committed
feat: add new lc problems
1 parent d940833 commit ac96dab

File tree

84 files changed

+5626
-52
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+5626
-52
lines changed

solution/0000-0099/0005.Longest Palindromic Substring/README_EN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@
3636

3737
Set $dp[i][j]$ to indicate whether the string $s[i..j]$ is a palindrome.
3838

39-
- When $j - i \lt 2$, that is, the string length is `2`, as long as $s[i] == s[j]$, then $dp[i][j]$ is `true`.
40-
- When $j - i \ge 2$, there is $dp[i][j] = dp[i + 1][j - 1] \cap s[i] == s[j]$.
39+
- When $j - i \lt 2$, that is, the string length is `2`, as long as $s[i] == s[j]$, then $dp[i][j]$ is `true`.
40+
- When $j - i \ge 2$, there is $dp[i][j] = dp[i + 1][j - 1] \cap s[i] == s[j]$.
4141

4242
The time complexity is $O(n^2)$ and the space complexity is $O(n^2)$. Where $n$ is the length of the string $s$.
4343

solution/0000-0099/0026.Remove Duplicates from Sorted Array/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<p>Given an integer array <code>nums</code> sorted in <strong>non-decreasing order</strong>, remove the duplicates <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank"><strong>in-place</strong></a> such that each unique element appears only <strong>once</strong>. The <strong>relative order</strong> of the elements should be kept the <strong>same</strong>. Then return <em>the number of unique elements in </em><code>nums</code>.</p>
88

9-
<p>Consider the number of unique elements of <code>nums</code> be <code>k</code>, to get accepted, you need to do the following things:</p>
9+
<p>Consider the number of unique elements of <code>nums</code> to be <code>k</code>, to get accepted, you need to do the following things:</p>
1010

1111
<ul>
1212
<li>Change the array <code>nums</code> such that the first <code>k</code> elements of <code>nums</code> contain the unique elements in the order they were present in <code>nums</code> initially. The remaining elements of <code>nums</code> are not important as well as the size of <code>nums</code>.</li>

solution/0000-0099/0039.Combination Sum/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class Solution:
8888
t.append(candidates[i])
8989
dfs(i, s - candidates[i])
9090
t.pop()
91-
91+
9292
candidates.sort()
9393
t = []
9494
ans = []

solution/0000-0099/0077.Combinations/README_EN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ public class Solution {
355355
private List<int> t = new List<int>();
356356
private int n;
357357
private int k;
358-
358+
359359
public IList<IList<int>> Combine(int n, int k) {
360360
this.n = n;
361361
this.k = k;
@@ -385,7 +385,7 @@ public class Solution {
385385
private List<int> t = new List<int>();
386386
private int n;
387387
private int k;
388-
388+
389389
public IList<IList<int>> Combine(int n, int k) {
390390
this.n = n;
391391
this.k = k;

solution/0500-0599/0581.Shortest Unsorted Continuous Subarray/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
## Description
66

7-
<p>Given an integer array <code>nums</code>, you need to find one <b>continuous subarray</b> that if you only sort this subarray in ascending order, then the whole array will be sorted in ascending order.</p>
7+
<p>Given an integer array <code>nums</code>, you need to find one <b>continuous subarray</b> such that if you only sort this subarray in non-decreasing order, then the whole array will be sorted in non-decreasing order.</p>
88

99
<p>Return <em>the shortest such subarray and output its length</em>.</p>
1010

solution/1000-1099/1023.Camelcase Matching/README.md

+13-10
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,31 @@
1414

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

17-
<pre><strong>输入:</strong>queries = [&quot;FooBar&quot;,&quot;FooBarTest&quot;,&quot;FootBall&quot;,&quot;FrameBuffer&quot;,&quot;ForceFeedBack&quot;], pattern = &quot;FB&quot;
17+
<pre>
18+
<strong>输入:</strong>queries = ["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"], pattern = "FB"
1819
<strong>输出:</strong>[true,false,true,true,false]
1920
<strong>示例:</strong>
20-
&quot;FooBar&quot; 可以这样生成:&quot;F&quot; + &quot;oo&quot; + &quot;B&quot; + &quot;ar&quot;
21-
&quot;FootBall&quot; 可以这样生成:&quot;F&quot; + &quot;oot&quot; + &quot;B&quot; + &quot;all&quot;.
22-
&quot;FrameBuffer&quot; 可以这样生成:&quot;F&quot; + &quot;rame&quot; + &quot;B&quot; + &quot;uffer&quot;.</pre>
21+
"FooBar" 可以这样生成:"F" + "oo" + "B" + "ar"
22+
"FootBall" 可以这样生成:"F" + "oot" + "B" + "all".
23+
"FrameBuffer" 可以这样生成:"F" + "rame" + "B" + "uffer".</pre>
2324

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

26-
<pre><strong>输入:</strong>queries = [&quot;FooBar&quot;,&quot;FooBarTest&quot;,&quot;FootBall&quot;,&quot;FrameBuffer&quot;,&quot;ForceFeedBack&quot;], pattern = &quot;FoBa&quot;
27+
<pre>
28+
<strong>输入:</strong>queries = ["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"], pattern = "FoBa"
2729
<strong>输出:</strong>[true,false,true,false,false]
2830
<strong>解释:</strong>
29-
&quot;FooBar&quot; 可以这样生成:&quot;Fo&quot; + &quot;o&quot; + &quot;Ba&quot; + &quot;r&quot;.
30-
&quot;FootBall&quot; 可以这样生成:&quot;Fo&quot; + &quot;ot&quot; + &quot;Ba&quot; + &quot;ll&quot;.
31+
"FooBar" 可以这样生成:"Fo" + "o" + "Ba" + "r".
32+
"FootBall" 可以这样生成:"Fo" + "ot" + "Ba" + "ll".
3133
</pre>
3234

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

35-
<pre><strong>输出:</strong>queries = [&quot;FooBar&quot;,&quot;FooBarTest&quot;,&quot;FootBall&quot;,&quot;FrameBuffer&quot;,&quot;ForceFeedBack&quot;], pattern = &quot;FoBaT&quot;
36-
<strong>输入:</strong>[false,true,false,false,false]
37+
<pre>
38+
<strong>输入:</strong>queries = ["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"], pattern = "FoBaT"
39+
<strong>输出:</strong>[false,true,false,false,false]
3740
<strong>解释: </strong>
38-
&quot;FooBarTest&quot; 可以这样生成:&quot;Fo&quot; + &quot;o&quot; + &quot;Ba&quot; + &quot;r&quot; + &quot;T&quot; + &quot;est&quot;.
41+
"FooBarTest" 可以这样生成:"Fo" + "o" + "Ba" + "r" + "T" + "est".
3942
</pre>
4043

4144
<p>&nbsp;</p>

solution/1100-1199/1157.Online Majority Element In Subarray/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ majorityChecker.query(2,3,2); // 返回 2
5454

5555
**方法一:线段树 + 摩尔投票 + 二分查找**
5656

57-
我们注意到,题目需要我们找出特定区间内可能的众数,我们可以用线段树来维护每个区间内的候选众数以及其出现的次数
57+
我们注意到,题目需要我们找出特定区间内可能的众数,考虑使用线段树来维护每个区间内的候选众数以及其出现的次数
5858

5959
我们定义线段树的每个节点为 `Node`,每个节点包含如下属性:
6060

solution/1500-1599/1594.Maximum Non Negative Product in a Matrix/README.md

+18-28
Original file line numberDiff line numberDiff line change
@@ -6,57 +6,47 @@
66

77
<!-- 这里写题目描述 -->
88

9-
<p>给你一个大小为 <code>rows x cols</code> 的矩阵 <code>grid</code> 。最初,你位于左上角 <code>(0, 0)</code> ,每一步,你可以在矩阵中 <strong>向右</strong> 或 <strong>向下</strong> 移动。</p>
9+
<p>给你一个大小为 <code>m x n</code> 的矩阵 <code>grid</code> 。最初,你位于左上角 <code>(0, 0)</code> ,每一步,你可以在矩阵中 <strong>向右</strong> 或 <strong>向下</strong> 移动。</p>
1010

11-
<p>在从左上角 <code>(0, 0)</code> 开始到右下角 <code>(rows - 1, cols - 1)</code> 结束的所有路径中,找出具有 <strong>最大非负积</strong> 的路径。路径的积是沿路径访问的单元格中所有整数的乘积。</p>
11+
<p>在从左上角 <code>(0, 0)</code> 开始到右下角 <code>(m - 1, n - 1)</code> 结束的所有路径中,找出具有 <strong>最大非负积</strong> 的路径。路径的积是沿路径访问的单元格中所有整数的乘积。</p>
1212

13-
<p>返回 <strong>最大非负积 </strong>对<strong><em> </em><code>10<sup>9</sup>&nbsp;+ 7</code></strong> <strong>取余</strong> 的结果。如果最大积为负数,则返回<em> </em><code>-1</code> 。</p>
13+
<p>返回 <strong>最大非负积 </strong>对<strong><em> </em><code>10<sup>9</sup>&nbsp;+ 7</code></strong> <strong>取余</strong> 的结果。如果最大积为 <strong>负数</strong> ,则返回<em> </em><code>-1</code> 。</p>
1414

1515
<p><strong>注意,</strong>取余是在得到最大积之后执行的。</p>
1616

1717
<p>&nbsp;</p>
1818

1919
<p><strong>示例 1:</strong></p>
20-
21-
<pre><strong>输入:</strong>grid = [[-1,-2,-3],
22-
&nbsp; [-2,-3,-3],
23-
&nbsp; [-3,-3,-2]]
20+
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1500-1599/1594.Maximum%20Non%20Negative%20Product%20in%20a%20Matrix/images/product1.jpg" style="width: 244px; height: 245px;" />
21+
<pre>
22+
<strong>输入:</strong>grid = [[-1,-2,-3],[-2,-3,-3],[-3,-3,-2]]
2423
<strong>输出:</strong>-1
25-
<strong>解释:</strong>从 (0, 0) 到 (2, 2) 的路径中无法得到非负积,所以返回 -1
26-
</pre>
24+
<strong>解释:</strong>从 (0, 0) 到 (2, 2) 的路径中无法得到非负积,所以返回 -1 。</pre>
2725

2826
<p><strong>示例 2:</strong></p>
29-
30-
<pre><strong>输入:</strong>grid = [[<strong>1</strong>,-2,1],
31-
&nbsp; [<strong>1</strong>,<strong>-2</strong>,1],
32-
&nbsp; [3,<strong>-4</strong>,<strong>1</strong>]]
27+
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1500-1599/1594.Maximum%20Non%20Negative%20Product%20in%20a%20Matrix/images/product2.jpg" style="width: 244px; height: 245px;" />
28+
<pre>
29+
<strong>输入:</strong>grid = [[1,-2,1],[1,-2,1],[3,-4,1]]
3330
<strong>输出:</strong>8
34-
<strong>解释:</strong>最大非负积对应的路径已经用粗体标出 (1 * 1 * -2 * -4 * 1 = 8)
31+
<strong>解释:</strong>最大非负积对应的路径如图所示 (1 * 1 * -2 * -4 * 1 = 8)
3532
</pre>
3633

3734
<p><strong>示例 3:</strong></p>
38-
39-
<pre><strong>输入:</strong>grid = [[<strong>1</strong>, 3],
40-
&nbsp; [<strong>0</strong>,<strong>-4</strong>]]
35+
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1500-1599/1594.Maximum%20Non%20Negative%20Product%20in%20a%20Matrix/images/product3.jpg" style="width: 164px; height: 165px;" />
36+
<pre>
37+
<strong>输入:</strong>grid = [[1,3],[0,-4]]
4138
<strong>输出:</strong>0
42-
<strong>解释:</strong>最大非负积对应的路径已经用粗体标出 (1 * 0 * -4 = 0)
43-
</pre>
44-
45-
<p><strong>示例 4:</strong></p>
46-
47-
<pre><strong>输入:</strong>grid = [[ <strong>1</strong>, 4,4,0],
48-
&nbsp; [<strong>-2</strong>, 0,0,1],
49-
&nbsp; [ <strong>1</strong>,<strong>-1</strong>,<strong>1</strong>,<strong>1</strong>]]
50-
<strong>输出:</strong>2
51-
<strong>解释:</strong>最大非负积对应的路径已经用粗体标出 (1 * -2 * 1 * -1 * 1 * 1 = 2)
39+
<strong>解释:</strong>最大非负积对应的路径如图所示 (1 * 0 * -4 = 0)
5240
</pre>
5341

5442
<p>&nbsp;</p>
5543

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

5846
<ul>
59-
<li><code>1 &lt;= rows, cols &lt;= 15</code></li>
47+
<li><code>m == grid.length</code></li>
48+
<li><code>n == grid[i].length</code></li>
49+
<li><code>1 &lt;= m, n &lt;= 15</code></li>
6050
<li><code>-4 &lt;= grid[i][j] &lt;= 4</code></li>
6151
</ul>
6252

solution/2500-2599/2581.Count Number of Possible Root Nodes/README_EN.md

-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ Considering any node as root will give at least 1 correct guess.
7272

7373
## Solutions
7474

75-
7675
**Approach 1: Tree DP (change root)**
7776

7877
First, we traverse the given edge set $edges$ and convert it to an adjacency list $g$ where $g[i]$ represents the adjacent nodes of node $i$. Use a hash map $gs$ to record the given guess set $guesses$.

solution/2600-2699/2605.Form Smallest Number From Two Digit Arrays/README_EN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Given two arrays of <strong>unique</strong> digits <code>nums1</code> and <code>
3434

3535
## Solutions
3636

37-
**Approach 1: Enumeration**
37+
**Approach 1: Enumeration**
3838

3939
We observe that if there are the same numbers in the arrays $nums1$ and $nums2$, then the minimum of the same numbers is the smallest number. Otherwise, we take the number $a$ in the array $nums1$ and the number $b$ in the array $nums2$, and concatenate the two numbers $a$ and $b$ into two numbers, and take the smaller number.
4040

@@ -54,7 +54,7 @@ If the number $mask$ obtained by performing a bitwise AND operation on $mask1$ a
5454

5555
Otherwise, we extract the position of the last $1$ in $mask1$ and $mask2$ respectively, and denote them as $a$ and $b$, respectively. Then the smallest number is $min(a \times 10 + b, b \times 10 + a)$.
5656

57-
The time complexity is $O(m + n)$, and the space complexity is $O(1)$. Where $m$ and $n$ are the lengths of the arrays $nums1$ and $nums2$ respectively.
57+
The time complexity is $O(m + n)$, and the space complexity is $O(1)$. Where $m$ and $n$ are the lengths of the arrays $nums1$ and $nums2$ respectively.
5858

5959
<!-- tabs:start -->
6060

solution/2600-2699/2612.Minimum Reverse Operations/README_EN.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ Therefore, for a specific index $i$, all its flipped indices form an arithmetic
7070

7171
Next, we consider the range of values ​​of the index $i$ after flipping $j$.
7272

73-
- If the boundary is not considered, the range of values ​​of $j$ is $[i - k + 1, i + k - 1]$.
74-
- If the subarray is on the left, then $[l, r] = [0, k - 1]$, so the flipped index $j$ of $i$ is $0 + k - 1 - i$, that is, $j = k - i - 1$, so the left boundary $mi = max(i - k + 1, k - i - 1)$.
75-
- If the subarray is on the right, then $[l, r] = [n - k, n - 1]$, so the flipped index $j= n - k + n - 1 - i$ is $j = n \times 2 - k - i - 1$, so the right boundary of $j$ is $mx = min(i + k - 1, n \times 2 - k - i - 1)$.
73+
- If the boundary is not considered, the range of values ​​of $j$ is $[i - k + 1, i + k - 1]$.
74+
- If the subarray is on the left, then $[l, r] = [0, k - 1]$, so the flipped index $j$ of $i$ is $0 + k - 1 - i$, that is, $j = k - i - 1$, so the left boundary $mi = max(i - k + 1, k - i - 1)$.
75+
- If the subarray is on the right, then $[l, r] = [n - k, n - 1]$, so the flipped index $j= n - k + n - 1 - i$ is $j = n \times 2 - k - i - 1$, so the right boundary of $j$ is $mx = min(i + k - 1, n \times 2 - k - i - 1)$.
7676

7777
We use two ordered sets to store all the odd indices and even indices to be searched, here we need to exclude the indices in the array $banned$ and the index $p$.
7878

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# [2618. 检查是否是类的对象实例](https://leetcode.cn/problems/check-if-object-instance-of-class)
2+
3+
[English Version](/solution/2600-2699/2618.Check%20if%20Object%20Instance%20of%20Class/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>请你编写一个函数,检查给定的对象是否是给定类或超类的实例。</p>
10+
11+
<p>可以传递给函数的数据类型没有限制。</p>
12+
13+
<p>&nbsp;</p>
14+
15+
<p><strong>示例 1:</strong></p>
16+
17+
<pre>
18+
<b>输入:</b>func = () =&gt; checkIfInstance(new Date(), Date)
19+
<b>输出:</b>true
20+
<strong>解释:</strong>根据定义,Date 构造函数返回的对象是 Date 的一个实例。
21+
</pre>
22+
23+
<p><strong>示例 2:</strong></p>
24+
25+
<pre>
26+
<b>输入:</b>func = () =&gt; { class Animal {}; class Dog extends Animal {}; return checkIfInstance(new Dog(), Animal); }
27+
<b>输出:</b>true
28+
<strong>解释:</strong>
29+
class Animal {};
30+
class Dog extends Animal {};
31+
checkIfInstance(new Dog(), Animal); // true
32+
33+
Dog 是 Animal 的子类。因此,Dog 对象同时是 Dog 和 Animal 的实例。</pre>
34+
35+
<p><strong>示例 3:</strong></p>
36+
37+
<pre>
38+
<b>输入:</b>func = () =&gt; checkIfInstance(Date, Date)
39+
<b>输出:</b>false
40+
<strong>解释:</strong>日期的构造函数在逻辑上不能是其自身的实例。
41+
</pre>
42+
43+
<p><strong>示例 4:</strong></p>
44+
45+
<pre>
46+
<b>输入:</b>func = () =&gt; checkIfInstance(5, Number)
47+
<b>输出:</b>true
48+
<strong>解释:</strong>5 是一个 Number。注意,"instanceof" 关键字将返回 false。</pre>
49+
50+
## 解法
51+
52+
<!-- 这里可写通用的实现逻辑 -->
53+
54+
<!-- tabs:start -->
55+
56+
### **TypeScript**
57+
58+
<!-- 这里可写当前语言的特殊实现逻辑 -->
59+
60+
```ts
61+
62+
```
63+
64+
### **...**
65+
66+
```
67+
68+
```
69+
70+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# [2618. Check if Object Instance of Class](https://leetcode.com/problems/check-if-object-instance-of-class)
2+
3+
[中文文档](/solution/2600-2699/2618.Check%20if%20Object%20Instance%20of%20Class/README.md)
4+
5+
## Description
6+
7+
<p>Write a function that checks if a given object is an instance of a given class or superclass.</p>
8+
9+
<p>There are&nbsp;no constraints on the data types that can be passed to the function.</p>
10+
11+
<p>&nbsp;</p>
12+
<p><strong class="example">Example 1:</strong></p>
13+
14+
<pre>
15+
<strong>Input:</strong> func = () =&gt; checkIfInstance(new Date(), Date)
16+
<strong>Output:</strong> true
17+
<strong>Explanation: </strong>The object returned by the Date constructor is, by definition, an instance of Date.
18+
</pre>
19+
20+
<p><strong class="example">Example 2:</strong></p>
21+
22+
<pre>
23+
<strong>Input:</strong> func = () =&gt; { class Animal {}; class Dog extends Animal {}; return checkIfInstance(new Dog(), Animal); }
24+
<strong>Output:</strong> true
25+
<strong>Explanation:</strong>
26+
class Animal {};
27+
class Dog extends Animal {};
28+
checkIfInstance(new Dog(), Animal); // true
29+
30+
Dog is a subclass of Animal. Therefore, a Dog object is an instance of both Dog and Animal.</pre>
31+
32+
<p><strong class="example">Example 3:</strong></p>
33+
34+
<pre>
35+
<strong>Input:</strong> func = () =&gt; checkIfInstance(Date, Date)
36+
<strong>Output:</strong> false
37+
<strong>Explanation: </strong>A date constructor cannot logically be an instance of itself.
38+
</pre>
39+
40+
<p><strong class="example">Example 4:</strong></p>
41+
42+
<pre>
43+
<strong>Input:</strong> func = () =&gt; checkIfInstance(5, Number)
44+
<strong>Output:</strong> true
45+
<strong>Explanation: </strong>5 is a Number. Note that the &quot;instanceof&quot; keyword would return false.
46+
</pre>
47+
48+
## Solutions
49+
50+
<!-- tabs:start -->
51+
52+
### **TypeScript**
53+
54+
```ts
55+
56+
```
57+
58+
### **...**
59+
60+
```
61+
62+
```
63+
64+
<!-- tabs:end -->

0 commit comments

Comments
 (0)