-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathMaxConsecutiveOnes.java
More file actions
42 lines (40 loc) · 1.08 KB
/
MaxConsecutiveOnes.java
File metadata and controls
42 lines (40 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package array;
// Source : https://leetcode.com/problems/max-consecutive-ones/
// Id : 485
// Author : Fanlu Hai | https://github.com/Fanlu91/FanluLeetcode
// Date : 2019-06-10
// Topic : Array
// Level : Easy-
// Other :
// Tips : Do not use for reach with array. It is much slower.
// Result : 99.83% 99.97%
public class MaxConsecutiveOnes {
// 20.56% 4 ms 93.19%
public int findMaxConsecutiveOnesOrigin(int[] nums) {
int max = 0, tmp = 0;
for (int i : nums) {
if (i == 1)
tmp++;
else {
if (tmp > max)
max = tmp;
tmp = 0;
}
}
return tmp > max ? tmp : max;
}
// 99.83% 2ms 99.97%
public int findMaxConsecutiveOnes(int[] nums) {
int max = 0, tmp = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == 1) {
tmp++;
} else {
if (tmp > max)
max = tmp;
tmp = 0;
}
}
return tmp > max ? tmp : max;
}
}