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 and 1.
  • 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】的更多相关文章

  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. java源码阅读Object

    1 类注释 Class {@code Object} is the root of the class hierarchy. Every class has {@code Object} as a s ...

  2. 1.5(java学习笔记)this关键字

    this关键字主要有三个作用 1.调用本类中的属性. public class TextThis { public static void main(String[] args){ Person p1 ...

  3. 十. 图形界面(GUI)设计13.鼠标事件

    鼠标事件的事件源往往与容器相关,当鼠标进入容器.离开容器,或者在容器中单击鼠标.拖动鼠标时都会发生鼠标事件.java语言为处理鼠标事件提供两个接口:MouseListener,MouseMotionL ...

  4. jQuery判断一个元素是否为另一个元素的子元素

    判断:当前元素是否是被筛选元素的子元素 jQuery.fn.isChildOf = function(b){ return (this.parents(b).length > 0); }; 判断 ...

  5. 使用FluentValidation来进行数据有效性验证

    之前我介绍过了使用系统自带的Data Annotations来进行数据有效性验证,今天在CodePlex上逛的时候,发现了一个非常简洁好用的库:FluentValidation 由于非常简洁,就直接拿 ...

  6. JAVA EE 学习笔记

    http://www.cnblogs.com/kuangdaoyizhimei/category/701794.html http://www.cnblogs.com/liunanjava/p/445 ...

  7. javascript 定时器 笔记

    最近想看下定时器,发现这东西越看越牵连的东西越多,比如js单线程,EVent loop 什么的 看到了几篇比较好的文章 http://ejohn.org/blog/how-javascript-tim ...

  8. react项目如何调试?

    进入到相应的网页界面,然后查看Sources->Page->top->webpack://->找到react的js代码处,设置断点,进行调试

  9. 《linux 内核全然剖析》 chapter 4 80x86 保护模式极其编程

    80x86 保护模式极其编程       首先我不得不说.看这章真的非常纠结...看了半天.不知道这个东西能干嘛.我感觉唯一有点用的就是对于内存映射的理解...我假设不在底层给80x86写汇编的话.我 ...

  10. iOS:Masonry练习详解

    Masonry练习详解   添加约束的方式: 1.通过使用NSLayoutConstraints添加约束到约束数组中,之前必须设置translatesAutoresizingMaskIntoConst ...