Question
485. Max Consecutive Ones
Solution
题目大意:给一个数组,取连续1的最大长度
思路:遍历数组,连续1就加1,取最大
Java实现:
public int findMaxConsecutiveOnes(int[] nums) {if (nums == null) return 0;int result = 0;int tmp = 0;for (int i : nums) {if (i == 1) {tmp++;} else {result = tmp > result? tmp: result;tmp = 0;}}result = tmp > result? tmp: result;return result;
}