485. Max Consecutive Ones

题目概述

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

Example 1:

nput: [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
Subscribe to see which companies asked this question.

算法思想

  1. 记录数组中连续1的值
  2. 记录数组中最大的连续1的值
  3. 遍历数组,遇1则加,遇0则置为0
  4. 返回最大连续1的值

代码实现

class Solution {
public:int findMaxConsecutiveOnes(vector<int>& nums) {int sum=0;//记录连续的1值int maxsum=0;//记录最大的连续1值for(int i=0; i<nums.size(); i++){if(nums[i]==1)  sum+=1;//遇1则加if(sum>maxsum)  maxsum=sum;//判断是否大于当前最大连续值if(nums[i]==0)  sum=0;//遇0则置为0}return maxsum;}
};

LeetCode--485相关推荐

  1. C++描述 LeetCode 485. 最大连续1的个数

    C++描述 LeetCode 485. 最大连续1的个数   大家好,我叫亓官劼(qí guān jié ),在CSDN中记录学习的点滴历程,时光荏苒,未来可期,加油~博主目前仅在CSDN中写博客,唯 ...

  2. leetcode 485,487,1004. Max Consecutive Ones I ,II, III(最大连续1的个数问题合集)

    485. Max Consecutive Ones https://leetcode.com/problems/max-consecutive-ones/ easy 题,思路不说了,直接上代码. cl ...

  3. leetcode 485. 最大连续 1 的个数(Java版)

    题目 https://leetcode-cn.com/problems/max-consecutive-ones/ 题解 本题比较简单,两分钟搞定.思路是:维护一个全局最大和局部最大,并保持全局最大及 ...

  4. leetcode 485. 最大连续1的个数

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

  5. LeetCode 485. Max Consecutive Ones

    题目描述: Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Inpu ...

  6. LeetCode题解目录

    最新更新于2020.11.27 前往LeetCode主页. 前往GitHub源码.(服务器原因,暂停同步.) 前往码云主页. 已解决 456/1878 - 简单353 中等 90 困难 13 2020 ...

  7. 《算法基础》线性枚举(一)——最值算法

    目录 1.LeetCode--485. 最大连续 1 的个数 2.LeetCode--1464. 数组中两元素的最大乘积 3.LeetCode--153. 寻找旋转排序数组中的最小值 4.LeetCd ...

  8. 485. Max Consecutive Ones - LeetCode

    Question 485. Max Consecutive Ones Solution 题目大意:给一个数组,取连续1的最大长度 思路:遍历数组,连续1就加1,取最大 Java实现: public i ...

  9. 从零开始刷Leetcode——数组(448.485.509)

    文章目录 448. 找到所有数组中消失的数字 485.最大连续1的个数 509. 斐波那契数 448. 找到所有数组中消失的数字 给定一个范围在 1 ≤ a[i] ≤ n ( n = 数组大小 ) 的 ...

  10. leetcode(1)485——最大连续 1 的个数(C++)

    文章目录 前言 程序 疑惑 vector& nums int v : nums 前言 程序, 边刷边总结边学语言. 程序 class Solution {public:int findMaxC ...

最新文章

  1. window.open的用法---javascript学习笔记
  2. 程序员的抱枕也太高大上了吧! | 每日趣闻
  3. ACL-文件访问控制列表
  4. 关于libnmap 的一些应用
  5. Matlab-基于短时神经网络的声音分类
  6. Python zipfile 文件名称编码 file_name.encode(‘cp437‘).decode(‘gbk‘)
  7. [Bug]Object reference not set to an instance of an object.
  8. jquery +做CheckBoxList全选,反选
  9. Android快速开发框架XUtils
  10. mybatis配置sql超时时间
  11. Http头部和状态码
  12. 智慧交通:数智化地铁大屏管控运维平台
  13. 海洋CMS采集翻译发布插件
  14. 从西方的七宗罪入手,提升自己
  15. 计算机基础播放演示文稿教案,信息技术 - powerpoint教案(20课时)
  16. 跨站漏洞解析-小韩网站编程安全系列一
  17. 在uniapp中使用element-ui组件
  18. python打印九九乘法口诀_使用Python打印九九乘法口诀表
  19. 黑马JAVA P163 字节缓冲流的性能分析
  20. 前端实现炫酷动效_前端动效实现--lottie - web

热门文章

  1. Entity Framework 博客园专题
  2. errors'MessageBoxA' : function does not take 1 parameter
  3. centos的ssh配置
  4. Windows 文件过滤驱动经验总结
  5. Linux学习(三)--vi和vim编辑器
  6. (转)linux下cp目录时排除一个或者多个目录的实现方法
  7. CRF算法中辅助概念 团 极大团 最大团
  8. log4j:WARN No such property [datePattern] in org.apache.log4j.RollingFileAppender.
  9. C语言怎么实现单词下落,如何用c语言实现单词统计
  10. 《智源社区周刊:预训练模型》第1期:吴恩达团队医疗影像预训练、快手落地万亿参数模型...