My Swift Solution


  • 0
    S

    if num == 1 then counter ++

    if num == 0 then refresh counter and put counter into result

    if counter is not greater than the last result then .. skip it

    class Solution {
        func findMaxConsecutiveOnes(_ nums: [Int]) -> Int {
            var result = 0
            var counter = 0
    
            for num in nums {
                counter += num == 1 ? num : 0
                counter = num == 0 ? 0: counter
                result = counter < result ? result : counter
            }
            return result
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.