485. Max Consecutive Ones最长的连续1的个数
[抄题]:
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.
[暴力解法]:
用temp,result,结果发现写起来很麻烦
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
[一句话思路]:
还是局部最大值+全局最大值的思路。用三元表达式解决局部变量的连续和中断问题
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 是maxHere + 1 统计,不是n + 1,这是是乐至?
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
用三元表达式解决局部变量的连续和中断问题
[复杂度]:Time complexity: O(n) Space complexity: O(1)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
487. Max Consecutive Ones II 好像不是数学就是两根指针吧
[LC给出的题目变变变]:
[代码风格] :
class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
//cc
if (nums == null || nums.length == 0) {
return 0;
}
//ini
int max = 0;
int maxHere = 0;
//for loop
for (int n : nums) {
max = Math.max(max, maxHere = n == 0 ? 0 : maxHere + 1);
}
//return
return max;
}
}
485. Max Consecutive Ones最长的连续1的个数的更多相关文章
- [LeetCode]485. Max Consecutive Ones 找到最大的连续的1的个数
题目描述 输入只有0和1的数组(长度为正整数,且<10000),找到最大的连续1的个数 比如[1,1,0,1,1,1],输出3 思路 遍历数组,统计当前连续个数curCount和最大连续值max ...
- 485. Max Consecutive Ones - LeetCode
Question 485. Max Consecutive Ones Solution 题目大意:给一个数组,取连续1的最大长度 思路:遍历数组,连续1就加1,取最大 Java实现: public i ...
- 【leetcode】485. Max Consecutive Ones
problem 485. Max Consecutive Ones solution1: class Solution { public: int findMaxConsecutiveOnes(vec ...
- 485. Max Consecutive Ones【easy】
485. Max Consecutive Ones[easy] Given a binary array, find the maximum number of consecutive 1s in t ...
- 485 Max Consecutive Ones 最大连续1的个数
给定一个二进制数组, 计算其中最大连续1的个数.示例 1:输入: [1,1,0,1,1,1]输出: 3解释: 开头的两位和最后的三位都是连续1,所以最大连续1的个数是 3.注意: 输入的数组只包 ...
- 485. Max Consecutive Ones
题目 Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: ...
- 485. Max Consecutive Ones@python
Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1, ...
- LeetCode 485. Max Consecutive Ones (最长连续1)
Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1, ...
- 485. Max Consecutive Ones最大连续1的个数
网址:https://leetcode.com/problems/max-consecutive-ones/ 很简单的一题 class Solution { public: int findMaxCo ...
随机推荐
- 【个人吐槽】C、Delphi、C#、java 摘抄
作为个人的一个感受就是,在win平台上开发软件,别再他妈的用MFC了,不适合新手,上手太难.你妹,实现一个半透明的功能,一堆代码,而C#就他妈的几行话.靠. 似乎很多人都觉得Delphi已经没落了.过 ...
- DNS域名解析负载均衡
- HDU - 6333:Harvest of Apples (组合数前缀和&莫队)
There are n n apples on a tree, numbered from 1 1 to n n . Count the number of ways to pick at most ...
- linux之管道
1. 进程间通信概述 进程是一个独立的资源分配单元,不同进程之间的资源是独立的,没有关联,不能在一个进程中直接访问另一个进程的资源.进程不是孤立的,不同的进程需要进行信息的交互和状态的传递等,因此需要 ...
- 窗口玻璃特效,半透明窗口,使用DWM实现Aero Glass效果
转自:http://blog.csdn.net/ntwilford/article/details/5656633 从Windows Vista开始,Aero Glass效果被应用在了Home Pre ...
- Git克隆、修改、更新项目,及查看项目地址命令
第一步:在本地新建一个文件夹,作为本地仓库,如“texzt”,直接打开该文件夹,并单击右键,选择git bash here 则可以直接进入到该文件夹目录下. 第二步:将本地仓库初始化,命令:git i ...
- Dawn 阿里开源前端开发构建工具
Dawn 取「黎明.破晓」之意,原为「阿里云·业务运营团队」内部的前端构建和工程化工具,现已完全开源.它通过pipeline 和 middleware 将开发过程抽象为相对固定的阶段和有限的操作,简化 ...
- FastAdmin + uni-app
FastAdmin + uni-app FastAdmin https://www.FastAdmin.net uni-app http://uniapp.dcloud.io/ 自己先挖个坑.
- 利用 Excel 写 C51 的宏定义
利用 Excel 写 C51 的宏定义 填好占空比,自动生成宏. #define LIGHT_LEVEL_00 0xFF #define LIGHT_LEVEL_10 0xE5 #define LIG ...
- dubbox消费者启动成功,却无法连接注册中心
使用dubbox作为服务提供端很好实现,因为git的说明和网上有很多的例子可供参考,但是消费端都一笔带过,简单得很,初学者往往以为只要配置如下3样东西就够了: <?xml version=&qu ...