题目:

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

代码:

别人的:

 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)的更多相关文章

  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, ...

  2. 8. leetcode 485. Max Consecutive Ones

    Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1, ...

  3. (双指针) leetcode 485. Max Consecutive Ones

    Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1, ...

  4. LeetCode 485 Max Consecutive Ones 解题报告

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

  5. [LeetCode]485. Max Consecutive Ones 找到最大的连续的1的个数

    题目描述 输入只有0和1的数组(长度为正整数,且<10000),找到最大的连续1的个数 比如[1,1,0,1,1,1],输出3 思路 遍历数组,统计当前连续个数curCount和最大连续值max ...

  6. 485. Max Consecutive Ones【easy】

    485. Max Consecutive Ones[easy] Given a binary array, find the maximum number of consecutive 1s in t ...

  7. 【leetcode】485. Max Consecutive Ones

    problem 485. Max Consecutive Ones solution1: class Solution { public: int findMaxConsecutiveOnes(vec ...

  8. 485. Max Consecutive Ones - LeetCode

    Question 485. Max Consecutive Ones Solution 题目大意:给一个数组,取连续1的最大长度 思路:遍历数组,连续1就加1,取最大 Java实现: public i ...

  9. 【LeetCode】485. Max Consecutive Ones 解题报告(Java & Python)

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

随机推荐

  1. hdu 1413 文件系统

    hdu   1413   文件系统         题目链接:pid=1413" target="_blank">http://acm.hdu.edu.cn/sho ...

  2. Qt 5.5.0 Windows环境搭建

    1)訪问官方站点:http://www.qt.io/download-open-source/ 2)选择离线安装包 3)选择 Windows 离线安装包(32 位或 64 位都可用,Windows 6 ...

  3. Java版TicTacToe

    MainFrame.java package com.bu_ish; import java.awt.BorderLayout; import java.awt.Color; import java. ...

  4. 第一个自定义开发的Arcgis地图

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  5. UVA1025 A Spy in the Metro —— DP

    题目链接: https://vjudge.net/problem/UVA-1025 题解: 详情请看紫书P267. 与其说是DP题,我觉得更像是模拟题,特别是用记忆化搜索写. 递推: #include ...

  6. RobotFramework教程使用笔记——Selenium2Library库

    selenium之前已经学习介绍过了,它是一个支持多语言.多平台.多浏览器的web自动化测试框架,在robotframework中也可以导入selenium库来进行web自动化测试.它使用seleni ...

  7. PYTHON 爬虫笔记一:爬虫基本原理梳理

    知识点一:爬虫的基本原理梳理 一.什么是爬虫? 请求网站并提取数据的自动化程序 二.爬虫的基本流程 1:向服务器发起请求 通过HTTP库向目标站点发起请求,即发送一个Request,请求可以包含额外的 ...

  8. 关于Ajax实现的简单示例

    一.代码示例 关于Ajax的基本概念(包括XMLHttpRequest对象及其相关方法属性)移步这里(w3school中文版)学习了解. <!doctype html> <html ...

  9. scanf()函数

    Scanf函数攻略: (A)                格式化说明符 格式字符           说明 %d                 读入十进制整数 %u                 ...

  10. WebApi发送HTML表单数据:文件上传与多部分MIME

    5.3 Sending HTML Form Data5.3 发送HTML表单数据(2) 本文引自:http://www.cnblogs.com/r01cn/archive/2012/12/20/282 ...