题目:

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;
}
};

最新文章

  1. 使用批处理文件在FTP服务器 上传下载文件
  2. Dubbo架构设计详解
  3. JVM 类型的生命周期学习
  4. 多语言文本资源的访问(Windows:ini)
  5. 【Android】 分享一个完整的项目,适合新手!
  6. CSS预处理语言——less与sass的使用
  7. 进阶-MongoDB 知识梳理
  8. 4.app是怎么炼成的
  9. day 05字典相关内容
  10. spring 动态代理
  11. C#中List按特定字段排序
  12. 【转】WPS word 文档中的插入对象 为什么打不开
  13. Python-CSS高级 题目
  14. Appium入门(4)__ Appium Client安装
  15. Eclipse如何导入DemoWeb.rar
  16. java RE Validation常用
  17. sourcetree回退已推送的代码
  18. node 跨域
  19. CAS-登出配置
  20. 从数据库表导出为excel表格

热门文章

  1. ubuntu安装交叉编译工具链
  2. Linux中ctrl+z 、ctrl+c、 ctrl+d差别
  3. sharding-jdbc源码学习(一)简介
  4. 在c代码中获取用户环境变量
  5. 使用Windows API发送HTTP请求
  6. HLS切片机
  7. Xmpp学习之Asmack取经-asmack入门(一)
  8. uboot之logo显示【转】
  9. hdu Integer Inquiry 解题报告
  10. 通过Chrome浏览器进行android调试/Remote Debugging on Android with Chrome