485. Max Consecutive Ones【easy】
485. Max Consecutive Ones【easy】
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.
Note:
- The input array will only contain
0
and1
. - The length of input array is a positive integer and will not exceed 10,000
解法一:
- class Solution {
- public:
- int findMaxConsecutiveOnes(vector<int>& nums) {
- int max = ;
- int temp = ;
- for (int i = ; i < nums.size(); ++i)
- {
- if (nums[i] == )
- {
- temp++;
- max = temp > max ? temp : max;
- }
- else
- {
- temp = ;
- }
- }
- return max;
- }
- };
思路很简单:是1就累加并且判断是否需要更新max,不是1就把累加和归为0,继续遍历。
解法二:
- public int findMaxConsecutiveOnes(int[] nums) {
- int maxHere = , max = ;
- for (int n : nums)
- max = Math.max(max, maxHere = n == ? : maxHere + );
- return max;
- }
大神解释如下:
The idea is to reset maxHere
to 0 if we see 0, otherwise increase maxHere
by 1
The max of all maxHere
is the solution
110111
^ maxHere = 1
110111
.^ maxHere = 2
110111
..^ maxHere = 0
110111
...^ maxHere = 1
110111
....^ maxHere = 2
110111
.....^ maxHere = 3
解法三:
- int findMaxConsecutiveOnes(int* nums, int numsSize) {
- int max = ;
- int sum = ;
- for (int i=; i<numsSize; i++)
- {
- sum = (sum+nums[i])*nums[i];
- if(max<sum){max=sum;}
- }
- return max;
- }
这方法更牛逼,大神解释如下:Use the fact that multiplication with 0 resets everything..
485. Max Consecutive Ones【easy】的更多相关文章
- 479. Second Max of Array【easy】
Find the second max number in a given array. Notice You can assume the array contains at least two n ...
- 【leetcode】485. Max Consecutive Ones
problem 485. Max Consecutive Ones solution1: class Solution { public: int findMaxConsecutiveOnes(vec ...
- 121. Best Time to Buy and Sell Stock【easy】
121. Best Time to Buy and Sell Stock[easy] Say you have an array for which the ith element is the pr ...
- 605. Can Place Flowers【easy】
605. Can Place Flowers[easy] Suppose you have a long flowerbed in which some of the plots are plante ...
- 561. Array Partition I【easy】
561. Array Partition I[easy] Given an array of 2n integers, your task is to group these integers int ...
- 217. Contains Duplicate【easy】
217. Contains Duplicate[easy] Given an array of integers, find if the array contains any duplicates. ...
- 521. Longest Uncommon Subsequence I【easy】
521. Longest Uncommon Subsequence I[easy] Given a group of two strings, you need to find the longest ...
- 170. Two Sum III - Data structure design【easy】
170. Two Sum III - Data structure design[easy] Design and implement a TwoSum class. It should suppor ...
- 160. Intersection of Two Linked Lists【easy】
160. Intersection of Two Linked Lists[easy] Write a program to find the node at which the intersecti ...
随机推荐
- 【费马小定理+矩阵快速幂】HDU4549——M斐波那契数列
[题目大意] M斐波那契数列F[n]是一种整数数列,它的定义如下:F[0] = aF[1] = bF[n] = F[n-1] * F[n-2] ( n > 1 )现在给出a, b, n,求出F[ ...
- 1.5 JSP标准标签库(JSTL)(核心标签 out、set、remove、if、choose、forEach、forTokens、redirect)
JSTL(JavaServer Page Standard Tag Library):JSP标准标签库.它封装了JSP应用的通用核心功能. 1.准备工作 使用JSTL前需要下载所需文件,下载地址及安 ...
- iOS 简单易用的二维码扫描及生成二维码三方控件LFQRCode,可灵活自定义UI
一.扫码 扫描的控件是一个view,使用者只需贴在自己的控制器内即可.其他UI用户可在自己控制器随便添加.代码如下 - (void)viewDidLoad { [super viewDidLoad]; ...
- linux-改变文件属主权限-chown
http://www.cnblogs.com/peida/archive/2012/12/04/2800684.html chown将指定文件的拥有者改为指定的用户或组,用户可以是用户名或者用户ID: ...
- 一个简单的WeakList的实现
有的时候,我们会使用到WeakList,它和传统的List不同的是,保存的是对象的弱应用,在WeakList中存储的对象会被GC回收,在一些和UI相关的编程的地方会用到它(弱事件,窗体的通知订阅等). ...
- menuStrip鼠标经过自动显示菜单
//--------------------------------------------------------------------------------- private void For ...
- java中的注解详解和自定义注解
一.java中的注解详解 1.什么是注解 用一个词就可以描述注解,那就是元数据,即一种描述数据的数据.所以,可以说注解就是源代码的元数据.比如,下面这段代码: @Override public Str ...
- 剪切Postscript图片中的多余边框
最近用plplot画图,其cairo ps库生成的ps图片总是不能合理地剪切掉多余的边框,于是乎自己写了一个小脚本epscrop,用修改ps图的BoundingBox. #!/bin/bash # c ...
- 设计工作-Axure
1,百度百科 http://baike.baidu.com/view/3332366.htm?fromtitle=axure&fromid=5056136&type=syn 2,官方网 ...
- PHPCMS V9管理员password忘记怎样改动
一般的虚拟主机商都提供了PHPmyAdmin,选择你站点数据库.然后选择v9_admin这个表. 编辑 password,变成:fa3250300be9b7ab0848257f3cbb06e7 enc ...