Leetcode刷题(Max Consecutive Ones)
来源:互联网 发布:ubuntu 16 安装Unity 编辑:程序博客网 时间:2024/06/16 10:00
问题描述:Given a binary array, find the maximum number of consecutive 1s in this array.
即 找到二进制数组中最多的连续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.
Note:
- The input array will only contain
0
and1
. - The length of input array is a positive integer and will not exceed 10,000
原理流程见图片
根据流程图,代码很简单就可以得出:
public class Solution { public int findMaxConsecutiveOnes(int[] nums) { int n=0,n1=0; for(int x=0;x<nums.length;x++) { if (nums[x]==1) { n1++; } else if(nums[x]==0) { if(n1>n) { n=n1; n1=0; } else n1=0; } } return n>n1?n:n1; }}return采用三元运算表达式简化结果。
Submission Result: Accepted
1 0
- Leetcode刷题(Max Consecutive Ones)
- LEETCODE--Max Consecutive Ones
- LeetCode: Max Consecutive Ones
- 【leetcode】Max Consecutive Ones
- LeetCode:Max Consecutive Ones
- LeetCode Max Consecutive Ones
- LeetCode Max Consecutive Ones
- leetCode-Max Consecutive Ones
- LeetCode刷题【Array】 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
- 名字常量_字面值常量_常量
- 父子组件
- Spring mvc url-pattern 和@RequestMapping 映射的小问题
- Android点击EditText文本框之外任何地方隐藏键盘的解决办法
- 给要入门量化分析的人一些建议(转)
- Leetcode刷题(Max Consecutive Ones)
- <转>斯坦福大学Andrew Ng教授主讲的《机器学习》公开课观后感
- Min Stack
- TCP长连接与短连接的区别
- Sentinel-1相关链接
- 题目1448:Legal or Not
- JCO java connector
- 解决使用securecrt登录linux系统中文乱码问题
- SPEC文件中的常用路径以及宏变量