资讯专栏INFORMATION COLUMN

Max Consecutive Ones

array_huang / 697人阅读

Max Consecutive Ones

题目链接:https://leetcode.com/problems...

</>复制代码

  1. public class Solution {
  2. public int findMaxConsecutiveOnes(int[] nums) {
  3. // loop invariant:
  4. // global is the max so far, local is the max including current nums[i]
  5. int global = 0;
  6. int local = 0;
  7. for(int i = 0; i < nums.length; i++) {
  8. local = (nums[i] == 1 ? local + 1 : 0);
  9. global = Math.max(global, local);
  10. }
  11. return global;
  12. }
  13. }
Max Consecutive Ones II

题目链接:https://leetcode.com/problems...

</>复制代码

  1. public class Solution {
  2. public int findMaxConsecutiveOnes(int[] nums) {
  3. // 2 points, slide window
  4. int i = 0, j = 0;
  5. int global = 0;
  6. // count the number of flip
  7. int count = 0;
  8. while(j < nums.length) {
  9. if(nums[j++] == 0) count++;
  10. while(count > 1) if(nums[i++] == 0) count--;
  11. global = Math.max(global, j - i);
  12. }
  13. return global;
  14. }
  15. }

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/66597.html

相关文章

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

    摘要:题目链接题目分析给定一个二进制数组只含有和的数组,返回最长的串。思路逐个遍历,若为则计数。遇到则判断当前计数是否大于之前记录的最大数字,并置零。最终代码若觉得本文章对你有用,欢迎用爱发电资助。 D67 485. Max Consecutive Ones 题目链接 485. Max Consecutive Ones 题目分析 给定一个二进制数组(只含有0和1的数组),返回最长的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:连续最大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元查看
<