【LeetCode】485.Max Consecutive Ones_EASY(九)
2017-04-08 05:47
495 查看
485.Max Consecutive Ones
Description: 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:
1. The input array will only contain 0 and 1.
2. The length of input array is a positive integer and will not exceed 10,000.
思路:设连续1的个数为number,遍历数组
数组元素为1时:
1.不是数组最后一个元素时,number++;
2.是数组最后一个元素时,number++,然后进行add操作。
数组元素为0时:
进行add操作,然后将number设为0。
最后返回List的最大值即可。
代码:
完成。
Description: 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:
1. The input array will only contain 0 and 1.
2. The length of input array is a positive integer and will not exceed 10,000.
思路:设连续1的个数为number,遍历数组
数组元素为1时:
1.不是数组最后一个元素时,number++;
2.是数组最后一个元素时,number++,然后进行add操作。
数组元素为0时:
进行add操作,然后将number设为0。
最后返回List的最大值即可。
代码:
public class Solution { public int findMaxConsecutiveOnes(int[] nums) { List<Integer> conse = new ArrayList<Integer>(); int number=0; for(int i=0;i<nums.length;i++){ if(nums[i]==1){ if(i==nums.length-1){ number++; conse.add(number); }else number++; }else{ conse.add(number); number=0; } } return Collections.max(conse); } }
完成。
相关文章推荐
- leetcode 485. Max Consecutive Ones(easy)
- 495. Teemo Attacking【middle】&& 485. Max Consecutive Ones【easy】 02-24
- leetcode 485. Max Consecutive Ones(C语言)
- LeetCode 485. Max Consecutive Ones
- leetcode 485. Max Consecutive Ones
- leetcode485. Max Consecutive Ones
- LeetCode 485. Max Consecutive Ones
- Leetcode 485. Max Consecutive Ones
- LeetCode 485. Max Consecutive Ones(Java)
- 【LeetCode】485. Max Consecutive Ones(最大连续数之和)
- [LeetCode]485. Max Consecutive Ones(最大连续为1的数量)
- leetcode485. Max Consecutive Ones
- 【LeetCode】485.Max Consecutive Ones解题报告
- LeetCode 485. Max Consecutive Ones
- [LeetCode]485. Max Consecutive Ones
- 【LeetCode】485. Max Consecutive Ones
- LeetCode 485. Max Consecutive Ones
- LeetCode -485. Max Consecutive Ones - 思路详解 - C++
- LeetCode 485:Max Consecutive Ones(连续1的最大个数,边界条件)
- [leetcode]: 485. Max Consecutive Ones