File tree 5 files changed +114
-19
lines changed
solution/0400-0499/0485.Max Consecutive Ones
5 files changed +114
-19
lines changed Original file line number Diff line number Diff line change 33
33
<!-- 这里可写当前语言的特殊实现逻辑 -->
34
34
35
35
``` python
36
-
36
+ class Solution :
37
+ def findMaxConsecutiveOnes (self , nums : List[int ]) -> int :
38
+ res = t = 0
39
+ for num in nums:
40
+ if num == 1 :
41
+ t += 1
42
+ else :
43
+ res = max (res, t)
44
+ t = 0
45
+ return max (res, t)
37
46
```
38
47
39
48
### ** Java**
40
49
41
50
<!-- 这里可写当前语言的特殊实现逻辑 -->
42
51
43
52
``` java
53
+ class Solution {
54
+ public int findMaxConsecutiveOnes (int [] nums ) {
55
+ int res = 0 , t = 0 ;
56
+ for (int num : nums) {
57
+ if (num == 1 ) {
58
+ ++ t;
59
+ } else {
60
+ res = Math . max(res, t);
61
+ t = 0 ;
62
+ }
63
+ }
64
+ return Math . max(res, t);
65
+ }
66
+ }
67
+ ```
44
68
69
+ ### ** JavaScript**
70
+
71
+ ``` js
72
+ /**
73
+ * @param {number[]} nums
74
+ * @return {number}
75
+ */
76
+ var findMaxConsecutiveOnes = function (nums ) {
77
+ let res = 0 ,
78
+ t = 0 ;
79
+ for (let num of nums) {
80
+ if (num == 1 ) {
81
+ ++ t;
82
+ } else {
83
+ res = Math .max (res, t);
84
+ t = 0 ;
85
+ }
86
+ }
87
+ return Math .max (res, t);
88
+ };
45
89
```
46
90
47
91
### ** ...**
Original file line number Diff line number Diff line change 41
41
### ** Python3**
42
42
43
43
``` python
44
-
44
+ class Solution :
45
+ def findMaxConsecutiveOnes (self , nums : List[int ]) -> int :
46
+ res = t = 0
47
+ for num in nums:
48
+ if num == 1 :
49
+ t += 1
50
+ else :
51
+ res = max (res, t)
52
+ t = 0
53
+ return max (res, t)
45
54
```
46
55
47
56
### ** Java**
48
57
49
58
``` java
59
+ class Solution {
60
+ public int findMaxConsecutiveOnes (int [] nums ) {
61
+ int res = 0 , t = 0 ;
62
+ for (int num : nums) {
63
+ if (num == 1 ) {
64
+ ++ t;
65
+ } else {
66
+ res = Math . max(res, t);
67
+ t = 0 ;
68
+ }
69
+ }
70
+ return Math . max(res, t);
71
+ }
72
+ }
73
+ ```
50
74
75
+ ### ** JavaScript**
76
+
77
+ ``` js
78
+ /**
79
+ * @param {number[]} nums
80
+ * @return {number}
81
+ */
82
+ var findMaxConsecutiveOnes = function (nums ) {
83
+ let res = 0 ,
84
+ t = 0 ;
85
+ for (let num of nums) {
86
+ if (num == 1 ) {
87
+ ++ t;
88
+ } else {
89
+ res = Math .max (res, t);
90
+ t = 0 ;
91
+ }
92
+ }
93
+ return Math .max (res, t);
94
+ };
51
95
```
52
96
53
97
### ** ...**
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public int findMaxConsecutiveOnes (int [] nums ) {
3
- int res = 0 ;
4
- int cnt = 0 ;
3
+ int res = 0 , t = 0 ;
5
4
for (int num : nums ) {
6
5
if (num == 1 ) {
7
- res = Math . max ( res , ++ cnt ) ;
6
+ ++ t ;
8
7
} else {
9
- cnt = 0 ;
8
+ res = Math .max (res , t );
9
+ t = 0 ;
10
10
}
11
11
}
12
- return res ;
12
+ return Math . max ( res , t ) ;
13
13
}
14
- }
14
+ }
Original file line number Diff line number Diff line change 3
3
* @return {number }
4
4
*/
5
5
var findMaxConsecutiveOnes = function ( nums ) {
6
- var max = 0 ;
7
- var n = 0 ;
8
- for ( var i = 0 ; i < nums . length ; i ++ ) {
9
- if ( nums [ i ] == 0 ) {
10
- n = 0 ;
6
+ let res = 0 ,
7
+ t = 0 ;
8
+ for ( let num of nums ) {
9
+ if ( num == 1 ) {
10
+ ++ t ;
11
11
} else {
12
- n ++ ;
13
- }
14
- max = n > max ? n : max ;
15
- if ( n > max ) {
16
- max = n ;
12
+ res = Math . max ( res , t ) ;
13
+ t = 0 ;
17
14
}
18
15
}
19
- return max ;
16
+ return Math . max ( res , t ) ;
20
17
} ;
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def findMaxConsecutiveOnes (self , nums : List [int ]) -> int :
3
+ res = t = 0
4
+ for num in nums :
5
+ if num == 1 :
6
+ t += 1
7
+ else :
8
+ res = max (res , t )
9
+ t = 0
10
+ return max (res , t )
You can’t perform that action at this time.
0 commit comments