lintcode/leetcode由易至难第13题:Max Consecutive Ones
来源:互联网 发布:宏观审慎管理知乎 编辑:程序博客网 时间:2024/06/05 08:41
Problem:
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.Code:
public class Solution { public int findMaxConsecutiveOnes(int[] nums) { int count = 0; int count2 = 0; for(int i = 0; i < nums.length; i++){ if (nums[i] == 0){ count2 = 0; } if (nums[i] == 1){ count2 += 1; } count = Math.max(count,count2); } return count; }}
Code2:
public class Solution { public int findMaxConsecutiveOnes(int[] nums) { int count = 0; int count2 = 0; for(int n : nums){ count = Math.max(count,count2 = n == 0 ? 0 : count2 + 1); //高手倾向于用增强型for循环和三目运算符写 } return count; }}
阅读全文
0 0
- lintcode/leetcode由易至难第13题: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 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
- iOS 设置导航透明的两种方法
- linux相关命令
- leetcode 183. Customers Who Never Order
- Qt之窗口动画(下坠、抖动、透明度)
- java 读写excel文件
- lintcode/leetcode由易至难第13题:Max Consecutive Ones
- nginx日志切割脚本
- ElasticSearch5+logstash的logstash-input-jdbc实现mysql数据同步
- HTML应用推广-自动识别手机系统(Android/iOS)
- JAVA8 Stream API 进阶
- Java 的Integer、int与new Integer到底怎么回事?
- 创建linux用户
- 2017最新App上架流程(首次,被拒后重新提交)(亲测)
- 学习JAVA,如何从入门到精通