52
52
53
53
### 方法一:一次遍历
54
54
55
- 遍历数组,记录当前连续 $1$ 的个数 ` cnt ` ,以及最大连续 $1$ 的个数 ` ans ` 。如果当前元素为 $1$,则 ` cnt++ ` ,否则更新 ` ans ` ,并且 ` cnt=0 ` 。最后返回 ` max(ans, cnt) ` 即可 。
55
+ 我们可以遍历数组,用一个变量 $\textit{cnt}$ 记录当前连续的 1 的个数,用另一个变量 $\textit{ ans}$ 记录最大连续 1 的个数 。
56
56
57
- 时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组 ` nums ` 的长度。
57
+ 当遍历到一个 1 时,将 $\textit{cnt}$ 加一,然后更新 $\textit{ans}$ 的值为 $\textit{cnt}$ 和 $\textit{ans}$ 本身的最大值,即 $\textit{ans} = \max(\textit{ans}, \textit{cnt})$。否则,将 $\textit{cnt}$ 重置为 0。
58
+
59
+ 遍历结束后,返回 $\textit{ans}$ 的值即可。
60
+
61
+ 时间复杂度 $O(n)$,其中 $n$ 为数组的长度。空间复杂度 $O(1)$。
58
62
59
63
<!-- tabs:start -->
60
64
@@ -63,31 +67,30 @@ tags:
63
67
``` python
64
68
class Solution :
65
69
def findMaxConsecutiveOnes (self , nums : List[int ]) -> int :
66
- cnt = ans = 0
67
- for v in nums:
68
- if v == 1 :
70
+ ans = cnt = 0
71
+ for x in nums:
72
+ if x :
69
73
cnt += 1
70
- else :
71
74
ans = max (ans, cnt)
75
+ else :
72
76
cnt = 0
73
- return max ( ans, cnt)
77
+ return ans
74
78
```
75
79
76
80
#### Java
77
81
78
82
``` java
79
83
class Solution {
80
84
public int findMaxConsecutiveOnes (int [] nums ) {
81
- int cnt = 0 , ans = 0 ;
82
- for (int v : nums) {
83
- if (v == 1 ) {
84
- ++ cnt;
85
+ int ans = 0 , cnt = 0 ;
86
+ for (int x : nums) {
87
+ if (x == 1 ) {
88
+ ans = Math . max(ans, ++ cnt) ;
85
89
} else {
86
- ans = Math . max(ans, cnt);
87
90
cnt = 0 ;
88
91
}
89
92
}
90
- return Math . max(cnt, ans) ;
93
+ return ans;
91
94
}
92
95
}
93
96
```
@@ -98,52 +101,49 @@ class Solution {
98
101
class Solution {
99
102
public:
100
103
int findMaxConsecutiveOnes(vector<int >& nums) {
101
- int cnt = 0, ans = 0;
102
- for (int v : nums) {
103
- if (v == 1 ) {
104
- ++cnt;
104
+ int ans = 0, cnt = 0;
105
+ for (int x : nums) {
106
+ if (x ) {
107
+ ans = max(ans, ++cnt) ;
105
108
} else {
106
- ans = max(ans, cnt);
107
109
cnt = 0;
108
110
}
109
111
}
110
- return max( ans, cnt) ;
112
+ return ans;
111
113
}
112
114
};
113
115
```
114
116
115
117
#### Go
116
118
117
119
```go
118
- func findMaxConsecutiveOnes(nums []int) int {
119
- ans, cnt := 0, 0
120
- for _, v := range nums {
121
- if v == 1 {
120
+ func findMaxConsecutiveOnes(nums []int) (ans int) {
121
+ cnt := 0
122
+ for _, x := range nums {
123
+ if x == 1 {
122
124
cnt++
123
- } else {
124
125
ans = max(ans, cnt)
126
+ } else {
125
127
cnt = 0
126
128
}
127
129
}
128
- return max(ans, cnt)
130
+ return
129
131
}
130
132
```
131
133
132
134
#### TypeScript
133
135
134
136
``` ts
135
137
function findMaxConsecutiveOnes(nums : number []): number {
136
- let res = 0 ;
137
- let count = 0 ;
138
- for (const num of nums ) {
139
- if (num === 0 ) {
140
- res = Math .max (res , count );
141
- count = 0 ;
138
+ let [ans, cnt] = [0 , 0 ];
139
+ for (const x of nums ) {
140
+ if (x ) {
141
+ ans = Math .max (ans , ++ cnt );
142
142
} else {
143
- count ++ ;
143
+ cnt = 0 ;
144
144
}
145
145
}
146
- return Math . max ( res , count ) ;
146
+ return ans ;
147
147
}
148
148
```
149
149
@@ -152,17 +152,19 @@ function findMaxConsecutiveOnes(nums: number[]): number {
152
152
``` rust
153
153
impl Solution {
154
154
pub fn find_max_consecutive_ones (nums : Vec <i32 >) -> i32 {
155
- let mut res = 0 ;
156
- let mut count = 0 ;
157
- for num in nums {
158
- if num == 0 {
159
- res = res . max (count );
160
- count = 0 ;
155
+ let mut ans = 0 ;
156
+ let mut cnt = 0 ;
157
+
158
+ for & x in nums . iter () {
159
+ if x == 1 {
160
+ cnt += 1 ;
161
+ ans = ans . max (cnt );
161
162
} else {
162
- count += 1 ;
163
+ cnt = 0 ;
163
164
}
164
165
}
165
- res . max (count )
166
+
167
+ ans
166
168
}
167
169
}
168
170
```
@@ -175,17 +177,15 @@ impl Solution {
175
177
* @return {number}
176
178
*/
177
179
var findMaxConsecutiveOnes = function (nums ) {
178
- let res = 0 ,
179
- t = 0 ;
180
- for (let num of nums) {
181
- if (num == 1 ) {
182
- ++ t;
180
+ let [ans, cnt] = [0 , 0 ];
181
+ for (const x of nums) {
182
+ if (x) {
183
+ ans = Math .max (ans, ++ cnt);
183
184
} else {
184
- res = Math .max (res, t);
185
- t = 0 ;
185
+ cnt = 0 ;
186
186
}
187
187
}
188
- return Math . max (res, t) ;
188
+ return ans ;
189
189
};
190
190
```
191
191
@@ -198,16 +198,18 @@ class Solution {
198
198
* @return Integer
199
199
*/
200
200
function findMaxConsecutiveOnes($nums) {
201
- $tmp = $max = 0;
202
- for ($i = 0; $i < count($nums); $i++) {
203
- if ($nums[$i] == 1) {
204
- $tmp++;
201
+ $ans = $cnt = 0;
202
+
203
+ foreach ($nums as $x) {
204
+ if ($x == 1) {
205
+ $cnt += 1;
206
+ $ans = max($ans, $cnt);
205
207
} else {
206
- $max = max($tmp, $max);
207
- $tmp = 0;
208
+ $cnt = 0;
208
209
}
209
210
}
210
- return max($tmp, $max);
211
+
212
+ return $ans;
211
213
}
212
214
}
213
215
```
0 commit comments