成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

[LeetCode] 487. Max Consecutive Ones II

nanfeiyan / 3203人閱讀

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;             
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/72691.html

相關(guān)文章

  • 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 評(píng)論0 收藏0
  • Leetcode PHP題解--D67 485. Max Consecutive Ones

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

    曹金海 評(píng)論0 收藏0
  • LeetCode 485:連續(xù)最大1的個(gè)數(shù) Max Consecutive Ones(python

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

    youkede 評(píng)論0 收藏0
  • LeetCode 485:連續(xù)最大1的個(gè)數(shù) Max Consecutive Ones(python

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

    TesterHome 評(píng)論0 收藏0
  • LeetCode 485:連續(xù)最大1的個(gè)數(shù) Max Consecutive Ones(python

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

    RichardXG 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<