[LeetCode] Max Consecutive Ones 最大连续1的个数
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
这道题让我们求最大连续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的个数的更多相关文章
- 485 Max Consecutive Ones 最大连续1的个数
给定一个二进制数组, 计算其中最大连续1的个数.示例 1:输入: [1,1,0,1,1,1]输出: 3解释: 开头的两位和最后的三位都是连续1,所以最大连续1的个数是 3.注意: 输入的数组只包 ...
- Leetcode485.Max Consecutive Ones最大连续1的个数
给定一个二进制数组, 计算其中最大连续1的个数. 示例 1: 输入: [1,1,0,1,1,1] 输出: 3 解释: 开头的两位和最后的三位都是连续1,所以最大连续1的个数是 3. 注意: 输入的数组 ...
- 485. Max Consecutive Ones最大连续1的个数
网址:https://leetcode.com/problems/max-consecutive-ones/ 很简单的一题 class Solution { public: int findMaxCo ...
- LeetCode——Max Consecutive Ones
LeetCode--Max Consecutive Ones Question Given a binary array, find the maximum number of consecutive ...
- [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 ...
- LeetCode Max Consecutive Ones II
原题链接在这里:https://leetcode.com/problems/max-consecutive-ones-ii/ 题目: Given a binary array, find the ma ...
- 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 ...
- [LeetCode] 829. Consecutive Numbers Sum 连续数字之和
Given a positive integer N, how many ways can we write it as a sum of consecutive positive integers? ...
- LeetCode: Max Consecutive Ones
这题最关键的是处理最开始连续1和最后连续1的方式,想到list一般在最前面加个node的处理方式,在最前面和最后面加0即可以很好地处理了 public class Solution { public ...
随机推荐
- bug单的生命周期
测试工程师发现了软件的缺陷(bug),那修复bug的整个流程是怎么样的呢? 1.发现bug 2.和开发确认是否是bug 3.如果是bug则提bug单到测试经理,如果不是则放过 4.测试经理把bug单走 ...
- 以太坊开发DApp实战教程——用区块链、星际文件系统(IPFS)、Node.js和MongoDB来构建电商平台(一)
第一节 简介 欢迎和我们一起来用以太坊开发构建一个去中心化电商DApp!我们将用区块链.星际文件系统(IPFS).Node.js和MongoDB来构建电商平台类似淘宝的在线电商应用,卖家可以自由地出售 ...
- JDK1.8源码(六)——java.util.LinkedList 类
上一篇博客我们介绍了List集合的一种典型实现 ArrayList,我们知道 ArrayList 是由数组构成的,本篇博客我们介绍 List 集合的另一种典型实现 LinkedList,这是一个有链表 ...
- C语言--总结报告
1.当初你是如何做出选择计算机专业的决定的? 经过一个学期,你的看法改变了么,为什么? 你觉得计算机是你喜欢的领域吗,它是你擅长的领域吗? 为什么? 当初填报志愿我是有很明确的专业方向的,就是IT类的 ...
- C语言--第三周作业
一.PTA作业中4个题目 1.7-9 A乘以B 要求:输入的两个整数:A是你学号前两位数字,B是你学号后两位数字 a.代码 #include <stdio.h> int main () { ...
- C语言--第二周作业
****学习内容总结**** 1.Git和编辑器截图 2.MOOC截图 3.阅读<提问的智慧>感想 读完<提问的智慧>之后,我认为在提问时,要根据以下步骤: 谨慎明确的描述症状 ...
- Flask 学习 九 用户资料
资料信息 app/models.py class User(UserMixin,db.Model): #...... name = db.Column(db.String(64)) location ...
- Spring Boot jar包linux服务器部署
Spring Boot 部署 一.使用命令行java -jar 常驻 nohup java -jar spring-boot-1.0-SNAPSHOT.jar > log.file 2>& ...
- VMware虚拟机误删除vmdk文件后如何恢复?
故障描述: Dell R710系列服务器(用于VMware虚拟主机),Dell MD 3200系列存储(用于存放虚拟机文件),VMware ESXi 5.5版本,因意外断电,导致某台虚拟机不能正常启动 ...
- 码农、黑客和2B程序员之间的区别
码农: 黑客: 2B程序员: 求2的32次方: 码农: System.out.println(Math.pow(2, 32)); 黑客: System.out.println(1L<<32 ...