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 ...
随机推荐
- 快排的python实现
快排的python实现 #python 2.7 def quick_sort(L): if len(L) <= 1: return L else: return quick_sort([lt f ...
- 第九集 经验风险最小化(ERM)
实在写不动了,将word文档转换为PDF直接截图了... 版权声明:本文为博主原创文章,未经博主允许不得转载.
- python库之_thread
官方参考文档:https://docs.python.org/3.7/library/_thread.html _thread库方法 (1) _thread.error (2)_thread.Lock ...
- 排序算法总结(C#版)
算法质量的衡量标准: 1:时间复杂度:分析关键字比较次数和记录的移动次数: 2:空间复杂度:需要的辅助内存: 3:稳定性:相同的关键字计算后,次序是否不变. 简单排序方法 .直接插入排序 直接插入排序 ...
- python 递归函数和二分查找
1.初始递归 递归属于函数中的一种特殊函数,功能迅速并且干净利落,在函数中递归的基本就是在函数中调用自己本身 def func(): print(111) func() func()#将会无限循环‘1 ...
- git 统计命令
git log --author='user' --pretty=tformat: --numstat --since='2018-01-01' --before='2018-10-10' | gaw ...
- linux 系统创建软连接
ln -s /data/var/ /usr/local/smokeping/var 需求:/var/本身在/usr/local/smokeping/var下,想要把/usr/local/smokepi ...
- Ubuntu-14.04-QT开发环境搭建-(一)
Ubuntu 14.04 QT 开发环境搭建 一 . 软件:qt-creator-linux-x86-opensource-2.7.0.binqt-everywhere-opensource-src- ...
- debian修改主机名
hostnamectl set-hostname aaa 或者 vim /etc/hostname 记得更新 /etc/hosts
- FPGA前世今生(一)
关于FPGA,我想做硬件的或多或少都听过.从上世纪80年代算来,FPGA已走过了30多个年头.我们以FPGA两大生产厂商,两大巨头之一的INTEL(altera)公司的FPGA为例,为大家逐步介绍FP ...