[LeetCode]485. Max Consecutive Ones 找到最大的连续的1的个数
题目描述
输入只有0和1的数组(长度为正整数,且<10000),找到最大的连续1的个数
比如[1,1,0,1,1,1],输出3
思路
遍历数组,统计当前连续个数curCount和最大连续值maxCount。If当前数组值为1,则curCount++;否则(值为0)比较curCount和maxCount,看是否需要替换,并把curCount置0
最后返回maxCount
错误:对于[1]这种边界条件没有考虑完全。这种时候上面的逻辑会输出0,而应该是1。也就是结束的时候需要增加一个比较,万一如果最后的连续1的个数比较多。
——解决:在遍历完,返回之前,增加一个[比较curCount和maxCount,看是否需要替换]。
代码
public class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
int curCount=0,maxCount=0;
for(int n: nums){
if(n==1)
curCount++;
else{
if(curCount>maxCount)
maxCount=curCount;
curCount=0;
}
}
if(curCount>maxCount)
maxCount=curCount;
return maxCount;
}
}
[LeetCode]485. Max Consecutive Ones 找到最大的连续的1的个数的更多相关文章
- LeetCode 485. Max Consecutive Ones (最长连续1)
Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1, ...
- 485. Max Consecutive Ones最长的连续1的个数
[抄题]: Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Inpu ...
- LeetCode 485 Max Consecutive Ones 解题报告
题目要求 Given a binary array, find the maximum number of consecutive 1s in this array. 题目分析及思路 给定一个01数组 ...
- 8. leetcode 485. Max Consecutive Ones
Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1, ...
- (双指针) leetcode 485. Max Consecutive Ones
Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1, ...
- LeetCode: 485 Max Consecutive Ones(easy)
题目: Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: ...
- 【leetcode】485. Max Consecutive Ones
problem 485. Max Consecutive Ones solution1: class Solution { public: int findMaxConsecutiveOnes(vec ...
- 485. Max Consecutive Ones - LeetCode
Question 485. Max Consecutive Ones Solution 题目大意:给一个数组,取连续1的最大长度 思路:遍历数组,连续1就加1,取最大 Java实现: public i ...
- 485. Max Consecutive Ones【easy】
485. Max Consecutive Ones[easy] Given a binary array, find the maximum number of consecutive 1s in t ...
随机推荐
- Requests的安装和使用
一.Requests的安装1.pip3 install requests2.验证 import requests 不报错即可
- 疯了吧!这帮人居然用 Go 写“前端”?(二)
作者 | 郑嘉涛(羣青) 来源|尔达 Erda 公众号 前言 上篇我们讲了故事发生的背景,也简单阐述了组件及协议的设想: 一.丰富的通用组件库. 二.组件渲染能力,将业务组件渲染成通用组件 ...
- accent, access, accident
accent A colon (:) is used to represent a long vowel [元音], e.g. sheet /ʃiːt/ and shit /ʃit/. The wor ...
- Ubuntu下STL源码文件路径+VS2010下查看STL源码
Ubuntu版本信息 然后STL源码位置就在 /usr/include/c++/7/bits /usr/include/c++/7.4.9/bits 这两个文件下都有 然后我日常写程序用的Window ...
- tomcat启动和停止脚本
#!/bin/bash JDK_HOME=/apps/jdk1.7.0_79 CATALINA_HOME=/apps/tomcat export JDK_HOME CATALINA_HOME sour ...
- GO 数字运算
大整数运算 // bigint project main.go package main import ( "fmt" "math" "math/bi ...
- OC-代理,字符串
总结 编号 标题 内容 一 protocol protocol 基本概念/语法格式/protocol和继承区别/使用注意/基协议/@required和@optional关键字/类型限制 二 代理设计模 ...
- 【Linux】【Services】【SaaS】Docker+kubernetes(2. 配置NTP服务chrony)
1. 简介 1.1. 这次使用另外一个轻量级的NTP服务,chrony.这是openstack推荐使用的ntp服务. 1.2. 官方网站:https://chrony.tuxfamily.org/ 2 ...
- 【Linux】【Shell】【Basic】条件测试和变量
bash脚本编程 脚本文件格式: 第一行,顶格:#!/bin/bash 注释信息:# 代码注释: 缩进,适度添加空白行: ...
- 数据库系统相关SQL
杀进程 查出所有被锁住的表 select b.owner TABLEOWNER, b.object_name TABLENAME, c.OSUSER LOCKBY, c.USERNAME LOGINI ...