LeetCode-485. Max Consecutive Ones
Given a binary array, find the maximum number of consecutive 1s in this array.
Input: [1,1,0,1,1,1] Output: 3 Explanation: The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
public class Solution { public int findMaxConsecutiveOnes(int[] nums) { if (nums == null) return 0; int sum = 0, b = 0; for (int v : nums) { b += v; if (v == 0) b = 0; if (sum < b) sum = b; } return sum; } }
作者:Pickle
声明:对于转载分享我是没有意见的,出于对博客园社区和作者的尊重一定要保留原文地址哈。
致读者:坚持写博客不容易,写高质量博客更难,我也在不断的学习和进步,希望和所有同路人一道用技术来改变生活。觉得有点用就点个赞哈。