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

資訊專欄INFORMATION COLUMN

Max Consecutive Ones

array_huang / 565人閱讀

Max Consecutive Ones

題目鏈接:https://leetcode.com/problems...

public class Solution {
    public int findMaxConsecutiveOnes(int[] nums) {
        // loop invariant: 
        // global is the max so far, local is the max including current nums[i]
        int global = 0;
        int local = 0;
        for(int i = 0; i < nums.length; i++) {
            local = (nums[i] == 1 ? local + 1 : 0);
            global = Math.max(global, local);
        }
        
        return global;
    }
}
Max Consecutive Ones II

題目鏈接:https://leetcode.com/problems...

public class Solution {
    public int findMaxConsecutiveOnes(int[] nums) {
        // 2 points, slide window
        int i = 0, j = 0;
        int global = 0;
        // count the number of flip
        int count = 0;
        while(j < nums.length) {
            if(nums[j++] == 0) count++;
            while(count > 1) if(nums[i++] == 0) count--;
            global = Math.max(global, j - i);
        }
        
        return global;
    }
}

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

轉(zhuǎn)載請注明本文地址:http://specialneedsforspecialkids.com/yun/66597.html

相關(guān)文章

  • Leetcode PHP題解--D67 485. Max Consecutive Ones

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

    曹金海 評論0 收藏0
  • [LeetCode] 487. Max Consecutive Ones II

    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: 4Explanation: Flip the first zero will get the ...

    nanfeiyan 評論0 收藏0
  • LeetCode 485:連續(xù)最大1的個數(shù) Max Consecutive Ones(python

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

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

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

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

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

    RichardXG 評論0 收藏0

發(fā)表評論

0條評論

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