【LeetCode】485. Max Consecutive Ones 解题报告(Java & Python)
作者: 负雪明烛
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:
- 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。
解题方法
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)的更多相关文章
- LeetCode 485 Max Consecutive Ones 解题报告
题目要求 Given a binary array, find the maximum number of consecutive 1s in this array. 题目分析及思路 给定一个01数组 ...
- 【LeetCode】383. Ransom Note 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...
- 【LeetCode】575. Distribute Candies 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- 【LeetCode】136. Single Number 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 异或 字典 日期 [LeetCode] 题目地址:h ...
- 【LeetCode】283. Move Zeroes 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:首尾指针 方法二:头部双指针+双循环 方法三 ...
- 【LeetCode】659. Split Array into Consecutive Subsequences 解题报告(Python)
[LeetCode]659. Split Array into Consecutive Subsequences 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...
- 【LeetCode】376. Wiggle Subsequence 解题报告(Python)
[LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...
- 【LeetCode】481. Magical String 解题报告(Python)
[LeetCode]481. Magical String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:/ ...
- 【LeetCode】554. Brick Wall 解题报告(Python)
[LeetCode]554. Brick Wall 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...
随机推荐
- Linux服务器I/O性能分析-2
一.如何正确分析IO性能 1.1 BLKTRACE分析IO性能 之前的文章已经说明,要是系统发生I/O性能问题,我们常用的命令是无法精确定位问题(内核I/O调度器消耗的时间和硬件消耗的时间,这个不能作 ...
- EXCEL-如何在excel中对图片进行批量排版
新建EXCEL->导入图片->如果每张图高度为33个单元格,共计10张图,那么将最后边的那张图(即正对着你的那一张)剪切粘贴到33*9行第一个单元格处->按F5定位"对象& ...
- Python异步IO之select
1. select模块的基本使用(以socket为例) 1 # -*- coding:utf-8 -*- 2 # Author:Wong Du 3 4 import select 5 import s ...
- C语言中宏定义#define 、 typedef 和枚举类型
替换时机 #define :预编译阶段简单替换,编译阶段展开源程序(1.词法扩展==程序生成期间的字符串替换 2.语义扩展==生成特定指令) 枚举常量:编译阶段确定其值 内联函数:编译阶段插入代码 t ...
- absurd, abundant
absurd How: absolutely, completely, clearly, faintly, manifestly, obviously, patently, quite, rather ...
- 【swift】复制后,为Xcode工程项目重新修改名称
感谢,参考了另一篇博客:https://www.jianshu.com/p/abf10c9609ef 我做了一些修改,和自己遇到的情况 我用的是繁体的mac,所以下面图片内,鼠标右键点出来的文字(丢到 ...
- 银联acp手机支付总结
总结: 1.手机调用后台服务端接口,获取银联返回的流水号tn 银联支付是请求后台,后台向银联下单,返回交易流水号,然后返回给用户,用户通过这个交易流水号,向银联发送请求,获取订单信息,然后再填写银行卡 ...
- [学习总结]3、Android---Scroller类(左右滑动效果常用的类)
参考资料:http://blog.csdn.net/vipzjyno1/article/details/24592591 非常感谢这个兄弟! 在android学习中,动作交互是软件中重要的一部分,其中 ...
- 【Linux】【Services】【Docker】Docker File
Docker Images: docker commit Dockerfile:文本文件,镜像文件构建脚本: Dockerfile:由一系列用于根据基础镜像构建新的镜像文件的专用指令序列组成: 指令: ...
- Java(变量和常量)
变量 可以变化的量.可以通过变量来操控内存中的数据:变量可以指代的是内存中的一块空间,而这块空间的位置是确定的但里边要放什么东西还不确定. Java是强类型语言,每个变量都要声明其类型. Java变量 ...