Skip to content

Commit aa5f136

Browse files
committed
feat: add solutions to lc problem: No.0485
No.0485.Max Consecutive Ones
1 parent 4689fbe commit aa5f136

File tree

6 files changed

+170
-42
lines changed

6 files changed

+170
-42
lines changed

solution/0400-0499/0485.Max Consecutive Ones/README.md

Lines changed: 64 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@
3838

3939
<!-- 这里可写通用的实现逻辑 -->
4040

41+
**方法一:一次遍历**
42+
43+
遍历数组,记录当前连续 $1$ 的个数 `cnt`,以及最大连续 $1$ 的个数 `ans`。如果当前元素为 $1$,则 `cnt++`,否则更新 `ans`,并且 `cnt=0`。最后返回 `max(ans, cnt)` 即可。
44+
45+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组 `nums` 的长度。
46+
4147
<!-- tabs:start -->
4248

4349
### **Python3**
@@ -47,14 +53,14 @@
4753
```python
4854
class Solution:
4955
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
50-
res = t = 0
51-
for num in nums:
52-
if num == 1:
53-
t += 1
56+
cnt = ans = 0
57+
for v in nums:
58+
if v == 1:
59+
cnt += 1
5460
else:
55-
res = max(res, t)
56-
t = 0
57-
return max(res, t)
61+
ans = max(ans, cnt)
62+
cnt = 0
63+
return max(ans, cnt)
5864
```
5965

6066
### **Java**
@@ -64,20 +70,64 @@ class Solution:
6470
```java
6571
class Solution {
6672
public int findMaxConsecutiveOnes(int[] nums) {
67-
int res = 0, t = 0;
68-
for (int num : nums) {
69-
if (num == 1) {
70-
++t;
73+
int cnt = 0, ans = 0;
74+
for (int v : nums) {
75+
if (v == 1) {
76+
++cnt;
7177
} else {
72-
res = Math.max(res, t);
73-
t = 0;
78+
ans = Math.max(ans, cnt);
79+
cnt = 0;
7480
}
7581
}
76-
return Math.max(res, t);
82+
return Math.max(cnt, ans);
7783
}
7884
}
7985
```
8086

87+
### **C++**
88+
89+
```cpp
90+
class Solution {
91+
public:
92+
int findMaxConsecutiveOnes(vector<int>& nums) {
93+
int cnt = 0, ans = 0;
94+
for (int v : nums) {
95+
if (v == 1) {
96+
++cnt;
97+
} else {
98+
ans = max(ans, cnt);
99+
cnt = 0;
100+
}
101+
}
102+
return max(ans, cnt);
103+
}
104+
};
105+
```
106+
107+
### **Go**
108+
109+
```go
110+
func findMaxConsecutiveOnes(nums []int) int {
111+
ans, cnt := 0, 0
112+
for _, v := range nums {
113+
if v == 1 {
114+
cnt++
115+
} else {
116+
ans = max(ans, cnt)
117+
cnt = 0
118+
}
119+
}
120+
return max(ans, cnt)
121+
}
122+
123+
func max(a, b int) int {
124+
if a > b {
125+
return a
126+
}
127+
return b
128+
}
129+
```
130+
81131
### **JavaScript**
82132

83133
```js

solution/0400-0499/0485.Max Consecutive Ones/README_EN.md

Lines changed: 58 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,35 +39,79 @@
3939
```python
4040
class Solution:
4141
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
42-
res = t = 0
43-
for num in nums:
44-
if num == 1:
45-
t += 1
42+
cnt = ans = 0
43+
for v in nums:
44+
if v == 1:
45+
cnt += 1
4646
else:
47-
res = max(res, t)
48-
t = 0
49-
return max(res, t)
47+
ans = max(ans, cnt)
48+
cnt = 0
49+
return max(ans, cnt)
5050
```
5151

5252
### **Java**
5353

5454
```java
5555
class Solution {
5656
public int findMaxConsecutiveOnes(int[] nums) {
57-
int res = 0, t = 0;
58-
for (int num : nums) {
59-
if (num == 1) {
60-
++t;
57+
int cnt = 0, ans = 0;
58+
for (int v : nums) {
59+
if (v == 1) {
60+
++cnt;
6161
} else {
62-
res = Math.max(res, t);
63-
t = 0;
62+
ans = Math.max(ans, cnt);
63+
cnt = 0;
6464
}
6565
}
66-
return Math.max(res, t);
66+
return Math.max(cnt, ans);
6767
}
6868
}
6969
```
7070

71+
### **C++**
72+
73+
```cpp
74+
class Solution {
75+
public:
76+
int findMaxConsecutiveOnes(vector<int>& nums) {
77+
int cnt = 0, ans = 0;
78+
for (int v : nums) {
79+
if (v == 1) {
80+
++cnt;
81+
} else {
82+
ans = max(ans, cnt);
83+
cnt = 0;
84+
}
85+
}
86+
return max(ans, cnt);
87+
}
88+
};
89+
```
90+
91+
### **Go**
92+
93+
```go
94+
func findMaxConsecutiveOnes(nums []int) int {
95+
ans, cnt := 0, 0
96+
for _, v := range nums {
97+
if v == 1 {
98+
cnt++
99+
} else {
100+
ans = max(ans, cnt)
101+
cnt = 0
102+
}
103+
}
104+
return max(ans, cnt)
105+
}
106+
107+
func max(a, b int) int {
108+
if a > b {
109+
return a
110+
}
111+
return b
112+
}
113+
```
114+
71115
### **JavaScript**
72116

73117
```js
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
int findMaxConsecutiveOnes(vector<int>& nums) {
4+
int cnt = 0, ans = 0;
5+
for (int v : nums) {
6+
if (v == 1) {
7+
++cnt;
8+
} else {
9+
ans = max(ans, cnt);
10+
cnt = 0;
11+
}
12+
}
13+
return max(ans, cnt);
14+
}
15+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
func findMaxConsecutiveOnes(nums []int) int {
2+
ans, cnt := 0, 0
3+
for _, v := range nums {
4+
if v == 1 {
5+
cnt++
6+
} else {
7+
ans = max(ans, cnt)
8+
cnt = 0
9+
}
10+
}
11+
return max(ans, cnt)
12+
}
13+
14+
func max(a, b int) int {
15+
if a > b {
16+
return a
17+
}
18+
return b
19+
}
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
class Solution {
22
public int findMaxConsecutiveOnes(int[] nums) {
3-
int res = 0, t = 0;
4-
for (int num : nums) {
5-
if (num == 1) {
6-
++t;
3+
int cnt = 0, ans = 0;
4+
for (int v : nums) {
5+
if (v == 1) {
6+
++cnt;
77
} else {
8-
res = Math.max(res, t);
9-
t = 0;
8+
ans = Math.max(ans, cnt);
9+
cnt = 0;
1010
}
1111
}
12-
return Math.max(res, t);
12+
return Math.max(cnt, ans);
1313
}
1414
}
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
class Solution:
22
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
3-
res = t = 0
4-
for num in nums:
5-
if num == 1:
6-
t += 1
3+
cnt = ans = 0
4+
for v in nums:
5+
if v == 1:
6+
cnt += 1
77
else:
8-
res = max(res, t)
9-
t = 0
10-
return max(res, t)
8+
ans = max(ans, cnt)
9+
cnt = 0
10+
return max(ans, cnt)

0 commit comments

Comments
 (0)