O(n) solution C++ Simple Approach


  • 0
    K
    
    class Solution {
    public:
        int findMaxConsecutiveOnes(vector<int>& nums) {
            int mx = 0, sum = 0;
            for(int i=0; i<nums.size(); i++){
                if(nums[i] == 0)
                    sum = 0;
                else{
                    sum++;
                    mx = max(sum ,mx);
                }
            }
            return mx;
        }
    };
    
    

Log in to reply
 

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