less compute times in C++


  • 0
    J

    It's quiet straightforward to counter the distance between first 1 and next 0. Time counting is 39ms.

    class Solution {
    public:
    int findMaxConsecutiveOnes(vector<int>& nums) {
    int counter = 0, max = 0;
    vector<int>::iterator it = nums.begin(), start1 = it;
    for (++it; it < nums.end(); ++ it) {
    if (*(it - 1) == 1 && it == 0) {
    counter = it - start1;
    max = max >= counter? max : counter;
    }
    else if (
    (it - 1) == 0 && *it == 1){
    start1 = it;
    }
    }

        if (*(it - 1) == 1) {
            counter = it - start1;
            max = max >= counter? max : counter;
        }
        
        return max;
    }
    

    };


Log in to reply
 

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