C++ Simple Solution


  • 0
    N
    int findMaxConsecutiveOnes(vector<int>& nums) {
        int returnValue = 0;
        int counter = 0;
        int numSize = nums.size()-1;
        for (int i = 0; i < nums.size(); i++) {
            if (nums[i] == 1) {
                counter++;
                if (counter > returnValue) {
                    returnValue = counter;
                }
            }
            else {
                counter = 0;
                if (returnValue > numSize-i) {
                    break;
                }
            }
        }
        return returnValue;
    }                           
    

Log in to reply
 

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