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;
}
}
'JAVA > leetcode' 카테고리의 다른 글
[LeetCode] Array - Remove Element !! (0) | 2021.08.05 |
---|---|
[LeetCode] Array - Merge Sorted Array (0) | 2021.08.05 |
[LeetCode] Array - Duplicate Zeros !! (0) | 2021.08.05 |
[LeetCode] Array - Squares of a Sorted Array (0) | 2021.08.05 |
[LeetCode] Array - Find Numbers with Even Number of Digits (0) | 2021.08.04 |