leetcode485 Max Consecutive Ones Java
来源:互联网 发布:单页商城源码 编辑:程序博客网 时间:2024/05/16 18:13
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:
The input array will only contain 0 and 1.
The length of input array is a positive integer and will not exceed 10,000
解法1
遍历数组,找到最长的连续1。
public int findMaxConsecutivesOnes(int[] nums) { int max = 0; int count = 0; for(int n : nums) { if(n == 1) { count ++; if(count > max) max = count; }else count = 0; } return max; }
解法2
110111
^ count= 1
110111
. ^ count= 2
110111
. . ^ count= 0
110111
. . . ^ count= 1
110111
. . . . ^ count= 2
110111
. . . . . ^ count= 3
public int findMaxConsecutivesOnes(int[] nums) { int max = 0; int count = 0; for(int n : nums) { max = Math.max(max, count = n == 0 ? 0 : count+1); } return max; }
0 0
- leetcode485 Max Consecutive Ones Java
- leetcode485: Max Consecutive Ones
- leetcode485~Max Consecutive Ones
- leetcode485. Max Consecutive Ones
- Leetcode485. Max Consecutive Ones
- leetcode485. Max Consecutive Ones
- Max Consecutive Ones---LeetCode485
- LeetCode485.Max Consecutive Ones题解
- python--leetcode485. Max Consecutive Ones
- LeetCode485:Max Consecutive Ones 解答
- LeetCode485. Max Consecutive Ones有你真好
- leetcode oj java 485. Max Consecutive Ones
- LeetCode 485. Max Consecutive Ones(Java)
- 485. Max Consecutive Ones*
- 485. Max Consecutive Ones
- LEETCODE--Max Consecutive Ones
- 485. Max Consecutive Ones
- 485. Max Consecutive Ones
- UIViewController 相关生命周期总结 和 UIView 总结
- 主成分分析(PCA)
- 通俗理解卷积神经网络
- 如何从零开始搭建一套web集成测试环境
- 王垠:谈 Linux,Windows 和 Mac
- leetcode485 Max Consecutive Ones Java
- Markdown 教程示例
- 关于Java变量名前缀m、s等的解释
- 函数重载
- Android获取手机设备信息并区分真机与模拟器
- 操作系统(五)-----进程的状态以及状态之间的转换
- RedisUtil 工具类
- spring cloud学习资料~持续更新
- 古董变频器,你见过么?