485. Max Consecutive Ones【easy】

Given a binary array, find the maximum number of consecutive 1s in this array.

Example 1:

  1. Input: [1,1,0,1,1,1]
  2. Output: 3
  3. Explanation: The first two digits or the last three digits are consecutive 1s.
  4. The maximum number of consecutive 1s is 3.

Note:

  • The input array will only contain 0 and 1.
  • The length of input array is a positive integer and will not exceed 10,000

解法一:

  1. class Solution {
  2. public:
  3. int findMaxConsecutiveOnes(vector<int>& nums) {
  4. int max = ;
  5. int temp = ;
  6.  
  7. for (int i = ; i < nums.size(); ++i)
  8. {
  9. if (nums[i] == )
  10. {
  11. temp++;
  12. max = temp > max ? temp : max;
  13. }
  14. else
  15. {
  16. temp = ;
  17. }
  18. }
  19.  
  20. return max;
  21. }
  22. };

思路很简单:是1就累加并且判断是否需要更新max,不是1就把累加和归为0,继续遍历。

 

解法二:

  1. public int findMaxConsecutiveOnes(int[] nums) {
  2. int maxHere = , max = ;
  3. for (int n : nums)
  4. max = Math.max(max, maxHere = n == ? : maxHere + );
  5. return max;
  6. }

大神解释如下:

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

  1. 110111
  2. ^ maxHere = 1
  3. 110111
  4. .^ maxHere = 2
  5. 110111
  6. ..^ maxHere = 0
  7. 110111
  8. ...^ maxHere = 1
  9. 110111
  10. ....^ maxHere = 2
  11. 110111
  12. .....^ maxHere = 3

解法三:

  1. int findMaxConsecutiveOnes(int* nums, int numsSize) {
  2. int max = ;
  3. int sum = ;
  4. for (int i=; i<numsSize; i++)
  5. {
  6. sum = (sum+nums[i])*nums[i];
  7. if(max<sum){max=sum;}
  8. }
  9. return max;
  10. }

这方法更牛逼,大神解释如下:Use the fact that multiplication with 0 resets everything..

485. Max Consecutive Ones【easy】的更多相关文章

  1. 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 ...

  2. 【leetcode】485. Max Consecutive Ones

    problem 485. Max Consecutive Ones solution1: class Solution { public: int findMaxConsecutiveOnes(vec ...

  3. 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 ...

  4. 605. Can Place Flowers【easy】

    605. Can Place Flowers[easy] Suppose you have a long flowerbed in which some of the plots are plante ...

  5. 561. Array Partition I【easy】

    561. Array Partition I[easy] Given an array of 2n integers, your task is to group these integers int ...

  6. 217. Contains Duplicate【easy】

    217. Contains Duplicate[easy] Given an array of integers, find if the array contains any duplicates. ...

  7. 521. Longest Uncommon Subsequence I【easy】

    521. Longest Uncommon Subsequence I[easy] Given a group of two strings, you need to find the longest ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 【费马小定理+矩阵快速幂】HDU4549——M斐波那契数列

    [题目大意] M斐波那契数列F[n]是一种整数数列,它的定义如下:F[0] = aF[1] = bF[n] = F[n-1] * F[n-2] ( n > 1 )现在给出a, b, n,求出F[ ...

  2. 1.5 JSP标准标签库(JSTL)(核心标签 out、set、remove、if、choose、forEach、forTokens、redirect)

    JSTL(JavaServer Page Standard Tag  Library):JSP标准标签库.它封装了JSP应用的通用核心功能. 1.准备工作 使用JSTL前需要下载所需文件,下载地址及安 ...

  3. iOS 简单易用的二维码扫描及生成二维码三方控件LFQRCode,可灵活自定义UI

    一.扫码 扫描的控件是一个view,使用者只需贴在自己的控制器内即可.其他UI用户可在自己控制器随便添加.代码如下 - (void)viewDidLoad { [super viewDidLoad]; ...

  4. linux-改变文件属主权限-chown

    http://www.cnblogs.com/peida/archive/2012/12/04/2800684.html chown将指定文件的拥有者改为指定的用户或组,用户可以是用户名或者用户ID: ...

  5. 一个简单的WeakList的实现

    有的时候,我们会使用到WeakList,它和传统的List不同的是,保存的是对象的弱应用,在WeakList中存储的对象会被GC回收,在一些和UI相关的编程的地方会用到它(弱事件,窗体的通知订阅等). ...

  6. menuStrip鼠标经过自动显示菜单

    //--------------------------------------------------------------------------------- private void For ...

  7. java中的注解详解和自定义注解

    一.java中的注解详解 1.什么是注解 用一个词就可以描述注解,那就是元数据,即一种描述数据的数据.所以,可以说注解就是源代码的元数据.比如,下面这段代码: @Override public Str ...

  8. 剪切Postscript图片中的多余边框

    最近用plplot画图,其cairo ps库生成的ps图片总是不能合理地剪切掉多余的边框,于是乎自己写了一个小脚本epscrop,用修改ps图的BoundingBox. #!/bin/bash # c ...

  9. 设计工作-Axure

    1,百度百科 http://baike.baidu.com/view/3332366.htm?fromtitle=axure&fromid=5056136&type=syn 2,官方网 ...

  10. PHPCMS V9管理员password忘记怎样改动

    一般的虚拟主机商都提供了PHPmyAdmin,选择你站点数据库.然后选择v9_admin这个表. 编辑 password,变成:fa3250300be9b7ab0848257f3cbb06e7 enc ...