leetcode485. Max Consecutive Ones
来源:互联网 发布:java在线 编辑:程序博客网 时间:2024/04/30 03:54
485. Max Consecutive Ones
Given a binary array, find the maximum number of consecutive 1s in this array.
Example 1:Input: [1,1,0,1,1,1]Output: 3Explanation: The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
解法一
从头开始计算连续1的长度。
public class Solution { public int findMaxConsecutiveOnes(int[] nums) { if (nums == null || nums.length == 0) { return 0; } int index = 0; int max = 0; for (int i = 0; i < nums.length; i++) { while(i < nums.length && nums[i] == 1) { index++; i++; } if (index > max) { max = index; } index = 0; } return max; }}
解法二
如果是1的话,sum值加1;如果是0,sum变为0,又重新开始计算。
public class Solution { public int findMaxConsecutiveOnes(int[] nums) { if (nums == null || nums.length == 0) { return 0; } int max = 0; int sum = 0; for (int i = 0; i < nums.length; i++) { sum = (sum + nums[i]) * nums[i]; if (sum > max) { max = sum; } } return max; }}
0 0
- 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题解
- leetcode485 Max Consecutive Ones Java
- python--leetcode485. Max Consecutive Ones
- LeetCode485:Max Consecutive Ones 解答
- LeetCode485. Max Consecutive Ones有你真好
- 485. Max Consecutive Ones*
- 485. Max Consecutive Ones
- LEETCODE--Max Consecutive Ones
- 485. Max Consecutive Ones
- 485. Max Consecutive Ones
- 485. Max Consecutive Ones
- Max Consecutive Ones
- Iserver产品模块划分
- 使用nexus搭建maven私服、手动更新索引
- 网卡数据捕获
- JavaSE教程-04Java中循环语句for,while,do···while-练习2
- 多线程的简单认识(二)
- leetcode485. Max Consecutive Ones
- Codeforces 523C&D C.Name Quest【贪心】、D.Statistics of Recompressing Videos【模拟】
- JAVA异常初级的懵逼之路
- 最佳拟合直线
- ios开发 之 中文转拼音
- 批处理调用cacls修改文件权限
- 蓝桥杯算法提高——盾神与条状项链(链表)
- ionic开发——获取手机设备信息
- Linux关机命令