leetcode解题报告(14):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
分析
太简单了,一分钟ac
代码如下:
class Solution {
public:
int findMaxConsecutiveOnes(vector<int>& nums) {
if(nums.size() == 0)return 0;
int max = 0;
int length = 0;
for(int i = 0; i != nums.size(); ++i){
if(nums[i] == 1){
++length;
max = length > max ? length : max;
}else{
length = 0;
}
}
return max;
}
};
leetcode解题报告(14):Max Consecutive Ones的更多相关文章
- LeetCode解题报告:Linked List Cycle && Linked List Cycle II
LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...
- leetcode解题报告(2):Remove Duplicates from Sorted ArrayII
描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- LeetCode 解题报告索引
最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
- leetcode解题报告(5):Longest Consecutive Sequence
描述 Given an unsorted array of integers, find the length of the longest consecutive elements sequence ...
- LeetCode解题报告—— Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- leetCode解题报告5道题(六)
题目一: Longest Substring Without Repeating Characters Given a string, find the length of the longest s ...
- LeetCode解题报告—— Minimum Window Substring && Largest Rectangle in Histogram
1. Minimum Window Substring Given a string S and a string T, find the minimum window in S which will ...
- LeetCode解题报告—— Maximal Rectangle
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and ...
- LeetCode解题报告—— Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
随机推荐
- access函数的使用
#include<unistd.h> int access(const char* pathname, int mode); 参数介绍: pathname 是文件的路径名+文件名 mode ...
- prometheus环境搭建
1. 下载文件 wget https://dl.grafana.com/oss/release/grafana-6.2.4.linux-amd64.tar.gz tar -zxvf grafana-. ...
- 怎样重启ssh服务
尝试下面两个命令: service sshd restart systemctl restart sshd.service
- C#中using的用途
using System; --主命名空间,包含所有.net基础类型和通用类型,比如Object, ...
- VBA嵌套if语句
一个If或ElseIf语句可以嵌套在另一个If或ElseIf语句中.内部的If语句是根据最外层的If语句执行的.这使得VBScript能够轻松处理复杂的条件. 语法 以下是VBScript中嵌套的If ...
- ol li 兼容
ol 标签在 chrome 60 和 safari12 缩进不一样. 因为序号距离copy距离不一样,导致显示不一样.解决办法. list-style-position: inside;text-in ...
- Tomcat安装及环境配置
欢迎任何形式的转载,但请务必注明出处. 本章内容 安装 环境变量入口 三个系统变量配置 测试安装配置是否成功 安装之前请安装jdk并进行环境配置(点击进入jdk教程) 一.安装 点击进入官网下载 二. ...
- iOS中JSON解析三方库的比较
网络数据解析框架 1. JsonModel 一个 JSON 模型转换库,有着比较简洁的接口.Model 需要继承自 JSONModel. 2. yyModel yyModel比较轻量(算上.h 只 ...
- Android NDK 学习之接受Java传入的Int数组
本博客主要是在Ubuntu 下开发,且默认你已经安装了Eclipse,Android SDK, Android NDK, CDT插件. 在Eclipse中添加配置NDK,路径如下Eclipse-> ...
- Window Server 2008 概述
Windows历史版本 Windows工作站发展历史 Windows95——>Windows98——>Windows2000——>WindowsXP——>Vista——> ...