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. ujmp使用心得

    1)对矩阵转置运算时: Matrix test2 = oneMatrix.transpose(); Matrix test2 = oneMatrix.transpose(Ret.LINK); Matr ...

  2. Mongodb 学习笔记简介

    目录 1       准备工作... 5 1.1        相关网址... 6 1.1        下载安装... 6 1.1.1         下载:... 6 1.1.2         ...

  3. linux如何启动/停止/重启MySQL

    如何启动/停止/重启MySQL 一.启动方式 1.使用 service 启动:service mysqld start2.使用 mysqld 脚本启动:/etc/inint.d/mysqld star ...

  4. LVM分区管理和扩展

    一.LVM简介 LVM是 Logical Volume Manager(逻辑卷管理)的简写.LVM将一个或多个硬盘的分区在逻辑上集合,相当于一个大硬盘来使用,当硬盘的空间不够使用的时候,可以继续将其它 ...

  5. cs-HtmlHelpers

    ylbtech-Unitity: cs-HtmlHelpers PagingHelpers.cs  SelectInputHelpers.cs TreeHelpers.cs 1.A,效果图返回顶部   ...

  6. javascriptMVC框架面向对象编程

    //抽象形状类 $.Class("Shape", {}, { //构造函数 init : function(edge) { this.edge = edge; }, //实例方法 ...

  7. js同比例缩放图片

    function DrawImage(ImgD, FitWidth, FitHeight) { var image = new Image(); image.src = ImgD.src; if (i ...

  8. 【Cocos2d-x 3.0 基础系列一】 各类回调函数写法汇总

    一.button回调 1. Lambda 表达式,C++11 Lambda 赋予了Cocos2d-x 3.0创建回调函数的灵活性. auto itemNor = Sprite::create(&quo ...

  9. POJ 2386 Lake Counting 搜索题解

    简单的深度搜索就能够了,看见有人说什么使用并查集,那简直是大算法小用了. 由于能够深搜而不用回溯.故此效率就是O(N*M)了. 技巧就是添加一个标志P,每次搜索到池塘,即有W字母,那么就觉得搜索到一个 ...

  10. 【招聘App】—— React/Nodejs/MongoDB全栈项目:登录注册

    前言:最近在学习Redux+react+Router+Nodejs全栈开发高级课程,这里对实践过程作个记录,方便自己和大家翻阅.最终成果github地址:https://github.com/66We ...