Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8a0a54d

Browse files
feat: add solutions to lc problem: No.0485 (#3247)
No.0485.Max Consecutive Ones
1 parent 894ebe6 commit 8a0a54d

File tree

10 files changed

+172
-166
lines changed

10 files changed

+172
-166
lines changed

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

Lines changed: 58 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,13 @@ tags:
5252

5353
### 方法一:一次遍历
5454

55-
遍历数组,记录当前连续 1ドル$ 的个数 `cnt`,以及最大连续 1ドル$ 的个数`ans`。如果当前元素为 1ドル,ドル则 `cnt++`,否则更新 `ans`,并且 `cnt=0`。最后返回 `max(ans, cnt)` 即可
55+
我们可以遍历数组,用一个变量 $\textit{cnt}$ 记录当前连续的 1 的个数,用另一个变量 $\textit{ans}$ 记录最大连续 1 的个数
5656

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)$。
5862

5963
<!-- tabs:start -->
6064

@@ -63,31 +67,30 @@ tags:
6367
```python
6468
class Solution:
6569
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:
6973
cnt += 1
70-
else:
7174
ans = max(ans, cnt)
75+
else:
7276
cnt = 0
73-
return max(ans, cnt)
77+
return ans
7478
```
7579

7680
#### Java
7781

7882
```java
7983
class Solution {
8084
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);
8589
} else {
86-
ans = Math.max(ans, cnt);
8790
cnt = 0;
8891
}
8992
}
90-
return Math.max(cnt, ans);
93+
return ans;
9194
}
9295
}
9396
```
@@ -98,52 +101,49 @@ class Solution {
98101
class Solution {
99102
public:
100103
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);
105108
} else {
106-
ans = max(ans, cnt);
107109
cnt = 0;
108110
}
109111
}
110-
return max(ans, cnt);
112+
return ans;
111113
}
112114
};
113115
```
114116
115117
#### Go
116118
117119
```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 {
122124
cnt++
123-
} else {
124125
ans = max(ans, cnt)
126+
} else {
125127
cnt = 0
126128
}
127129
}
128-
return max(ans, cnt)
130+
return
129131
}
130132
```
131133

132134
#### TypeScript
133135

134136
```ts
135137
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);
142142
} else {
143-
count++;
143+
cnt=0;
144144
}
145145
}
146-
return Math.max(res, count);
146+
return ans;
147147
}
148148
```
149149

@@ -152,17 +152,19 @@ function findMaxConsecutiveOnes(nums: number[]): number {
152152
```rust
153153
impl Solution {
154154
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);
161162
} else {
162-
count+=1;
163+
cnt=0;
163164
}
164165
}
165-
res.max(count)
166+
167+
ans
166168
}
167169
}
168170
```
@@ -175,17 +177,15 @@ impl Solution {
175177
* @return {number}
176178
*/
177179
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);
183184
} else {
184-
res = Math.max(res, t);
185-
t = 0;
185+
cnt = 0;
186186
}
187187
}
188-
return Math.max(res, t);
188+
return ans;
189189
};
190190
```
191191

@@ -198,16 +198,18 @@ class Solution {
198198
* @return Integer
199199
*/
200200
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);
205207
} else {
206-
$max = max($tmp, $max);
207-
$tmp = 0;
208+
$cnt = 0;
208209
}
209210
}
210-
return max($tmp, $max);
211+
212+
return $ans;
211213
}
212214
}
213215
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /