作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


[LeetCode]

题目地址:https://leetcode.com/problems/max-consecutive-ones/

Difficulty: Easy

题目描述

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:

  1. The input array will only contain 0 and 1.
  2. The length of input array is a positive integer and will not exceed 10,000

题目大意

找出数组中最多有多少个连续的1。

解题方法

Java解法

找出一个二进制串中最多连续的1的个数。

第一想法就是动态规划。找到目前的连续的串和过去连续的串的最大值,设为曾经最大的串。

故,设置count代表现在连续的串,anwer为过去连续的值得最大值。转移方程就是求两者的最大值。

有个技巧,就是判断count和answer最大值得时候,注意到count要包括末尾的连续的1的个数,而不仅仅是遇到0停止。所以设置的是循环变量到了nums.length,超出了数组的clength,这样的时候就使停止的判断有两个,遇到0和超出数组长度。

好像有人比我的代码更简洁,但是思路大致一样的。

public class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
int answer=0;
int count =0;
for(int i=0; i<= nums.length; i++){
if(i != nums.length && nums[i] == 1){
count++ ;
}else{
if(count == 0){
continue;
}
answer=answer > count ? answer : count;
count = 0;
}
}
return answer;
}
}

AC: 8 ms

Python解法

二刷的时候使用的是Python。写起来比较简单了,如果是0的话,更新起始位置,如果是1就更新最大值。

class Solution:
def findMaxConsecutiveOnes(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
index = -1
N = len(nums)
res = 0
for i, n in enumerate(nums):
if n == 0:
index = i
else:
res = max(res, i - index)
return res

日期

2017 年 1 月 15 日
2018 年 11 月 9 日 —— 睡眠可以

【LeetCode】485. Max Consecutive Ones 解题报告(Java & Python)的更多相关文章

  1. LeetCode 485 Max Consecutive Ones 解题报告

    题目要求 Given a binary array, find the maximum number of consecutive 1s in this array. 题目分析及思路 给定一个01数组 ...

  2. 【LeetCode】383. Ransom Note 解题报告(Java & Python)

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

  3. 【LeetCode】575. Distribute Candies 解题报告(Java & Python)

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

  4. 【LeetCode】136. Single Number 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 异或 字典 日期 [LeetCode] 题目地址:h ...

  5. 【LeetCode】283. Move Zeroes 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:首尾指针 方法二:头部双指针+双循环 方法三 ...

  6. 【LeetCode】659. Split Array into Consecutive Subsequences 解题报告(Python)

    [LeetCode]659. Split Array into Consecutive Subsequences 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...

  7. 【LeetCode】376. Wiggle Subsequence 解题报告(Python)

    [LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...

  8. 【LeetCode】481. Magical String 解题报告(Python)

    [LeetCode]481. Magical String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:/ ...

  9. 【LeetCode】554. Brick Wall 解题报告(Python)

    [LeetCode]554. Brick Wall 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...

随机推荐

  1. 【机器学习与R语言】4-决策树

    目录 1.决策树原理 2.决策树应用示例 2.1)收集数据 2.2)探索和准备数据 2.3)训练模型 2.4)评估模型性能 2.5)提高模型性能 通过自适应增强算法(boosting) 将惩罚因子分配 ...

  2. HTML三层界面显示

    1.效果示意图 2.主要标签属性 3.实现代码 1.效果示意图 要实现类似如下效果:点击"大模态框",中间出现一层遮盖整个页面的半透明页面,最上面出现"Large mod ...

  3. mongodb数据库简单类

    <?php/*** Mongodb类** examples: * $mongo = new HMongodb("127.0.0.1:11223"); * $mongo-> ...

  4. MySQL插入大量数据探讨

    笔者想进行数据库查询优化探索,但是前提是需要一个很大的表,因此得先导入大量数据至一张表中. 准备工作 准备一张表,id为主键且自增: 方案一 首先我想到的方案就是通过for循环插入 xml文件: &l ...

  5. 云原生PaaS平台通过插件整合SkyWalking,实现APM即插即用

    一. 简介 SkyWalking 是一个开源可观察性平台,用于收集.分析.聚合和可视化来自服务和云原生基础设施的数据.支持分布式追踪.性能指标分析.应用和服务依赖分析等:它是一种现代 APM,专为云原 ...

  6. == 和 equals() 方法的区别

    == 在比较基本数据类型时,是比较两边的数据的值是否相等 // 整数类型 int num1 = 1; // 双精度浮点数类型 double num2 = 1.0; // 输出结果为 true Syst ...

  7. android知识点duplicateParentState

    android知识点duplicateParentState 今天要做一个效果,组件RelativeLayout上有两个TextView,这两个TextView具有不同的颜色值,现在要的效果是,当Re ...

  8. GO 定时器NewTimer、NewTicker使用

    package main import ( "fmt" "sync" "time" ) /** *ticker只要定义完成,从此刻开始计时, ...

  9. Docker 生产环境之配置容器 - 自动启动容器

    原文地址 Docker 提供了重启策略,以控制容器在退出时是否自动启动,或在 Docker 重新启动时自动启动.重启策略可确保链接的容器以正确的顺序启动.Docker 建议使用重启策略,并避免使用流程 ...

  10. ES6 object.defineProperty

    Object.defineProperty() 方法会直接在一个对象上定义一个新属性,或者修改一个对象的现有属性, 并返回这个对象. Object.defineProperty(obj, prop, ...