C++_AC_two pointers


  • 0
    class Solution {
    public:
    int findMaxConsecutiveOnes(vector<int>& nums) {
        if(nums.empty()) return 0;
        int MAX = 0, ans = 0;
        int pre = 0, cur = 0, zero = -1;
        for(int i = 0; i < nums.size(); i++){
            if(nums[i] == 0){
                if(zero != -1){
                    MAX = max(MAX, i - pre);//i-1 - pre + 1
                    pre = zero + 1;
                    zero = i;
                }else{
                    zero = i;
                }
            }
            if(zero == i || nums[i] == 1) cur = i;
        }
        return max(MAX, cur - pre + 1);
    }
    };

Log in to reply
 

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