2 Star 10 Fork 2

CG国斌 / myleetcode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
_485.java 1.40 KB
一键复制 编辑 原始数据 按行查看 历史
CG国斌 提交于 2020-03-15 20:56 . 新增 数组&字符串 题解
package com.hit.basmath.learn.array_and_string;
/**
* 485. Max Consecutive Ones
* <p>
* Given a binary array, find the maximum number of consecutive 1s in this array.
* <p>
* Example 1:
* <p>
* Input: [1,1,0,1,1,1]
* <p>
* Output: 3
* <p>
* Explanation: The first two digits or the last three digits are consecutive 1s.
* The maximum number of consecutive 1s is 3.
* <p>
* Note:
* <p>
* The input array will only contain 0 and 1.
* The length of input array is a positive integer and will not exceed 10,000
*/
public class _485 {
public int findMaxConsecutiveOnes(int[] nums) {
int maxConsecutiveLength = 0;
int tempConsecutiveLength = 0;
/**
* Iterate all element of nums
*/
for (int num : nums) {
/**
* If num == 1 is true, let tempConsecutiveLength++
* and let maxConsecutiveLength is the largest of maxConsecutiveLength and tempConsecutiveLength
*/
if (num == 1) {
tempConsecutiveLength++;
maxConsecutiveLength = Math.max(maxConsecutiveLength, tempConsecutiveLength);
} else {
/**
* If num == 1 is false, it's mean num is 0,
* so let tempConsecutiveLength = 0 always
*/
tempConsecutiveLength = 0;
}
}
return maxConsecutiveLength;
}
}
Java
1
https://gitee.com/guobinhit/myleetcode.git
git@gitee.com:guobinhit/myleetcode.git
guobinhit
myleetcode
myleetcode
master

搜索帮助