FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
leetcode/java/485_Max_Consecutive_Ones.java at master · jakehoare/leetcode · GitHub
jakehoare
/
leetcode
Public
Notifications
You must be signed in to change notification settings
Fork
30
Star
51
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
leetcode
/
java
/
485_Max_Consecutive_Ones.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
26 lines (22 loc) · 705 Bytes
Breadcrumbs
leetcode
/
java
/
485_Max_Consecutive_Ones.java
Copy path
File metadata and controls
26 lines (22 loc) · 705 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/*
https://leetcode.com/problems/max-consecutive-ones/
Given a binary array, find the maximum number of consecutive 1s in this array.
Iterate over array, incrementing current consecutive count for every 1, resetting and updating max for every 0.
Time - O(n)
Space - O(1)
*/
public
class
Solution
{
public
int
findMaxConsecutiveOnes
(
int
[]
nums
) {
int
consec
=
0
;
int
maxConsec
=
0
;
for
(
int
num
:
nums
) {
if
(
num
==
0
) {
maxConsec
=
Math
.
max
(
maxConsec
,
consec
);
consec
=
0
;
}
else
{
++
consec
;
}
}
return
Math
.
max
(
maxConsec
,
consec
);
// case of nums ending in 1s
}
}
Back
|
FazBrowse Home
|
New Git URL