leetCode-Max Consecutive Ones
来源:互联网 发布:linux项目部署 编辑:程序博客网 时间:2024/05/22 01:43
Description:
Given a binary array, find the maximum number of consecutive 1s in this array.
Example1:
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 and 1.
The length of input array is a positive integer and will not exceed 10,000
My Solution:
class Solution { public int findMaxConsecutiveOnes(int[] nums) { int count = 0; int maxN = 0; for(int i : nums){ if(i == 1){ count++; if(count > maxN){ maxN = count; } }else{ count = 0; } } return maxN; }}
改进:
public class Solution { public int findMaxConsecutiveOnes(int[] nums) { int result = 0; int count = 0; for (int i = 0; i < nums.length; i++) { if (nums[i] == 1) { count++; //替换为了Math.max()函数,更简洁 result = Math.max(count, result); } else count = 0; } return result; }}
总结:
注意每次碰到1的时候增加count,然后求max(count,maxN),然后每次num = 0将count置0就可以了。
阅读全文
0 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 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题解:Max Consecutive Ones
- LeetCode 485. Max Consecutive Ones
- EventBus使用详解(二)
- 浏览器兼容问题
- iOS开发中 中NS_ENUM与NS_OPTIONS区别
- 顶尖程序员的风雨20年:我所积累的20条编程经验
- Leetcode:26 Remove Duplicates from Sorted Array
- leetCode-Max Consecutive Ones
- 直播系统以及上传图片鉴黄原理简述
- js 操作表单
- Somethings about the Process scheduler in Linux
- 关于unity自带延时函数
- 数据结构实验之图论二:图的深度遍历
- HTTP中Get与Post的区别
- Android 系统权限使用详解
- Android中补间动画1----Animation的基本使用代码实现(平移,缩放,渐变,旋转)