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: ...
随机推荐
- Release Candidate
RC_百度百科 https://baike.baidu.com/item/RC/7311964?fr=aladdin RC=Release Candidate,含义是"发布候选版" ...
- log Configuration
Log4j – Configuring Log4j 2 - Apache Log4j 2 https://logging.apache.org/log4j/2.x/manual/configurati ...
- bootstrap学习心得
一.html的编写规范 <!DOCTYPE html> <html lang="zh-CN"> <head> <title>Page ...
- java在某个日期上添加n天的方法实现
//得到添加n天后的时间字符串 public String getAddDate(Date date,int n){ //格式转换 SimpleDateFormat sdf = new SimpleD ...
- Fleck websocket官方事例
Fleck websocket官方事例 server: using Fleck;using System;using System.Collections.Generic;using System.L ...
- I.MX6 按键开关机 PMIC 检测
/************************************************************************* * I.MX6 按键开关机 PMIC 检测 * 说 ...
- CodeForces-816B:Karen and Coffee (简单线段树)
To stay woke and attentive during classes, Karen needs some coffee! Karen, a coffee aficionado, want ...
- NOIP提高组2006-金明的预算方案
链接 分析:依赖型0-1背包问题,对于一个主件,可以挂0个,1个,2个附件,所以最终为4种状态情况下的最大值. #include "iostream" #include " ...
- 洛谷P2148 E&D——打表
题目:https://www.luogu.org/problemnew/show/P2148 先打表找个规律: #include<iostream> #include<cstdio& ...
- eclipse恢复界面默认设置
使用eclipse的时候有时候会一不小心把一些界面设置给弄乱,可以恢复默认界面设置 eclipse导航栏window选项卡 找到Perspective->点击Reset Perspective ...