485. Max Consecutive Ones
题目
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'的个数
##解答
###解法1:(我)每次'0'时取max,但返回需要再取一次max(12ms)
```
public class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
int count = 0;
int count1 = 0;
for (int i = 0; i
###解法2:每次'1'时取max,返回无需再取;遍历数组由for改为for each(9ms√)
```
public class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
int count = 0;
int result = 0;
for (int num : nums){
if(num == 1){
count++;
result = Math.max(count,result);
}
else{
count = 0;
}
}
return result;
}
}
```
485. Max Consecutive Ones的更多相关文章
- 【leetcode】485. Max Consecutive Ones
problem 485. Max Consecutive Ones solution1: class Solution { public: int findMaxConsecutiveOnes(vec ...
- 485. Max Consecutive Ones【easy】
485. Max Consecutive Ones[easy] Given a binary array, find the maximum number of consecutive 1s in t ...
- 485. Max Consecutive Ones - LeetCode
Question 485. Max Consecutive Ones Solution 题目大意:给一个数组,取连续1的最大长度 思路:遍历数组,连续1就加1,取最大 Java实现: public i ...
- 485. Max Consecutive Ones最长的连续1的个数
[抄题]: Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Inpu ...
- 485. Max Consecutive Ones@python
Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1, ...
- 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 (最长连续1)
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 解题报告
题目要求 Given a binary array, find the maximum number of consecutive 1s in this array. 题目分析及思路 给定一个01数组 ...
随机推荐
- WCF服务发布到IIS中去 在WCF调试
第一个WCF程序 1. 新建立空白解决方案,并在解决方案中新建项目,项目类型为:WCF服务应用程序.建立完成后如下图所示: 2.删除系统生成的两个文件IService1.cs与Service1.svc ...
- 30万奖金!还带你奔赴加拿大相约KDD!?阿里聚安全算法挑战赛带你飞起!
KDD(Knowledge Discovery and Data Mining,知识发现与数据挖掘)会议,作为数据挖掘届的顶会,一直是算法爱好者心中的圣地麦加. 想去?有点难. 给你奖金和差旅赞助 ...
- 浅谈js代码规范
要放假了 后天就可以 回家,心里很高兴,忙里偷闲写篇博客吧!!!! 声明:这是我自己总结的,如果有不对的地方请大家不要较真 一 .变量声明 对所有的变量声明,我们都应该指定var,如果没有指定var ...
- react-router 学习笔记
前言: 本文为个人学习react-router的总结.包括路由基础配置,跳转,权限管理,组件与路由配置的关系,代码分割.欢迎交流指导. 一.路由基础 1.路由配置 & 显示路由组件的view( ...
- Redis key 相关命令
其实本质上,Redis 就是一个Key---Value 数据库.这里我先介绍下Redis中关于的key的相关命令, 注意:key是字符串存储,但是不能使用 空格 或者 “\n”,value 则可以使用 ...
- ubuntu 更新引导命令
sudo update-grub 运行结果: Generating grub configuration file ...Warning: Setting GRUB_TIMEOUT to a non- ...
- 理解redis高可用方案
*:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...
- bootstrap 预定义样式风格
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- HDU 4256 The Famous Clock
The Famous Clock Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- Python爬虫爬取qq视频等动态网页全代码
环境:py3.4.4 32位 需要插件:selenium BeautifulSoup xlwt # coding = utf-8 from selenium import webdriverfrom ...