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 ...
随机推荐
- Tomcat启动超时问题Server Tomcat v7.0 Server at localhost was unable to start within 45 seconds
使用Eclipse启动Tomcat时出现启动超时的问题如下所示: Server Tomcat v7.0 Server at localhost was unable to start within 4 ...
- Block Towers (思维实现)
个人心得:这题没怎么看,题意难懂.后面比完再看的时候发现很好做但是怎么卡时间是个问题. 题意:就是有m个可以用2层积木的,n个可以用三层积木的,但是他们不允许重复所以可以无限添加. 比如 3 2 一开 ...
- AngularJS方法 —— angular.bootstrap
描述: 此方法用于手动加载angularjs模板 (官方翻译:注意基于端到端的测试不能使用此功能来引导手动加载,他们必须使用ngapp. angularjs会检测这个模板是否被浏览器加载或者加载多次并 ...
- 监听Documents文件夹内文件发生改变
// 当Documents内文件发生改变时,启动计时器,每秒计算一次大小,当大小不发生改变时说明传输完毕,就开始刷新. @property (nonatomic, strong) NSTimer *t ...
- Xcode7 修改项目名完全攻略
1.先把整个工程文件夹名改为新的工程名. 2 .将旧项目文件夹和Tests文件名夹修改为新的名称,修改后如下图所示 3.右击 ,选择“show content package”(中文:显示包内容),看 ...
- 浅谈Sql各种join的用法
1.left join.right join.inner join三者区别 left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录 right join(右联接) 返回包括右 ...
- laravel 网站速率优化
https://segmentfault.com/a/1190000009954966
- gen_server模块
转自http://www.xuebuyuan.com/2132233.html
- 【转】windows下mysql5.1忘记root密码解决方法
步骤如下:1.停止mysql服务(以管理员身份,在cmd命令行下运行) net stop mysql D:\>net stop mysql MySQL 服务正在停止. MySQL 服务已成功停止 ...
- spring-aop + memcached 的简单实现
一般情况下,java程序取一条数据是直接从数据库中去取,当数据库达到一定的连接数时,就会处于排队等待状态,某些在一定时间内不会发生变化的数据,完全没必要每次都从数据库中去取,使用spring-aop ...