485. Max Consecutive Ones

hyunwoo·2023년 9월 28일

LeetCode

목록 보기
2/3

문제 링크

class Solution {
    public int findMaxConsecutiveOnes(int[] nums) {
        int input = 0;
        int result = 0;
        
        for (int i=0; i<nums.length; i++){
            if (nums[i] == 1)
                input++;
            else{
                if (input>result){
                    result= input;
                    input = 0;
                }
            
            }
        }
        if (input> result)
            return input;
        else 
            return result;
    }
}

근데 왜 이렇게 하면 통과가 되는거지..?

class Solution {
    public int findMaxConsecutiveOnes(int[] nums) {
        int input = 0;
        int result = 0;
        
        for (int i=0; i<nums.length; i++){
            if (nums[i] == 1)
                input++;
            else{
                if (input>result){
                    result= input;
                }
                input = 0;

            
            }
        }
        if (input> result)
            return input;
        else 
            return result;
    }
}

0개의 댓글