Skip to content

Commit bec7ed2

Browse files
committed
feat: add new lc problems
1 parent b7b8688 commit bec7ed2

File tree

21 files changed

+987
-204
lines changed

21 files changed

+987
-204
lines changed

solution/1300-1399/1381.Design a Stack With Increment Operation/README.md

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66

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

9-
<p>请你设计一个支持下述操作的栈。</p>
9+
<p>请你设计一个支持对其元素进行增量操作的栈。</p>
1010

1111
<p>实现自定义栈类 <code>CustomStack</code> :</p>
1212

1313
<ul>
14-
<li><code>CustomStack(int maxSize)</code>:用 <code>maxSize</code> 初始化对象,<code>maxSize</code> 是栈中最多能容纳的元素数量,栈在增长到 <code>maxSize</code> 之后则不支持 <code>push</code> 操作。</li>
14+
<li><code>CustomStack(int maxSize)</code>:用 <code>maxSize</code> 初始化对象,<code>maxSize</code> 是栈中最多能容纳的元素数量。</li>
1515
<li><code>void push(int x)</code>:如果栈还未增长到 <code>maxSize</code> ,就将 <code>x</code> 添加到栈顶。</li>
1616
<li><code>int pop()</code>:弹出栈顶元素,并返回栈顶的值,或栈为空时返回 <strong>-1</strong> 。</li>
1717
<li><code>void inc(int k, int val)</code>:栈底的 <code>k</code> 个元素的值都增加 <code>val</code> 。如果栈中元素总数小于 <code>k</code> ,则栈中的所有元素都增加 <code>val</code> 。</li>
@@ -21,35 +21,34 @@
2121

2222
<p><strong>示例:</strong></p>
2323

24-
<pre><strong>输入:</strong>
25-
[&quot;CustomStack&quot;,&quot;push&quot;,&quot;push&quot;,&quot;pop&quot;,&quot;push&quot;,&quot;push&quot;,&quot;push&quot;,&quot;increment&quot;,&quot;increment&quot;,&quot;pop&quot;,&quot;pop&quot;,&quot;pop&quot;,&quot;pop&quot;]
24+
<pre>
25+
<strong>输入:</strong>
26+
["CustomStack","push","push","pop","push","push","push","increment","increment","pop","pop","pop","pop"]
2627
[[3],[1],[2],[],[2],[3],[4],[5,100],[2,100],[],[],[],[]]
2728
<strong>输出:</strong>
2829
[null,null,null,2,null,null,null,null,null,103,202,201,-1]
2930
<strong>解释:</strong>
30-
CustomStack customStack = new CustomStack(3); // 栈是空的 []
31-
customStack.push(1); // 栈变为 [1]
32-
customStack.push(2); // 栈变为 [1, 2]
33-
customStack.pop(); // 返回 2 --&gt; 返回栈顶值 2,栈变为 [1]
34-
customStack.push(2); // 栈变为 [1, 2]
35-
customStack.push(3); // 栈变为 [1, 2, 3]
36-
customStack.push(4); // 栈仍然是 [1, 2, 3],不能添加其他元素使栈大小变为 4
37-
customStack.increment(5, 100); // 栈变为 [101, 102, 103]
38-
customStack.increment(2, 100); // 栈变为 [201, 202, 103]
39-
customStack.pop(); // 返回 103 --&gt; 返回栈顶值 103,栈变为 [201, 202]
40-
customStack.pop(); // 返回 202 --&gt; 返回栈顶值 202,栈变为 [201]
41-
customStack.pop(); // 返回 201 --&gt; 返回栈顶值 201,栈变为 []
42-
customStack.pop(); // 返回 -1 --&gt; 栈为空,返回 -1
31+
CustomStack stk = new CustomStack(3); // 栈是空的 []
32+
stk.push(1); // 栈变为 [1]
33+
stk.push(2); // 栈变为 [1, 2]
34+
stk.pop(); // 返回 2 --&gt; 返回栈顶值 2,栈变为 [1]
35+
stk.push(2); // 栈变为 [1, 2]
36+
stk.push(3); // 栈变为 [1, 2, 3]
37+
stk.push(4); // 栈仍然是 [1, 2, 3],不能添加其他元素使栈大小变为 4
38+
stk.increment(5, 100); // 栈变为 [101, 102, 103]
39+
stk.increment(2, 100); // 栈变为 [201, 202, 103]
40+
stk.pop(); // 返回 103 --&gt; 返回栈顶值 103,栈变为 [201, 202]
41+
stk.pop(); // 返回 202 --&gt; 返回栈顶值 202,栈变为 [201]
42+
stk.pop(); // 返回 201 --&gt; 返回栈顶值 201,栈变为 []
43+
stk.pop(); // 返回 -1 --&gt; 栈为空,返回 -1
4344
</pre>
4445

4546
<p>&nbsp;</p>
4647

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

4950
<ul>
50-
<li><code>1 &lt;= maxSize &lt;= 1000</code></li>
51-
<li><code>1 &lt;= x &lt;= 1000</code></li>
52-
<li><code>1 &lt;= k &lt;= 1000</code></li>
51+
<li><code>1 &lt;= maxSize, x, k &lt;= 1000</code></li>
5352
<li><code>0 &lt;= val &lt;= 100</code></li>
5453
<li>每种方法 <code>increment</code>,<code>push</code> 以及 <code>pop</code> 分别最多调用 <code>1000</code> 次</li>
5554
</ul>

solution/2200-2299/2204.Distance to a Cycle in Undirected Graph/README_EN.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<p><strong class="example">Example 1:</strong></p>
1717
<img src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2200-2299/2204.Distance%20to%20a%20Cycle%20in%20Undirected%20Graph/images/image-20220315154238-1.png" style="width: 350px; height: 237px;" />
1818
<pre>
19-
<strong>Input:</strong> n = 7, edges = [[1,2],[2,3],[3,4],[4,1],[0,1],[5,2],[6,5]]
19+
<strong>Input:</strong> n = 7, edges = [[1,2],[2,4],[4,3],[3,1],[0,1],[5,2],[6,5]]
2020
<strong>Output:</strong> [1,0,0,0,0,1,2]
2121
<strong>Explanation:</strong>
2222
The nodes 1, 2, 3, and 4 form the cycle.
@@ -32,7 +32,7 @@ The distance from 6 to 2 is 2.
3232
<p><strong class="example">Example 2:</strong></p>
3333
<img src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2200-2299/2204.Distance%20to%20a%20Cycle%20in%20Undirected%20Graph/images/image-20220315154634-1.png" style="width: 400px; height: 297px;" />
3434
<pre>
35-
<strong>Input:</strong> n = 9, edges = [[0,1],[1,2],[0,2],[2,6],[6,7],[6,8],[1,3],[3,4],[3,5]]
35+
<strong>Input:</strong> n = 9, edges = [[0,1],[1,2],[0,2],[2,6],[6,7],[6,8],[0,3],[3,4],[3,5]]
3636
<strong>Output:</strong> [0,0,0,1,2,2,1,2,2]
3737
<strong>Explanation:</strong>
3838
The nodes 0, 1, and 2 form the cycle.
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# [2506. 统计相似字符串对的数目](https://leetcode.cn/problems/count-pairs-of-similar-strings)
2+
3+
[English Version](/solution/2500-2599/2506.Count%20Pairs%20Of%20Similar%20Strings/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个下标从 <strong>0</strong> 开始的字符串数组 <code>words</code> 。</p>
10+
11+
<p>如果两个字符串由相同的字符组成,则认为这两个字符串 <strong>相似</strong> 。</p>
12+
13+
<ul>
14+
<li>例如,<code>"abca"</code> 和 <code>"cba"</code> 相似,因为它们都由字符 <code>'a'</code>、<code>'b'</code>、<code>'c'</code> 组成。</li>
15+
<li>然而,<code>"abacba"</code> 和 <code>"bcfd"</code> 不相似,因为它们不是相同字符组成的。</li>
16+
</ul>
17+
18+
<p>请你找出满足字符串&nbsp;<code>words[i]</code><em> </em>和<em> </em><code>words[j]</code> 相似的下标对<em> </em><code>(i, j)</code><em> </em>,并返回下标对的数目,其中 <code>0 &lt;= i &lt; j &lt;= word.length - 1</code> 。</p>
19+
20+
<p>&nbsp;</p>
21+
22+
<p><strong>示例 1:</strong></p>
23+
24+
<pre><strong>输入:</strong>words = ["aba","aabb","abcd","bac","aabc"]
25+
<strong>输出:</strong>2
26+
<strong>解释:</strong>共有 2 对满足条件:
27+
- i = 0 且 j = 1 :words[0] 和 words[1] 只由字符 'a' 和 'b' 组成。
28+
- i = 3 且 j = 4 :words[3] 和 words[4] 只由字符 'a'、'b' 和 'c' 。
29+
</pre>
30+
31+
<p><strong>示例 2:</strong></p>
32+
33+
<pre><strong>输入:</strong>words = ["aabb","ab","ba"]
34+
<strong>输出:</strong>3
35+
<strong>解释:</strong>共有 3 对满足条件:
36+
- i = 0 且 j = 1 :words[0] 和 words[1] 只由字符 'a' 和 'b' 组成。
37+
- i = 0 且 j = 2 :words[0] 和 words[2] 只由字符 'a' 和 'b' 组成。
38+
- i = 1 且 j = 2 :words[1] 和 words[2] 只由字符 'a' 和 'b' 组成。
39+
</pre>
40+
41+
<p><strong>示例 3:</strong></p>
42+
43+
<pre><strong>输入:</strong>words = ["nba","cba","dba"]
44+
<strong>输出:</strong>0
45+
<strong>解释:</strong>不存在满足条件的下标对,返回 0 。</pre>
46+
47+
<p>&nbsp;</p>
48+
49+
<p><strong>提示:</strong></p>
50+
51+
<ul>
52+
<li><code>1 &lt;= words.length &lt;= 100</code></li>
53+
<li><code>1 &lt;= words[i].length &lt;= 100</code></li>
54+
<li><code>words[i]</code> 仅由小写英文字母组成</li>
55+
</ul>
56+
57+
## 解法
58+
59+
<!-- 这里可写通用的实现逻辑 -->
60+
61+
<!-- tabs:start -->
62+
63+
### **Python3**
64+
65+
<!-- 这里可写当前语言的特殊实现逻辑 -->
66+
67+
```python
68+
69+
```
70+
71+
### **Java**
72+
73+
<!-- 这里可写当前语言的特殊实现逻辑 -->
74+
75+
```java
76+
77+
```
78+
79+
### **C++**
80+
81+
```cpp
82+
83+
```
84+
85+
### **Go**
86+
87+
```go
88+
89+
```
90+
91+
### **...**
92+
93+
```
94+
95+
```
96+
97+
<!-- tabs:end -->
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# [2506. Count Pairs Of Similar Strings](https://leetcode.com/problems/count-pairs-of-similar-strings)
2+
3+
[中文文档](/solution/2500-2599/2506.Count%20Pairs%20Of%20Similar%20Strings/README.md)
4+
5+
## Description
6+
7+
<p>You are given a <strong>0-indexed</strong> string array <code>words</code>.</p>
8+
9+
<p>Two strings are <strong>similar</strong> if they consist of the same characters.</p>
10+
11+
<ul>
12+
<li>For example, <code>&quot;abca&quot;</code> and <code>&quot;cba&quot;</code> are similar since both consist of characters <code>&#39;a&#39;</code>, <code>&#39;b&#39;</code>, and <code>&#39;c&#39;</code>.</li>
13+
<li>However, <code>&quot;abacba&quot;</code> and <code>&quot;bcfd&quot;</code> are not similar since they do not consist of the same characters.</li>
14+
</ul>
15+
16+
<p>Return <em>the number of pairs </em><code>(i, j)</code><em> such that </em><code>0 &lt;= i &lt; j &lt;= word.length - 1</code><em> and the two strings </em><code>words[i]</code><em> and </em><code>words[j]</code><em> are similar</em>.</p>
17+
18+
<p>&nbsp;</p>
19+
<p><strong class="example">Example 1:</strong></p>
20+
21+
<pre>
22+
<strong>Input:</strong> words = [&quot;aba&quot;,&quot;aabb&quot;,&quot;abcd&quot;,&quot;bac&quot;,&quot;aabc&quot;]
23+
<strong>Output:</strong> 2
24+
<strong>Explanation:</strong> There are 2 pairs that satisfy the conditions:
25+
- i = 0 and j = 1 : both words[0] and words[1] only consist of characters &#39;a&#39; and &#39;b&#39;.
26+
- i = 3 and j = 4 : both words[3] and words[4] only consist of characters &#39;a&#39;, &#39;b&#39;, and &#39;c&#39;.
27+
</pre>
28+
29+
<p><strong class="example">Example 2:</strong></p>
30+
31+
<pre>
32+
<strong>Input:</strong> words = [&quot;aabb&quot;,&quot;ab&quot;,&quot;ba&quot;]
33+
<strong>Output:</strong> 3
34+
<strong>Explanation:</strong> There are 3 pairs that satisfy the conditions:
35+
- i = 0 and j = 1 : both words[0] and words[1] only consist of characters &#39;a&#39; and &#39;b&#39;.
36+
- i = 0 and j = 2 : both words[0] and words[2] only consist of characters &#39;a&#39; and &#39;b&#39;.
37+
- i = 1 and j = 2 : both words[1] and words[2] only consist of characters &#39;a&#39; and &#39;b&#39;.
38+
</pre>
39+
40+
<p><strong class="example">Example 3:</strong></p>
41+
42+
<pre>
43+
<strong>Input:</strong> words = [&quot;nba&quot;,&quot;cba&quot;,&quot;dba&quot;]
44+
<strong>Output:</strong> 0
45+
<strong>Explanation:</strong> Since there does not exist any pair that satisfies the conditions, we return 0.</pre>
46+
47+
<p>&nbsp;</p>
48+
<p><strong>Constraints:</strong></p>
49+
50+
<ul>
51+
<li><code>1 &lt;= words.length &lt;= 100</code></li>
52+
<li><code>1 &lt;= words[i].length &lt;= 100</code></li>
53+
<li><code>words[i]</code> consist of only lowercase English letters.</li>
54+
</ul>
55+
56+
## Solutions
57+
58+
<!-- tabs:start -->
59+
60+
### **Python3**
61+
62+
```python
63+
64+
```
65+
66+
### **Java**
67+
68+
```java
69+
70+
```
71+
72+
### **C++**
73+
74+
```cpp
75+
76+
```
77+
78+
### **Go**
79+
80+
```go
81+
82+
```
83+
84+
### **...**
85+
86+
```
87+
88+
```
89+
90+
<!-- tabs:end -->
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# [2507. 使用质因数之和替换后可以取到的最小值](https://leetcode.cn/problems/smallest-value-after-replacing-with-sum-of-prime-factors)
2+
3+
[English Version](/solution/2500-2599/2507.Smallest%20Value%20After%20Replacing%20With%20Sum%20of%20Prime%20Factors/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个正整数 <code>n</code> 。</p>
10+
11+
<p>请你将 <code>n</code> 的值替换为 <code>n</code> 的 <strong>质因数</strong> 之和,重复这一过程。</p>
12+
13+
<ul>
14+
<li>注意,如果 <code>n</code> 能够被某个质因数多次整除,则在求和时,应当包含这个质因数同样次数。</li>
15+
</ul>
16+
17+
<p>返回<em> </em><code>n</code><em> </em>可以取到的最小值。</p>
18+
19+
<p>&nbsp;</p>
20+
21+
<p><strong>示例 1:</strong></p>
22+
23+
<pre><strong>输入:</strong>n = 15
24+
<strong>输出:</strong>5
25+
<strong>解释:</strong>最开始,n = 15 。
26+
15 = 3 * 5 ,所以 n 替换为 3 + 5 = 8 。
27+
8 = 2 * 2 * 2 ,所以 n 替换为 2 + 2 + 2 = 6 。
28+
6 = 2 * 3 ,所以 n 替换为 2 + 3 = 5 。
29+
5 是 n 可以取到的最小值。
30+
</pre>
31+
32+
<p><strong>示例 2:</strong></p>
33+
34+
<pre><strong>输入:</strong>n = 3
35+
<strong>输出:</strong>3
36+
<strong>解释:</strong>最开始,n = 3 。
37+
3 是 n 可以取到的最小值。</pre>
38+
39+
<p>&nbsp;</p>
40+
41+
<p><strong>提示:</strong></p>
42+
43+
<ul>
44+
<li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li>
45+
</ul>
46+
47+
## 解法
48+
49+
<!-- 这里可写通用的实现逻辑 -->
50+
51+
<!-- tabs:start -->
52+
53+
### **Python3**
54+
55+
<!-- 这里可写当前语言的特殊实现逻辑 -->
56+
57+
```python
58+
59+
```
60+
61+
### **Java**
62+
63+
<!-- 这里可写当前语言的特殊实现逻辑 -->
64+
65+
```java
66+
67+
```
68+
69+
### **C++**
70+
71+
```cpp
72+
73+
```
74+
75+
### **Go**
76+
77+
```go
78+
79+
```
80+
81+
### **...**
82+
83+
```
84+
85+
```
86+
87+
<!-- tabs:end -->

0 commit comments

Comments
 (0)