[力扣] 算法 1004 (C#)

1004. 最大连续1的个数 III

public class Solution {
    public int LongestOnes(int[] A, int K) {
        int max = 0;
        int zero = 0;
        int from = 0, to = 0;
        while (to < A.Length)
        {
            if (A[to] == 0)
            {
                zero++;
                while (zero > K)
                {
                    if (A[from] == 0)
                    {
                        zero--;
                    }
                    from++;
                }
            }
            to++;
            if (to - from > max)
            {
                max = to - from;
            }
        }
        return max;
    }
}

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据