Skip to content

Commit 05ad303

Browse files
committed
feat: add solutions to lc problems: No.0128,1248
* No.0128.Longest Consecutive Sequence * No.1248.Count Number of Nice Subarrays
1 parent 5050019 commit 05ad303

File tree

11 files changed

+272
-22
lines changed

11 files changed

+272
-22
lines changed

solution/0100-0199/0128.Longest Consecutive Sequence/README.md

+28-5
Original file line numberDiff line numberDiff line change
@@ -180,15 +180,14 @@ public:
180180
class Solution {
181181
public:
182182
int longestConsecutive(vector<int>& nums) {
183-
unordered_set<int> s;
184-
for (int num : nums)
185-
s.insert(num);
183+
unordered_set<int> s(nums.begin(), nums.end());
186184
int res = 0;
187-
for (int num : nums) {
185+
for (int& num : nums) {
188186
if (!s.count(num - 1)) {
189187
int t = 1, next = num + 1;
190-
while (s.count(next++))
188+
while (s.count(next++)) {
191189
++t;
190+
}
192191
res = max(res, t);
193192
}
194193
}
@@ -255,6 +254,30 @@ func max(a, b int) int {
255254
}
256255
```
257256

257+
### **JavaScript**
258+
259+
```js
260+
/**
261+
* @param {number[]} nums
262+
* @return {number}
263+
*/
264+
var longestConsecutive = function (nums) {
265+
const s = new Set(nums);
266+
let res = 0;
267+
for (const num of nums) {
268+
if (!s.has(num - 1)) {
269+
let t = 1;
270+
let next = num + 1;
271+
while (s.has(next++)) {
272+
t++;
273+
}
274+
res = Math.max(res, t);
275+
}
276+
}
277+
return res;
278+
};
279+
```
280+
258281
### **...**
259282

260283
```

solution/0100-0199/0128.Longest Consecutive Sequence/README_EN.md

+28-5
Original file line numberDiff line numberDiff line change
@@ -157,15 +157,14 @@ public:
157157
class Solution {
158158
public:
159159
int longestConsecutive(vector<int>& nums) {
160-
unordered_set<int> s;
161-
for (int num : nums)
162-
s.insert(num);
160+
unordered_set<int> s(nums.begin(), nums.end());
163161
int res = 0;
164-
for (int num : nums) {
162+
for (int& num : nums) {
165163
if (!s.count(num - 1)) {
166164
int t = 1, next = num + 1;
167-
while (s.count(next++))
165+
while (s.count(next++)) {
168166
++t;
167+
}
169168
res = max(res, t);
170169
}
171170
}
@@ -232,6 +231,30 @@ func max(a, b int) int {
232231
}
233232
```
234233

234+
### **JavaScript**
235+
236+
```js
237+
/**
238+
* @param {number[]} nums
239+
* @return {number}
240+
*/
241+
var longestConsecutive = function (nums) {
242+
const s = new Set(nums);
243+
let res = 0;
244+
for (const num of nums) {
245+
if (!s.has(num - 1)) {
246+
let t = 1;
247+
let next = num + 1;
248+
while (s.has(next++)) {
249+
t++;
250+
}
251+
res = Math.max(res, t);
252+
}
253+
}
254+
return res;
255+
};
256+
```
257+
235258
### **...**
236259

237260
```

solution/0100-0199/0128.Longest Consecutive Sequence/Solution.cpp

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
class Solution {
22
public:
33
int longestConsecutive(vector<int>& nums) {
4-
unordered_set<int> s;
5-
for (int num : nums)
6-
s.insert(num);
4+
unordered_set<int> s(nums.begin(), nums.end());
75
int res = 0;
8-
for (int num : nums) {
6+
for (int& num : nums) {
97
if (!s.count(num - 1)) {
108
int t = 1, next = num + 1;
11-
while (s.count(next++))
9+
while (s.count(next++)) {
1210
++t;
11+
}
1312
res = max(res, t);
1413
}
1514
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var longestConsecutive = function (nums) {
6+
const s = new Set(nums);
7+
let res = 0;
8+
for (const num of nums) {
9+
if (!s.has(num - 1)) {
10+
let t = 1;
11+
let next = num + 1;
12+
while (s.has(next++)) {
13+
t++;
14+
}
15+
res = Math.max(res, t);
16+
}
17+
}
18+
return res;
19+
};

solution/1200-1299/1248.Count Number of Nice Subarrays/README.md

+71-1
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,92 @@
4949

5050
<!-- 这里可写通用的实现逻辑 -->
5151

52+
**方法一:前缀和 + 数组/哈希表**
53+
54+
题目求子数组中恰好有 $k$ 个奇数的子数组个数,我们可以求出每个前缀数组中奇数的个数 $t$,记录在数组或哈希表 `cnt` 中。对于每个前缀数组,我们只需要求出前缀数组中奇数个数为 $t-k$ 的前缀数组个数,即为以当前前缀数组结尾的子数组个数。
55+
56+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 `nums` 的长度。
57+
5258
<!-- tabs:start -->
5359

5460
### **Python3**
5561

5662
<!-- 这里可写当前语言的特殊实现逻辑 -->
5763

5864
```python
59-
65+
class Solution:
66+
def numberOfSubarrays(self, nums: List[int], k: int) -> int:
67+
cnt = Counter({0: 1})
68+
ans = t = 0
69+
for v in nums:
70+
t += v & 1
71+
ans += cnt[t - k]
72+
cnt[t] += 1
73+
return ans
6074
```
6175

6276
### **Java**
6377

6478
<!-- 这里可写当前语言的特殊实现逻辑 -->
6579

6680
```java
81+
class Solution {
82+
public int numberOfSubarrays(int[] nums, int k) {
83+
int n = nums.length;
84+
int[] cnt = new int[n + 1];
85+
cnt[0] = 1;
86+
int ans = 0, t = 0;
87+
for (int v : nums) {
88+
t += v & 1;
89+
if (t - k >= 0) {
90+
ans += cnt[t - k];
91+
}
92+
cnt[t]++;
93+
}
94+
return ans;
95+
}
96+
}
97+
```
98+
99+
### **C++**
100+
101+
```cpp
102+
class Solution {
103+
public:
104+
int numberOfSubarrays(vector<int>& nums, int k) {
105+
int n = nums.size();
106+
vector<int> cnt(n + 1);
107+
cnt[0] = 1;
108+
int ans = 0, t = 0;
109+
for (int& v : nums) {
110+
t += v & 1;
111+
if (t - k >= 0) {
112+
ans += cnt[t - k];
113+
}
114+
cnt[t]++;
115+
}
116+
return ans;
117+
}
118+
};
119+
```
67120
121+
### **Go**
122+
123+
```go
124+
func numberOfSubarrays(nums []int, k int) (ans int) {
125+
n := len(nums)
126+
cnt := make([]int, n+1)
127+
cnt[0] = 1
128+
t := 0
129+
for _, v := range nums {
130+
t += v & 1
131+
if t >= k {
132+
ans += cnt[t-k]
133+
}
134+
cnt[t]++
135+
}
136+
return
137+
}
68138
```
69139

70140
### **...**

solution/1200-1299/1248.Count Number of Nice Subarrays/README_EN.md

+65-5
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,9 @@
4949
<p><strong>Constraints:</strong></p>
5050

5151
<ul>
52-
5352
<li><code>1 &lt;= nums.length &lt;= 50000</code></li>
54-
5553
<li><code>1 &lt;= nums[i] &lt;= 10^5</code></li>
56-
5754
<li><code>1 &lt;= k &lt;= nums.length</code></li>
58-
5955
</ul>
6056

6157
## Solutions
@@ -65,13 +61,77 @@
6561
### **Python3**
6662

6763
```python
68-
64+
class Solution:
65+
def numberOfSubarrays(self, nums: List[int], k: int) -> int:
66+
cnt = Counter({0: 1})
67+
ans = t = 0
68+
for v in nums:
69+
t += v & 1
70+
ans += cnt[t - k]
71+
cnt[t] += 1
72+
return ans
6973
```
7074

7175
### **Java**
7276

7377
```java
78+
class Solution {
79+
public int numberOfSubarrays(int[] nums, int k) {
80+
int n = nums.length;
81+
int[] cnt = new int[n + 1];
82+
cnt[0] = 1;
83+
int ans = 0, t = 0;
84+
for (int v : nums) {
85+
t += v & 1;
86+
if (t - k >= 0) {
87+
ans += cnt[t - k];
88+
}
89+
cnt[t]++;
90+
}
91+
return ans;
92+
}
93+
}
94+
```
95+
96+
### **C++**
97+
98+
```cpp
99+
class Solution {
100+
public:
101+
int numberOfSubarrays(vector<int>& nums, int k) {
102+
int n = nums.size();
103+
vector<int> cnt(n + 1);
104+
cnt[0] = 1;
105+
int ans = 0, t = 0;
106+
for (int& v : nums) {
107+
t += v & 1;
108+
if (t - k >= 0) {
109+
ans += cnt[t - k];
110+
}
111+
cnt[t]++;
112+
}
113+
return ans;
114+
}
115+
};
116+
```
74117
118+
### **Go**
119+
120+
```go
121+
func numberOfSubarrays(nums []int, k int) (ans int) {
122+
n := len(nums)
123+
cnt := make([]int, n+1)
124+
cnt[0] = 1
125+
t := 0
126+
for _, v := range nums {
127+
t += v & 1
128+
if t >= k {
129+
ans += cnt[t-k]
130+
}
131+
cnt[t]++
132+
}
133+
return
134+
}
75135
```
76136

77137
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
int numberOfSubarrays(vector<int>& nums, int k) {
4+
int n = nums.size();
5+
vector<int> cnt(n + 1);
6+
cnt[0] = 1;
7+
int ans = 0, t = 0;
8+
for (int& v : nums) {
9+
t += v & 1;
10+
if (t - k >= 0) {
11+
ans += cnt[t - k];
12+
}
13+
cnt[t]++;
14+
}
15+
return ans;
16+
}
17+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
func numberOfSubarrays(nums []int, k int) (ans int) {
2+
n := len(nums)
3+
cnt := make([]int, n+1)
4+
cnt[0] = 1
5+
t := 0
6+
for _, v := range nums {
7+
t += v & 1
8+
if t >= k {
9+
ans += cnt[t-k]
10+
}
11+
cnt[t]++
12+
}
13+
return
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public int numberOfSubarrays(int[] nums, int k) {
3+
int n = nums.length;
4+
int[] cnt = new int[n + 1];
5+
cnt[0] = 1;
6+
int ans = 0, t = 0;
7+
for (int v : nums) {
8+
t += v & 1;
9+
if (t - k >= 0) {
10+
ans += cnt[t - k];
11+
}
12+
cnt[t]++;
13+
}
14+
return ans;
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def numberOfSubarrays(self, nums: List[int], k: int) -> int:
3+
cnt = Counter({0: 1})
4+
ans = t = 0
5+
for v in nums:
6+
t += v & 1
7+
ans += cnt[t - k]
8+
cnt[t] += 1
9+
return ans

0 commit comments

Comments
 (0)