- 1. 题目
- 2. 读题(需要重点注意的东西)
- 3. 解法
- 4. 可能有帮助的前置习题
- 5. 所用到的数据结构与算法思想
- 6. 总结
思路:因为是0和1组成的数组,如果是1,则n++;否则,判断n和目前最长长度max,如果n>max,将n的值赋值给max;最后需要注意,在输出max前,还需要判断n和max的大小。思考为什么?
3. 解法解法:
class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
int n = 0;
int max = 0;
for(int i = 0; i < nums.length; i++){
if(nums[i] == 1){
n++;
}else{
if(n > max) {
max = n;
}
n = 0;
}
}
if(n > max) max = n;
return max;
}
}
4. 可能有帮助的前置习题
5. 所用到的数据结构与算法思想
- Java数据结构—Array(数组))


![[LeetCode]485. 最大连续 1 的个数(java实现) [LeetCode]485. 最大连续 1 的个数(java实现)](http://www.mshxw.com/aiimages/31/319296.png)
