本文主要是介绍[LeetCode] 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:
- 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子串的最大长度。
思路
class Solution {public int findMaxConsecutiveOnes(int[] nums) {int maxlen = 0;int curlen = 0;for(int num:nums){if(num == 1){if(++curlen > maxlen)maxlen = curlen;}elsecurlen = 0;}return maxlen;}
}
这篇关于[LeetCode] 485. Max Consecutive Ones的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!