LeetCode: 485 Max Consecutive Ones(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
and1
. - The length of input array is a positive integer and will not exceed 10,000
代码:
别人的:
class Solution {
public:
int findMaxConsecutiveOnes(vector<int>& nums) {
int count = , max = ;
for (int i = ; i < nums.size(); i++) {
if (nums[i] == && (!i || nums[i - ] != )) count = ;
else if (i && nums[i] == && nums[i - ] == ) count++;
if (max < count) max = count;
}
if (max < count) max = count;
return max;
}
};
自己的:
class Solution {
public:
int findMaxConsecutiveOnes(vector<int>& nums) {
int result = ;
int tem = ;
nums.push_back();
for (auto c : nums){
if(c == ){
if ( tem > result)
result = tem;
tem = ;
}
else
tem++;
}
return result;
}
};
LeetCode: 485 Max Consecutive Ones(easy)的更多相关文章
- LeetCode 485. Max Consecutive Ones (最长连续1)
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
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数组 ...
- [LeetCode]485. Max Consecutive Ones 找到最大的连续的1的个数
题目描述 输入只有0和1的数组(长度为正整数,且<10000),找到最大的连续1的个数 比如[1,1,0,1,1,1],输出3 思路 遍历数组,统计当前连续个数curCount和最大连续值max ...
- 485. Max Consecutive Ones【easy】
485. Max Consecutive Ones[easy] Given a binary array, find the maximum number of consecutive 1s in t ...
- 【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 ...
- 【LeetCode】485. Max Consecutive Ones 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...
随机推荐
- C#注冊表操作汇总
一.注冊表基本知识 1) 结构 键->项->子项->值项(名称.类型.数据) REG_SZ 字符串 REG_BINARY 二进制 REG_DWORD ...
- db2 命令
很久没有些博客了.把以前用到的操作 DB2 的命令发表下可能有很多人已经发布了.就当是自己做下功课吧,以备有用之需. 1. 打开命令行窗口 #db2cmd 2. 打开控制中心 # db2cmd db2 ...
- java socket相关的timeout
1 java socket的两个timeout 一个是connect timeout,即建立连接的timeout,另外一个是so timeout,是读取数据的timeout.这两个timeout都是因 ...
- SAP建数据库索引
[转]SAP建数据库索引 %_hints db6 'INDEX("MKPF","MKPF~BUD")' db6 'INDEX(&quo ...
- 【R】R语言生成随机数
1.概述 作为一种语言进行统计分析,R有一个随机数生成各种统计分布功能的综合性图书馆.R语言可以针对不同的分布,生成该分布下的随机数.其中,有许多常用的个分布可以直接调用.本文简单介绍生成常用分布随机 ...
- AndroidContactsTest.java
以下代码使用ContactManager.apk进行测试 package com.saucelabs.appium; import io.appium.java_client.AppiumDriver ...
- ZOJ - 3935 2016 【数的筛选】
题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3935 题意 要求找出 从 2016-990528 中 是闰年 并 ...
- chrome最小字体12px
http://www.mamicode.com/info-detail-512021.html http://www.divcss5.com/wenji/w738.shtml
- Ansible 动态获取主机列表
参考文献: http://www.linuxidc.com/Linux/2016-12/138111.htm 附加 这个 include_vars 变量,可以 动态分别环境或者其他条件- hosts: ...
- 使用grunt js进行js的链接和压缩
1,http://nodejs.org/download/ 安装nodejs 2,配置环境变量,将nodejs的安装目录放置在Path环境变量中 3,在cmd中 npm install -g grun ...