题目:

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

Example 1:

  1. Input: [1,1,0,1,1,1]
  2. Output: 3
  3. Explanation: The first two digits or the last three digits are consecutive 1s.
  4. 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. class Solution {
  2. public:
  3. int findMaxConsecutiveOnes(vector<int>& nums) {
  4. int count = , max = ;
  5. for (int i = ; i < nums.size(); i++) {
  6. if (nums[i] == && (!i || nums[i - ] != )) count = ;
  7. else if (i && nums[i] == && nums[i - ] == ) count++;
  8. if (max < count) max = count;
  9. }
  10. if (max < count) max = count;
  11. return max;
  12. }
  13. };

自己的:

  1. class Solution {
  2. public:
  3. int findMaxConsecutiveOnes(vector<int>& nums) {
  4. int result = ;
  5. int tem = ;
  6. nums.push_back();
  7. for (auto c : nums){
  8. if(c == ){
  9. if ( tem > result)
  10. result = tem;
  11. tem = ;
  12. }
  13. else
  14. tem++;
  15. }
  16. return result;
  17. }
  18. };

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. JavaScript事件在WebKit中的处理流程研究

    本文主要探讨了JavaScript事件在WebKit中的注冊和触发机制. JS事件有两种注冊方式: 通过DOM节点的属性加入或者通过node.addEventListener()函数注冊: 通过DOM ...

  2. Eclipse快捷键大全(转载)

    一·eclipse 中的一些常用的快捷键Eclipse的编辑功能非常强大,掌握了Eclipse快捷键功能,能够大大提高开发效率.Eclipse中有如下一些和编辑相关的快捷键.   1. [ALT+/] ...

  3. 在JS中将JSON的字符串解析成JSON数据格式

    使用eval函数来解析 <script> var data="{root: [{name:'1',value:'0'},{name:'6101',value:'北京市'},{na ...

  4. mac gem命令

    $ gem sources -r https://rubygems.org/ (移除旧版本的镜像,如果你不知道你电脑上目前用的是什么镜像,可用  $ gem sources -l  来查看)  $ g ...

  5. 锁定xcode api 文档

    1, 打开终端2, 前往Xcode.app, 命令: cd /Applications/Xcode.app 3, 把头文件修改为只读, 命令: sudo chown -hR root:wheel Co ...

  6. "php"正则表达式使用总结

    一直对php的正则表达式的理解不是很深刻,而且是很一知半解,所以把自己用的的正则表达式总结下,以后方便查阅,以后遇到正则表达式的时候然后再追加到该文档的后面: /** * php常用正则表达式 * p ...

  7. 骨牌覆盖问题 KxM

    前面我们说了一些简单的骨牌覆盖问题,有了上面的经验,我们可以尝试解决K*M的 思路和上一篇文章所提到的3*N的 很类似: 依然是矩阵快速幂.我们需要把一个小的边固定下来作为的已知边,然后进行矩阵快速幂 ...

  8. windows下使用emacs+plink远程编辑erlang文件

    1)plink.exe属于putty套件, 注册到环境变量;emacs的bin目录也要注册到环境变量中; 2)在.emacs中增加如下: (require 'tramp)(setq tramp-def ...

  9. hadoop源码剖析--RawLocalFileSystem

    RawLocalFileSystem是hadoop中实现的本地文件系统,在该类中与文件元数据和目录相关的操作,都是通过适配方式适配到java.io.File的对应API来完成的,适配过程简单,代码清晰 ...

  10. codeforces 665C C. Simple Strings(乱搞)

    题目链接: C. Simple Strings time limit per test 2 seconds memory limit per test 256 megabytes input stan ...