日野弥生:勉強しよう

LeetCode 485 - 最大连续1的个数

发表于2025年01月01日

#数组 #双指针

利用双指针法,通过双指针之间的距离求出长度。

class Solution {
public:
    int findMaxConsecutiveOnes(vector<int>& nums) {
        nums.push_back(false);
        int slow =0,fast=0,length=0;
        for(;fast<nums.size();fast++)
        {
            if(!nums[fast])
            {
                length= (fast-slow)>length?fast-slow:length;
                slow=fast+1;
            }
        }
        return length;
    }
};

フラッシュタブ:LeetCode

题目链接:https://leetcode.cn/problems/max-consecutive-ones/

上一篇

LeetCode 167 - 两数之和 II - 输入有序数组

下一篇

LeetCode 118 - 杨辉三角