题目:

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. idea 编辑yml文件没有联想功能,解决方案

    idea 编辑yml文件没有联想功能,解决方案 https://segmentfault.com/q/1010000010556550 按Ctrl+Shift+Alt+S,点Facets如果没有添加s ...

  2. Html5的placeholder属性(IE兼容)

    HTML5对Web Form做了很多增强,比方input新增的type类型.Form Validation等. Placeholder是HTML5新增的还有一个属性,当input或者textarea设 ...

  3. LeetCode: Binary Tree Postorder Traversal [145]

    [题目] Given a binary tree, return the postorder traversal of its nodes' values. For example: Given bi ...

  4. The server must be started under an unprivileged user ID to prevent

    mysql8 PostgreSQL [root@test local]# postgres -D /usr/local/pgsql/data"root" execution of ...

  5. git文章列表

    关于gitlab默认clone协议 Git实现从本地加入项目到远程仓库 翻翻git之---一个简单的标签控件 LabelView (随手发了两张小宝宝的玩耍照) id=1125" targe ...

  6. 一文读懂P2P和区块链的异同

    说到P2P,大家的第一反应肯定是网络贷款.非法集资...事实上,P2P不等于网络贷款,他是点对点,人对人的交易模式. 而对于近两年最热的区块链技术,他和P2P有什么区别呢?区块链数据交换模式包括私有链 ...

  7. 【Advanced Windows Phone Programming】在windows phone 8中解码mp3 和编码pcm

    转眼间不做wp开发,投身于php事业已然一年了,转身看到8.1的发布,俨然一片欣欣向荣的景象,但是开发社区却没比一年前有过多大的提高,这并不是一个好现象,遂在git上开源了之前音频处理库,希望能对社区 ...

  8. 矩阵管理——和visitor模式没有本质区别,都是为了避免资源重复

    矩阵管理中的员工是双线汇报的模式.其上司有两个,一个是流程上司,一个是专业上司.流程上司负责你的日常考核,专业上司负责你的晋升和任免. 管理条件 相对于矩阵管理的矩阵式组织,适合于某些较为庞大的全球性 ...

  9. v-for指令用法一

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...

  10. 「ZJOI2007」「LuoguP1169」棋盘制作(并查集

    题目描述 国际象棋是世界上最古老的博弈游戏之一,和中国的围棋.象棋以及日本的将棋同享盛名.据说国际象棋起源于易经的思想,棋盘是一个8×88 \times 88×8大小的黑白相间的方阵,对应八八六十四卦 ...