【LeetCode】485.Max Consecutive Ones_EASY(九)
来源:互联网 发布:wind数据库试用账号 编辑:程序博客网 时间:2024/06/18 01:28
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的最大值即可。
代码:
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); }}
完成。
0 0
- 【LeetCode】485.Max Consecutive Ones_EASY(九)
- 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
- 485. Max Consecutive Ones [LeetCode]
- 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
- leetcode 485. Max Consecutive Ones
- LeetCode-485. Max Consecutive Ones
- [LeetCode]485. Max Consecutive Ones
- LeetCode 485. Max Consecutive Ones
- 【LeetCode】463.Island Perimeter_EASY(七)
- 【LeetCode】292.Nim Game_EASY(八)
- iOS开发—蓝牙4.0(BLE)与外设连接及收发数据的流程
- 基于mini2440嵌入式linux上整合一套Domoticz智能家居系统(四)交叉编译OpenSSL、c-ares、e2fsprogs和mosquitto
- 数据库学习第五课
- 【LeetCode】485.Max Consecutive Ones_EASY(九)
- 【LeetCode】136.Single Number_EASY(十)
- QNX的应用移植迁移到Linux
- docker容器中安装vim 、telnet、ifconfig命令
- logstash nginx error access 日志处理
- Qt在嵌入式系统QNX的HMI应用方案
- Discuz!开发之页面插件嵌入点列表
- HDU1055Color a tree
- SQL Server 判断及增加列的默认值约束