Max Consecutive Ones

Given a binary array, find the maximum number of consecutive 1s in this array.

Example 1:

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.

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

分析:计算1连续出现的最大次数。

思路:遍历统计次数,记录下连续出现的次数即可。

JAVA CODE

class Solution {
    public int findMaxConsecutiveOnes(int[] nums) {
        int times = 0, maxTimes = 0;
        for(int i = 0; i < nums.length; i++){
            if(nums[i] == 0){
                maxTimes = maxTimes > times ? maxTimes : times;
                times = 0;
            }else{
                times++;
            }
        }
        return maxTimes = maxTimes > times ? maxTimes : times;
    }
}
时间: 2024-10-14 21:27:27

Max Consecutive Ones的相关文章

LeetCode Max Consecutive Ones

原题链接在这里:https://leetcode.com/problems/max-consecutive-ones/ 题目: Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1,1,0,1,1,1] Output: 3 Explanation: The first two digits or the last three digits are co

485. Max Consecutive Ones (最大连续数) by Python

485. Max Consecutive Ones 题目: Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: 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 o

【Flume】【源码分析】flume中sink到hdfs,文件系统频繁产生文件,文件滚动配置不起作用? ERROR hdfs.BucketWriter: Hit max consecutive under-replication rotations (30)

[转载] http://blog.csdn.net/simonchi/article/details/43231891 ERROR hdfs.BucketWriter: Hit max consecutive under-replication rotations (30) 本人在测试hdfs的sink,发现sink端的文件滚动配置项起不到任何作用,配置如下: [plain] view plain copy print? a1.sinks.k1.type=hdfs a1.sinks.k1.cha

485. Max Consecutive Ones最长的连续1的个数

[抄题]: Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: 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. [

【leetcode】485. Max Consecutive Ones

problem 485. Max Consecutive Ones solution1: class Solution { public: int findMaxConsecutiveOnes(vector<int>& nums) { if(nums.empty()) return 0; int ans = 0, max = INT_MIN; for(int i=0; i<nums.size(); i++) { if(nums[i]==1) ans++; else if(nums

LeetCode_485. Max Consecutive Ones

485. Max Consecutive Ones Easy Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: 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

485. Max Consecutive Ones

Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: 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. Note: T

[LeetCode OJ]485. Max Consecutive Ones

Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: 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. Note: T

(leetcode题解)Max Consecutive Ones

Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: 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. Note: T

487. Max Consecutive Ones II

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, t