Leetcode Brush title Record [java]--485 Max consecutive Ones

Source: Internet
Author: User

First, preface

Second, the question 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 and digits or the last three digits is consecutive 1s.    The maximum number of consecutive 1s is 3.

Note:

  • The input array would be only contain and 0 1 .
  • The length of input array is a positive integer and would not exceed 10,000

Three, the thinking of solving problems

Iterates through the array, counting 1 consecutive cases, comparing with the current largest 1 cluster, returning a larger value, and the final result being the maximum value.

1 classSolution {2      Public intFindmaxconsecutiveones (int[] nums) {3         intresult = 0;4         intCount = 0;5          for(inti=0; i<nums.length; i++) {6             if(Nums[i] = = 1) {7count++;8result =Math.max (count, result);9             }Ten             ElseCount = 0; One         } A         returnresult; -     } -}

Leetcode Brush title Record [java]--485 Max consecutive Ones

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.