File tree Expand file tree Collapse file tree 6 files changed +170
-42
lines changed
solution/0400-0499/0485.Max Consecutive Ones Expand file tree Collapse file tree 6 files changed +170
-42
lines changed Original file line number Diff line number Diff line change 38
38
39
39
<!-- 这里可写通用的实现逻辑 -->
40
40
41
+ ** 方法一:一次遍历**
42
+
43
+ 遍历数组,记录当前连续 $1$ 的个数 ` cnt ` ,以及最大连续 $1$ 的个数 ` ans ` 。如果当前元素为 $1$,则 ` cnt++ ` ,否则更新 ` ans ` ,并且 ` cnt=0 ` 。最后返回 ` max(ans, cnt) ` 即可。
44
+
45
+ 时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组 ` nums ` 的长度。
46
+
41
47
<!-- tabs:start -->
42
48
43
49
### ** Python3**
47
53
``` python
48
54
class Solution :
49
55
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
54
60
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 )
58
64
```
59
65
60
66
### ** Java**
@@ -64,20 +70,64 @@ class Solution:
64
70
``` java
65
71
class Solution {
66
72
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 ;
71
77
} else {
72
- res = Math . max(res, t );
73
- t = 0 ;
78
+ ans = Math . max(ans, cnt );
79
+ cnt = 0 ;
74
80
}
75
81
}
76
- return Math . max(res, t );
82
+ return Math . max(cnt, ans );
77
83
}
78
84
}
79
85
```
80
86
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
+
81
131
### ** JavaScript**
82
132
83
133
``` js
Original file line number Diff line number Diff line change 39
39
``` python
40
40
class Solution :
41
41
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
46
46
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 )
50
50
```
51
51
52
52
### ** Java**
53
53
54
54
``` java
55
55
class Solution {
56
56
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 ;
61
61
} else {
62
- res = Math . max(res, t );
63
- t = 0 ;
62
+ ans = Math . max(ans, cnt );
63
+ cnt = 0 ;
64
64
}
65
65
}
66
- return Math . max(res, t );
66
+ return Math . max(cnt, ans );
67
67
}
68
68
}
69
69
```
70
70
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
+
71
115
### ** JavaScript**
72
116
73
117
``` js
Original file line number Diff line number Diff line change
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
+ };
Original file line number Diff line number Diff line change
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
+ }
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 , 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 ;
7
7
} else {
8
- res = Math .max (res , t );
9
- t = 0 ;
8
+ ans = Math .max (ans , cnt );
9
+ cnt = 0 ;
10
10
}
11
11
}
12
- return Math .max (res , t );
12
+ return Math .max (cnt , ans );
13
13
}
14
14
}
Original file line number Diff line number Diff line change 1
1
class Solution :
2
2
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
7
7
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 )
You can’t perform that action at this time.
0 commit comments