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

这道题让我们求最大连续1的个数,不是一道难题。我们可以遍历一遍数组,用一个计数器cnt来统计1的个数,方法是如果当前数字为0,那么cnt重置为0,如果不是0,cnt自增1,然后每次更新结果res即可,参见代码如下:

解法一:

class Solution {
public:
int findMaxConsecutiveOnes(vector<int>& nums) {
int res = , cnt = ;
for (int num : nums) {
cnt = (num == ) ? : cnt + ;
res = max(res, cnt);
}
return res;
}
};

由于是个二进制数组,所以数组中的数字只能是0或1,那么连续1的和跟个数相等,所以我们可以计算和,通过加上num,再乘以num来计算,如果当前数字是0,那么sum就被重置为0,还是要更新结果res,参见代码如下:

解法二:

class Solution {
public:
int findMaxConsecutiveOnes(vector<int>& nums) {
int res = , sum = ;
for (int num : nums) {
sum = (sum + num) * num;
res = max(res, sum);
}
return res;
}
};

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Max Consecutive Ones 最大连续1的个数的更多相关文章

  1. 485 Max Consecutive Ones 最大连续1的个数

    给定一个二进制数组, 计算其中最大连续1的个数.示例 1:输入: [1,1,0,1,1,1]输出: 3解释: 开头的两位和最后的三位都是连续1,所以最大连续1的个数是 3.注意:    输入的数组只包 ...

  2. Leetcode485.Max Consecutive Ones最大连续1的个数

    给定一个二进制数组, 计算其中最大连续1的个数. 示例 1: 输入: [1,1,0,1,1,1] 输出: 3 解释: 开头的两位和最后的三位都是连续1,所以最大连续1的个数是 3. 注意: 输入的数组 ...

  3. 485. Max Consecutive Ones最大连续1的个数

    网址:https://leetcode.com/problems/max-consecutive-ones/ 很简单的一题 class Solution { public: int findMaxCo ...

  4. LeetCode——Max Consecutive Ones

    LeetCode--Max Consecutive Ones Question Given a binary array, find the maximum number of consecutive ...

  5. [LeetCode] Max Consecutive Ones II 最大连续1的个数之二

    Given a binary array, find the maximum number of consecutive 1s in this array if you can flip at mos ...

  6. LeetCode Max Consecutive Ones II

    原题链接在这里:https://leetcode.com/problems/max-consecutive-ones-ii/ 题目: Given a binary array, find the ma ...

  7. Leetcode: Max Consecutive Ones II(unsolved locked problem)

    Given a binary array, find the maximum number of consecutive 1s in this array if you can flip at mos ...

  8. [LeetCode] 829. Consecutive Numbers Sum 连续数字之和

    Given a positive integer N, how many ways can we write it as a sum of consecutive positive integers? ...

  9. LeetCode: Max Consecutive Ones

    这题最关键的是处理最开始连续1和最后连续1的方式,想到list一般在最前面加个node的处理方式,在最前面和最后面加0即可以很好地处理了 public class Solution { public ...

随机推荐

  1. android studio视频教学

    英语+中文字幕: http://www.apkbus.com/plugin.php?id=buskc&modo=learn&kcid=82 中文字幕: http://www.maizi ...

  2. java数组排序,并将数组内的数据求和

    java数据编列并求和,江湖我狼哥,人狠话不多,直接上代码! import java.util.Arrays; public class Intarry { public static void ma ...

  3. 分区表SQL调优/优化(Tuning)时容易“被欺骗”的场景之一

    近几天没有用户找到,除了看看书,就是上网浏览点东西,好不惬意.可惜好景不长,正在享受悠闲惬意的日子时,一个用户的工作人员QQ找到我,说他们在统计一些数据,但一个SQL特别慢,或者说就从来没出过数据,我 ...

  4. 网络1712--c语言第二次作业总结

    1.作业亮点 1.1在调试问题方面有明显进步,变量声明方面有所改变,没有发现大面积抄袭现象. 1.2 以下几位同学博文写的较为优秀,可作为范例供大家参考 田亚琴--代码格式良好,思路清晰,调试部分图文 ...

  5. beta冲刺1-咸鱼

    前言:这篇算是开始补之前的开端,毕竟beta阶段我们从前面开始就有在陆续做了. 今天的工作: 接收了新成员*1,然后几个人聚了一下,并且讨论了一下目前遇到的问题,以及目前需要处理的问题. 目前遇到的问 ...

  6. 微信小程序轮播图

    swiper标签 <!--index.wxml--> <swiper class="swiper" indicator-dots="true" ...

  7. nyoj 仿射密码

    仿射密码 时间限制:1000 ms | 内存限制:65535 KB 难度:1 描述 仿射密码是替换密码的另一个特例,可以看做是移位密码和乘数密码的结合.其加密变换如下: E(m)=(k1*m+k2) ...

  8. C#中的函数式编程:递归与纯函数(二)

    在序言中,我们提到函数式编程的两大特征:无副作用.函数是第一公民.现在,我们先来深入第一个特征:无副作用. 无副作用是通过引用透明(Referential transparency)来定义的.如果一个 ...

  9. Spring知识点回顾(05)bean的初始化和销毁

    Java配置方式:@Bean @InitMethod @destroyMethod xml配置方式:init-method,destroy-method 注解方式:@PostConstruct,@Pr ...

  10. 新概念英语(1-11)Is this your shirt ?

    Is this your shirt?Whose shirt is white? A:Whose shirt is that? Is this your shirt, Dave? Dave:No si ...