国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

[LeetCode] 487. Max Consecutive Ones II

nanfeiyan / 3189人閱讀

Problem

Given a binary array, find the maximum number of consecutive 1s in this array if you can flip at most one 0.

Example 1:
Input: [1,0,1,1,0]
Output: 4
Explanation: Flip the first zero will get the the maximum number of consecutive 1s.

After flipping, the maximum number of consecutive 1s is 4.

Note:

The input array will only contain 0 and 1.
The length of input array is a positive integer and will not exceed 10,000
Follow up:
What if the input numbers come in one by one as an infinite stream? In other words, you can"t store all numbers coming from the stream as it"s too large to hold in memory. Could you solve it efficiently?

Solution
class Solution {
    public int findMaxConsecutiveOnes(int[] nums) {
        int max = 0, lastZeroIndex = -1;
        int l = 0, r = 0;
        while (r < nums.length) {
            if (nums[r] == 0) {
                l = lastZeroIndex+1;
                lastZeroIndex = r;
            }
            max = Math.max(max, r-l+1);
            r++;
        }                                                     
        return max;             
    }
}

文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。

轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/72691.html

相關文章

  • Max Consecutive Ones

    Max Consecutive Ones 題目鏈接:https://leetcode.com/problems... public class Solution { public int findMaxConsecutiveOnes(int[] nums) { // loop invariant: // global is the max so far, ...

    array_huang 評論0 收藏0
  • Leetcode PHP題解--D67 485. Max Consecutive Ones

    摘要:題目鏈接題目分析給定一個二進制數組只含有和的數組,返回最長的串。思路逐個遍歷,若為則計數。遇到則判斷當前計數是否大于之前記錄的最大數字,并置零。最終代碼若覺得本文章對你有用,歡迎用愛發電資助。 D67 485. Max Consecutive Ones 題目鏈接 485. Max Consecutive Ones 題目分析 給定一個二進制數組(只含有0和1的數組),返回最長的1串。 思...

    曹金海 評論0 收藏0
  • LeetCode 485:連續最大1的個數 Max Consecutive Ones(python

    摘要:示例輸入輸出解釋開頭的兩位和最后的三位都是連續,所以最大連續的個數是注意輸入的數組只包含和。輸入數組的長度是正整數,且不超過。 公眾號:愛寫bug 給定一個二進制數組, 計算其中最大連續1的個數。 Given a binary array, find the maximum number of consecutive 1s in this array. 示例 1: 輸入: [1,1,0...

    youkede 評論0 收藏0
  • LeetCode 485:連續最大1的個數 Max Consecutive Ones(python

    摘要:示例輸入輸出解釋開頭的兩位和最后的三位都是連續,所以最大連續的個數是注意輸入的數組只包含和。輸入數組的長度是正整數,且不超過。 公眾號:愛寫bug 給定一個二進制數組, 計算其中最大連續1的個數。 Given a binary array, find the maximum number of consecutive 1s in this array. 示例 1: 輸入: [1,1,0...

    TesterHome 評論0 收藏0
  • LeetCode 485:連續最大1的個數 Max Consecutive Ones(python

    摘要:示例輸入輸出解釋開頭的兩位和最后的三位都是連續,所以最大連續的個數是注意輸入的數組只包含和。輸入數組的長度是正整數,且不超過。 公眾號:愛寫bug 給定一個二進制數組, 計算其中最大連續1的個數。 Given a binary array, find the maximum number of consecutive 1s in this array. 示例 1: 輸入: [1,1,0...

    RichardXG 評論0 收藏0

發表評論

0條評論

最新活動
閱讀需要支付1元查看
<