公众号:爱写bug

给定一个二进制数组, 计算其中最大连续1的个数。

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

示例 1:

输入: [1,1,0,1,1,1]
输出: 3
解释: 开头的两位和最后的三位都是连续1,所以最大连续1的个数是 3.

注意:

  • 输入的数组只包含 01
  • 输入数组的长度是正整数,且不超过 10,000。

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的个数,遇1就累加1,遇0就倒置为0。具体见 Java 注释。

Java:

class Solution{
public int findMaxConsecutiveOnes(int[] nums) {
int temp=0,count=0;//temp记录当前连续1的个数,count记录当前最大连续1的个数
for (int i=0;i<nums.length;i++){//指针右移
if(nums[i]==1){
temp++;//遇1累加1
}else{
if(count<temp){
count=temp;//记录目前最大连续1的个数
}
temp=0;//遇0倒置为0
}
}
return (count>temp)? count:temp;//返回count、temp中较大的数
}
}

注意:

​ 返回值必须是counttemp 中较大的一个。明明已经比较了counttemp,并把较大的赋值给count ,很明显是count 更大,为什么还要比较?

​ 这是因为还有一种输入数组全为1的情况,此时temp一直累加,从未遇到0,所以count自始至终都不可能得到temp的值。

python3:

class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
count=temp=0
for num in nums:
if num==1:
temp+=1
else:
if(count<temp):
count=temp
temp=0
return count if count>temp else temp

LeetCode 485:连续最大1的个数 Max Consecutive Ones(python java)的更多相关文章

  1. 485. 找出二进制串中连续的1的个数 Max Consecutive Ones

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

  2. 485. Max Consecutive Ones@python

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

  3. [Swift]LeetCode485. 最大连续1的个数 | Max Consecutive Ones

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

  4. LeetCode算法题-Max Consecutive Ones(Java实现)

    这是悦乐书的第242次更新,第255篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第109题(顺位题号是485).给定二进制数组,找到此数组中连续1的最大数量.例如: 输 ...

  5. leetcode解题报告(14):Max Consecutive Ones

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

  6. 【LeetCode】633. Sum of Square Numbers 解题报告(python & Java & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 列表生成式 循环 日期 题目地址:https ...

  7. 【LeetCode】232. Implement Queue using Stacks 解题报告(Python & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Python解法 Java解法 日期 [LeetCo ...

  8. [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 ...

  9. Leetcode 485. 最大连续1的个数

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

随机推荐

  1. gitea/gogs在push操作时报RPC failed的问题

    原文发布于:https://www.chenxublog.com/2019/05/26/gitea-gogs-push-rpc-failed.html 最近川普在搞出口管制,GitHub也更新了相应的 ...

  2. C 预处理器、头文件、文件读写

    预处理器 参考链接:https://www.runoob.com/cprogramming/c-preprocessors.html 预处理器是再变异之前做的一些工作 include 包含一个源文件代 ...

  3. deepin把vscode设为默认文本应用

    一开始我想自己写一个desktop文件放在/usr/share/applications下面,结果在右键菜单里面找不到vscode. [Desktop Entry] Categories=Develo ...

  4. 电信NBIOT 4 - NB73模块上行测试(NB73-电信平台-自己平台)

    电信NBIOT 1 - 数据上行(中国电信开发者平台对接流程) 电信NBIOT 2 - 数据上行(中间件获取电信消息通知) 电信NBIOT 3 - 数据下行 电信NBIOT 4 - NB73模块上行测 ...

  5. redo log 重做日志

    --------------------------------------------------2015-02-10---------------------------------------- ...

  6. sqlmap 注入的方法及技巧

    sqlmap 注入的方法及技巧 当给 sqlmap 这么一个 url 的时候,它会: 1.判断可注入的参数 2.判断可以用那种 SQL 注入技术来注入 3.识别出哪种数据库 4.根据用户选择,读取哪些 ...

  7. Python环境安装与基础语法(2)——数据类型、标识符、语言分类

    高级语言发展 结构化语言:以顺序(步骤化),分支,循环描述问题 面向对象语言:接近人类的认知,万物抽象成对象,对象间的关系抽象成类和继承 程序=数据结果+算法 Python解释器 CPython:由C ...

  8. paxos算法—今生

    Paxos 定义2.1  票:即弱化形式的锁.它具备下面几个性质: 可重新发布:服务器可以重新发布新票,即使前面发布的票没有释放. 票可以过期:客户端用一张票来给服务器发送命令请求时,只有当这张票是最 ...

  9. 关于UE4音效的一些小问题

    前言 前几天实在忍受不了StarterContent默认音效的折磨,去网上翻罗了一下初中时很着迷的游戏<Bounce-Tales>的音效,在导入音效时出了点问题,特此说明一下解决方案,希望 ...

  10. 05showLoading配置和 <text>标签的坑 如何发送请求 分享功能和懒加载

    14-电影-列表-需求分析 小程序里面取数据 没有冒号这么一说 加载动画 在对应页面 js文件中 showLoading你可以去看他的配置     // wx.showLoading() 应用在让用户 ...