485 Max Consecutive Ones 最大连续1的个数
给定一个二进制数组, 计算其中最大连续1的个数。
示例 1:
输入: [1,1,0,1,1,1]
输出: 3
解释: 开头的两位和最后的三位都是连续1,所以最大连续1的个数是 3.
注意:
输入的数组只包含 0 和1。
输入数组的长度是正整数,且不超过 10,000。
详见:https://leetcode.com/problems/max-consecutive-ones/description/
Java实现:
class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
int size=nums.length;
if(size==0||nums==null){
return 0;
}
int cnt=0;
int res=0;
for(int num:nums){
cnt=num==0?0:cnt+1;
res=Math.max(res,cnt);
}
return res;
}
}
C++实现:
class Solution {
public:
int findMaxConsecutiveOnes(vector<int>& nums) {
int cnt=0,res=0;
for(int num:nums)
{
cnt=num==0?0:cnt+1;
res=max(res,cnt);
}
return res;
}
};
485 Max Consecutive Ones 最大连续1的个数的更多相关文章
- 485. Max Consecutive Ones最大连续1的个数
网址:https://leetcode.com/problems/max-consecutive-ones/ 很简单的一题 class Solution { public: int findMaxCo ...
- [LeetCode] Max Consecutive Ones 最大连续1的个数
Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1, ...
- Leetcode485.Max Consecutive Ones最大连续1的个数
给定一个二进制数组, 计算其中最大连续1的个数. 示例 1: 输入: [1,1,0,1,1,1] 输出: 3 解释: 开头的两位和最后的三位都是连续1,所以最大连续1的个数是 3. 注意: 输入的数组 ...
- 485. Max Consecutive Ones - LeetCode
Question 485. Max Consecutive Ones Solution 题目大意:给一个数组,取连续1的最大长度 思路:遍历数组,连续1就加1,取最大 Java实现: public i ...
- 【leetcode】485. Max Consecutive Ones
problem 485. Max Consecutive Ones solution1: class Solution { public: int findMaxConsecutiveOnes(vec ...
- 485. Max Consecutive Ones【easy】
485. Max Consecutive Ones[easy] Given a binary array, find the maximum number of consecutive 1s in t ...
- 485. Max Consecutive Ones最长的连续1的个数
[抄题]: Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Inpu ...
- [LeetCode]485. Max Consecutive Ones 找到最大的连续的1的个数
题目描述 输入只有0和1的数组(长度为正整数,且<10000),找到最大的连续1的个数 比如[1,1,0,1,1,1],输出3 思路 遍历数组,统计当前连续个数curCount和最大连续值max ...
- 485. Max Consecutive Ones
题目 Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: ...
随机推荐
- cat /proc/cpuinfo | awk -F: '/name/{print $2}' | uniq -c
cat /proc/cpuinfo | awk -F: '/name/{print $2}' | uniq -c
- crontab -e 定时任务中的 脚本文件 路径
crontab -l 57 */1 * * * python /home/data/crontab_chk_url/personas/trunk/plugins/spider/chk_url_stat ...
- eclipse输入提示 设置
- aapt2 错误
android.enableAapt2=false Error:Execution failed for task ':app:preDebugAndroidTestBuild'. > Conf ...
- BZOJ_2017_[Usaco2009 Nov]硬币游戏_博弈论+DP
BZOJ_2017_[Usaco2009 Nov]硬币游戏_博弈论+DP Description 农夫约翰的奶牛喜欢玩硬币游戏,因此他发明了一种称为“Xoinc”的两人硬币游戏. 初始时,一个有N(5 ...
- 枚举子集 Codeforces306 Div2 B
题目 分析:用二进制法去枚举子集,同时判断满足条件的子集个数加1 #include "iostream" #include "cstdio" using nam ...
- 【前端】CentOS 7 系列教程之四: 配置 git 服务器自动部署
转载请注明出处:http://www.cnblogs.com/shamoyuu/p/linux_4.html 安装pm2守护进程,备用 npm install -g pm2 创建/srv/www文件夹 ...
- bzoj1925地精部落——数学
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1925 真是精妙的递推式...好难想到啊: 详见这位的博客:https://www.cnblo ...
- laravel5.2 增加Caffienate Modules,实现模块化开发
1.模块化开发可以把框架分成 Topc前台模块,Topm手机端前台,Admin后台管理模块,每个模块中都有自己的一套Controller,Logic,router等. 2.咖啡因模块是一个简单的包,以 ...
- .NETFramework:Cache
ylbtech-.NETFramework:Cache 1.返回顶部 1. #region 程序集 System.Web, Version=4.0.0.0, Culture=neutral, Publ ...