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: ...
随机推荐
- JDK and JRE File Structure JAVA_HOME HotSpot优化技术
https://docs.oracle.com/javase/8/docs/technotes/tools/windows/jdkfiles.html Java Platform, Standard ...
- 使用NetBeans生成jar包,并在jar包中添加资源
在NetBeans中,执行Clean and Build便可得到jar文件 若要在jar中添加资源,先用压缩软件打开jar,然后将资源拖进当前归档文件即可 使用Class.getResource(St ...
- (C)do{...}while(0);的用法及意义
实际上,do{…}while(0)的作用远大于美化你的代码. 总结起来这样写主要有以下几点好处: 1. 辅助定义复杂的宏 避免引用的时候出错: 举例来说,假设你需要定义这样一个宏: #define D ...
- HDU1693 Eat the Trees —— 插头DP
题目链接:https://vjudge.net/problem/HDU-1693 Eat the Trees Time Limit: 4000/2000 MS (Java/Others) Mem ...
- 设置Tomcat的jvm内存问题
tomcat的jvm大小设置与操作系统以及jdk有关:具体来说: 1.操作系统是32bit的,程序最大内存访问空间是4G, 2的32次方,这是硬件决定的,跟windows linux没有任何关系. 2 ...
- codeforces 459E E. Pashmak and Graph(dp+sort)
题目链接: E. Pashmak and Graph time limit per test 1 second memory limit per test 256 megabytes input st ...
- NSString -- UILabel中字体有多种颜色,字符串自动计算高度/换行
一:UILabel中字体有多种颜色 UILabel *label = [[UILabel alloc] init]; label.frame = CGRectMake(, , , ); label.b ...
- bzoj2431逆序对数列——递推
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2431 考虑新加入一个数i,根据放的位置不同,可以产生0~i-1个新逆序对: 所以f[i][j ...
- 微信小程序在线支付功能使用总结
最近需要在微信小程序中用到在线支付功能,于是看了一下官方的文档,发现要在小程序里实现微信支付还是很方便的,如果你以前开发过服务号下的微信支付,那么你会发现其实小程序里的微信支付和服务号里的开发过程如出 ...
- Bootstrap Notify
https://github.com/mouse0270/bootstrap-notify $.notify('Hello World', { offset: { x: 50, y: 100 } }) ...