1. 程式人生 > 其它 >力扣刷題1/70

力扣刷題1/70

陣列 2/3

力扣283----移動零

最大連續1的個數
比較基礎的一道題,要注意不要忘記統計最大連續1的個數。

class Solution {
public:
    int findMaxConsecutiveOnes(vector<int>& nums) {
    //maxcount 最大連續1的個數 count 當前連續1的個數
        int maxcount = 0; int count = 0;
        for(int i = 0; i < nums.size(); i++) {
            if(nums[i] == 1) {
                count ++;
            } else {
            	//容易忘記統計最大連續1個數
                maxcount = max(maxcount,count);
                
                count = 0;
             }
        }
        maxcount = max(maxcount,count);
        return maxcount;
    }
};

在這裡插入圖片描述