JAVA/leetcode
[LeetCode] Array - Max Consecutive Ones
JJunDol2
2021. 8. 4. 22:08
1이 나타나면 개수를 새고, 0이 나오면 연속이 끊긴 것이므로, 다시 카운팅
class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
int max = 0;
int cnt = 0;
for(int i = 0; i < nums.length; i++){
if(nums[i] == 1){
cnt++;
if(max < cnt){
max = cnt;
}
} else{
cnt = 0;
}
}
return max;
}
}