Skip to content

Commit c5d0367

Browse files
committed
feat: add solutions to leetcode problem: No.0485
1 parent adae7d8 commit c5d0367

File tree

5 files changed

+114
-19
lines changed

5 files changed

+114
-19
lines changed

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

+45-1
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,59 @@
3333
<!-- 这里可写当前语言的特殊实现逻辑 -->
3434

3535
```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)
3746
```
3847

3948
### **Java**
4049

4150
<!-- 这里可写当前语言的特殊实现逻辑 -->
4251

4352
```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+
```
4468

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+
};
4589
```
4690

4791
### **...**

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

+45-1
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,57 @@
4141
### **Python3**
4242

4343
```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)
4554
```
4655

4756
### **Java**
4857

4958
```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+
```
5074

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+
};
5195
```
5296

5397
### **...**
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;
4-
int cnt = 0;
3+
int res = 0, t = 0;
54
for (int num : nums) {
65
if (num == 1) {
7-
res = Math.max(res, ++cnt);
6+
++t;
87
} else {
9-
cnt = 0;
8+
res = Math.max(res, t);
9+
t = 0;
1010
}
1111
}
12-
return res;
12+
return Math.max(res, t);
1313
}
14-
}
14+
}

solution/0400-0499/0485.Max Consecutive Ones/Solution.js

+8-11
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,15 @@
33
* @return {number}
44
*/
55
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;
1111
} 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;
1714
}
1815
}
19-
return max;
16+
return Math.max(res, t);
2017
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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)

0 commit comments

Comments
 (0)