Skip to content

Commit 3f09a68

Browse files
committed
feat: update solutions to lc problem: No.0066,2031
1 parent 30629d5 commit 3f09a68

File tree

20 files changed

+1946
-1189
lines changed

20 files changed

+1946
-1189
lines changed

solution/0000-0099/0066.Plus One/README.md

+3-4
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,7 @@ var plusOne = function(digits) {
105105
return digits;
106106
}
107107
}
108-
digits.unshift(1);
109-
return digits;
108+
return [1, ...digits];
110109
};
111110
```
112111

@@ -116,8 +115,8 @@ var plusOne = function(digits) {
116115
class Solution {
117116
public:
118117
vector<int> plusOne(vector<int>& digits) {
119-
int n = digits.size();
120-
for (int i = n - 1; i >= 0; --i) {
118+
for (int i = digits.size() - 1; i >= 0; --i)
119+
{
121120
++digits[i];
122121
digits[i] %= 10;
123122
if (digits[i] != 0) return digits;

solution/0000-0099/0066.Plus One/README_EN.md

+3-4
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,7 @@ var plusOne = function(digits) {
9595
return digits;
9696
}
9797
}
98-
digits.unshift(1);
99-
return digits;
98+
return [1, ...digits];
10099
};
101100
```
102101

@@ -106,8 +105,8 @@ var plusOne = function(digits) {
106105
class Solution {
107106
public:
108107
vector<int> plusOne(vector<int>& digits) {
109-
int n = digits.size();
110-
for (int i = n - 1; i >= 0; --i) {
108+
for (int i = digits.size() - 1; i >= 0; --i)
109+
{
111110
++digits[i];
112111
digits[i] %= 10;
113112
if (digits[i] != 0) return digits;

solution/0000-0099/0066.Plus One/Solution.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
class Solution {
22
public:
33
vector<int> plusOne(vector<int>& digits) {
4-
int n = digits.size();
5-
for (int i = n - 1; i >= 0; --i) {
4+
for (int i = digits.size() - 1; i >= 0; --i)
5+
{
66
++digits[i];
77
digits[i] %= 10;
88
if (digits[i] != 0) return digits;

solution/0000-0099/0066.Plus One/Solution.js

+8-9
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
* @return {number[]}
44
*/
55
var plusOne = function(digits) {
6-
for (let i = digits.length - 1; i >= 0; --i) {
7-
++digits[i];
8-
digits[i] %= 10;
9-
if (digits[i] != 0) {
10-
return digits;
11-
}
12-
}
13-
digits.unshift(1);
14-
return digits;
6+
for (let i = digits.length - 1; i >= 0; --i) {
7+
++digits[i];
8+
digits[i] %= 10;
9+
if (digits[i] != 0) {
10+
return digits;
11+
}
12+
}
13+
return [1, ...digits];
1514
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# [2031. Count Subarrays With More Ones Than Zeros](https://leetcode-cn.com/problems/count-subarrays-with-more-ones-than-zeros)
2+
3+
[English Version](/solution/2000-2099/2031.Count%20Subarrays%20With%20More%20Ones%20Than%20Zeros/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>You are given a binary array <code>nums</code> containing only the integers <code>0</code> and <code>1</code>. Return<em> the number of <strong>subarrays</strong> in nums that have <strong>more</strong> </em><code>1</code>&#39;<em>s than </em><code>0</code><em>&#39;s. Since the answer may be very large, return it <strong>modulo</strong> </em><code>10<sup>9</sup> + 7</code>.</p>
10+
11+
<p>A <strong>subarray</strong> is a contiguous sequence of elements within an array.</p>
12+
13+
<p>&nbsp;</p>
14+
<p><strong>Example 1:</strong></p>
15+
16+
<pre>
17+
<strong>Input:</strong> nums = [0,1,1,0,1]
18+
<strong>Output:</strong> 9
19+
<strong>Explanation:</strong>
20+
The subarrays of size 1 that have more ones than zeros are: [1], [1], [1]
21+
The subarrays of size 2 that have more ones than zeros are: [1,1]
22+
The subarrays of size 3 that have more ones than zeros are: [0,1,1], [1,1,0], [1,0,1]
23+
The subarrays of size 4 that have more ones than zeros are: [1,1,0,1]
24+
The subarrays of size 5 that have more ones than zeros are: [0,1,1,0,1]
25+
</pre>
26+
27+
<p><strong>Example 2:</strong></p>
28+
29+
<pre>
30+
<strong>Input:</strong> nums = [0]
31+
<strong>Output:</strong> 0
32+
<strong>Explanation:</strong>
33+
No subarrays have more ones than zeros.
34+
</pre>
35+
36+
<p><strong>Example 3:</strong></p>
37+
38+
<pre>
39+
<strong>Input:</strong> nums = [1]
40+
<strong>Output:</strong> 1
41+
<strong>Explanation:</strong>
42+
The subarrays of size 1 that have more ones than zeros are: [1]
43+
</pre>
44+
45+
<p>&nbsp;</p>
46+
<p><strong>Constraints:</strong></p>
47+
48+
<ul>
49+
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
50+
<li><code>0 &lt;= nums[i] &lt;= 1</code></li>
51+
</ul>
52+
53+
54+
## 解法
55+
56+
<!-- 这里可写通用的实现逻辑 -->
57+
58+
<!-- tabs:start -->
59+
60+
### **Python3**
61+
62+
<!-- 这里可写当前语言的特殊实现逻辑 -->
63+
64+
```python
65+
66+
```
67+
68+
### **Java**
69+
70+
<!-- 这里可写当前语言的特殊实现逻辑 -->
71+
72+
```java
73+
74+
```
75+
76+
### **...**
77+
78+
```
79+
80+
```
81+
82+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# [2031. Count Subarrays With More Ones Than Zeros](https://leetcode.com/problems/count-subarrays-with-more-ones-than-zeros)
2+
3+
[中文文档](/solution/2000-2099/2031.Count%20Subarrays%20With%20More%20Ones%20Than%20Zeros/README.md)
4+
5+
## Description
6+
7+
<p>You are given a binary array <code>nums</code> containing only the integers <code>0</code> and <code>1</code>. Return<em> the number of <strong>subarrays</strong> in nums that have <strong>more</strong> </em><code>1</code>&#39;<em>s than </em><code>0</code><em>&#39;s. Since the answer may be very large, return it <strong>modulo</strong> </em><code>10<sup>9</sup> + 7</code>.</p>
8+
9+
<p>A <strong>subarray</strong> is a contiguous sequence of elements within an array.</p>
10+
11+
<p>&nbsp;</p>
12+
<p><strong>Example 1:</strong></p>
13+
14+
<pre>
15+
<strong>Input:</strong> nums = [0,1,1,0,1]
16+
<strong>Output:</strong> 9
17+
<strong>Explanation:</strong>
18+
The subarrays of size 1 that have more ones than zeros are: [1], [1], [1]
19+
The subarrays of size 2 that have more ones than zeros are: [1,1]
20+
The subarrays of size 3 that have more ones than zeros are: [0,1,1], [1,1,0], [1,0,1]
21+
The subarrays of size 4 that have more ones than zeros are: [1,1,0,1]
22+
The subarrays of size 5 that have more ones than zeros are: [0,1,1,0,1]
23+
</pre>
24+
25+
<p><strong>Example 2:</strong></p>
26+
27+
<pre>
28+
<strong>Input:</strong> nums = [0]
29+
<strong>Output:</strong> 0
30+
<strong>Explanation:</strong>
31+
No subarrays have more ones than zeros.
32+
</pre>
33+
34+
<p><strong>Example 3:</strong></p>
35+
36+
<pre>
37+
<strong>Input:</strong> nums = [1]
38+
<strong>Output:</strong> 1
39+
<strong>Explanation:</strong>
40+
The subarrays of size 1 that have more ones than zeros are: [1]
41+
</pre>
42+
43+
<p>&nbsp;</p>
44+
<p><strong>Constraints:</strong></p>
45+
46+
<ul>
47+
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
48+
<li><code>0 &lt;= nums[i] &lt;= 1</code></li>
49+
</ul>
50+
51+
52+
## Solutions
53+
54+
<!-- tabs:start -->
55+
56+
### **Python3**
57+
58+
```python
59+
60+
```
61+
62+
### **Java**
63+
64+
```java
65+
66+
```
67+
68+
### **...**
69+
70+
```
71+
72+
```
73+
74+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# [2036. Maximum Alternating Subarray Sum](https://leetcode-cn.com/problems/maximum-alternating-subarray-sum)
2+
3+
[English Version](/solution/2000-2099/2036.Maximum%20Alternating%20Subarray%20Sum/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>A <strong>subarray</strong> of a <strong>0-indexed</strong> integer array is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p>
10+
11+
<p>The <strong>alternating subarray sum</strong> of a subarray that ranges from index <code>i</code> to <code>j</code> (<strong>inclusive</strong>, <code>0 &lt;= i &lt;= j &lt; nums.length</code>) is <code>nums[i] - nums[i+1] + nums[i+2] - ... +/- nums[j]</code>.</p>
12+
13+
<p>Given a <strong>0-indexed</strong> integer array <code>nums</code>, return <em>the <strong>maximum alternating subarray sum</strong> of any subarray of </em><code>nums</code>.</p>
14+
15+
<p>&nbsp;</p>
16+
<p><strong>Example 1:</strong></p>
17+
18+
<pre>
19+
<strong>Input:</strong> nums = [3,-1,1,2]
20+
<strong>Output:</strong> 5
21+
<strong>Explanation:</strong>
22+
The subarray [3,-1,1] has the largest alternating subarray sum.
23+
The alternating subarray sum is 3 - (-1) + 1 = 5.
24+
</pre>
25+
26+
<p><strong>Example 2:</strong></p>
27+
28+
<pre>
29+
<strong>Input:</strong> nums = [2,2,2,2,2]
30+
<strong>Output:</strong> 2
31+
<strong>Explanation:</strong>
32+
The subarrays [2], [2,2,2], and [2,2,2,2,2] have the largest alternating subarray sum.
33+
The alternating subarray sum of [2] is 2.
34+
The alternating subarray sum of [2,2,2] is 2 - 2 + 2 = 2.
35+
The alternating subarray sum of [2,2,2,2,2] is 2 - 2 + 2 - 2 + 2 = 2.
36+
</pre>
37+
38+
<p><strong>Example 3:</strong></p>
39+
40+
<pre>
41+
<strong>Input:</strong> nums = [1]
42+
<strong>Output:</strong> 1
43+
<strong>Explanation:</strong>
44+
There is only one non-empty subarray, which is [1].
45+
The alternating subarray sum is 1.
46+
</pre>
47+
48+
<p>&nbsp;</p>
49+
<p><strong>Constraints:</strong></p>
50+
51+
<ul>
52+
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
53+
<li><code>-10<sup>5</sup> &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
54+
</ul>
55+
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+
### **...**
80+
81+
```
82+
83+
```
84+
85+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# [2036. Maximum Alternating Subarray Sum](https://leetcode.com/problems/maximum-alternating-subarray-sum)
2+
3+
[中文文档](/solution/2000-2099/2036.Maximum%20Alternating%20Subarray%20Sum/README.md)
4+
5+
## Description
6+
7+
<p>A <strong>subarray</strong> of a <strong>0-indexed</strong> integer array is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p>
8+
9+
<p>The <strong>alternating subarray sum</strong> of a subarray that ranges from index <code>i</code> to <code>j</code> (<strong>inclusive</strong>, <code>0 &lt;= i &lt;= j &lt; nums.length</code>) is <code>nums[i] - nums[i+1] + nums[i+2] - ... +/- nums[j]</code>.</p>
10+
11+
<p>Given a <strong>0-indexed</strong> integer array <code>nums</code>, return <em>the <strong>maximum alternating subarray sum</strong> of any subarray of </em><code>nums</code>.</p>
12+
13+
<p>&nbsp;</p>
14+
<p><strong>Example 1:</strong></p>
15+
16+
<pre>
17+
<strong>Input:</strong> nums = [3,-1,1,2]
18+
<strong>Output:</strong> 5
19+
<strong>Explanation:</strong>
20+
The subarray [3,-1,1] has the largest alternating subarray sum.
21+
The alternating subarray sum is 3 - (-1) + 1 = 5.
22+
</pre>
23+
24+
<p><strong>Example 2:</strong></p>
25+
26+
<pre>
27+
<strong>Input:</strong> nums = [2,2,2,2,2]
28+
<strong>Output:</strong> 2
29+
<strong>Explanation:</strong>
30+
The subarrays [2], [2,2,2], and [2,2,2,2,2] have the largest alternating subarray sum.
31+
The alternating subarray sum of [2] is 2.
32+
The alternating subarray sum of [2,2,2] is 2 - 2 + 2 = 2.
33+
The alternating subarray sum of [2,2,2,2,2] is 2 - 2 + 2 - 2 + 2 = 2.
34+
</pre>
35+
36+
<p><strong>Example 3:</strong></p>
37+
38+
<pre>
39+
<strong>Input:</strong> nums = [1]
40+
<strong>Output:</strong> 1
41+
<strong>Explanation:</strong>
42+
There is only one non-empty subarray, which is [1].
43+
The alternating subarray sum is 1.
44+
</pre>
45+
46+
<p>&nbsp;</p>
47+
<p><strong>Constraints:</strong></p>
48+
49+
<ul>
50+
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
51+
<li><code>-10<sup>5</sup> &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
52+
</ul>
53+
54+
55+
## Solutions
56+
57+
<!-- tabs:start -->
58+
59+
### **Python3**
60+
61+
```python
62+
63+
```
64+
65+
### **Java**
66+
67+
```java
68+
69+
```
70+
71+
### **...**
72+
73+
```
74+
75+
```
76+
77+
<!-- tabs:end -->

0 commit comments

Comments
 (0)