LeetCode 485 Max Consecutive Ones 最大连续1的个数
来源:互联网 发布:apache 进程池 编辑:程序博客网 时间:2024/06/05 23:39
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
题目描述
给定只含0,1的数组,在数组中返回连续的1的最大个数
解题思路
遍历一遍数组,用count记录当前1的个数,如果为1,则count加1,否则count为0,num记录现有连续1的最大个数。
/* C++ */class Solution {public: int findMaxConsecutiveOnes(vector<int>& nums) { if(nums.size() == 0) return 0; int count = 0; int num = 0; for(int i = 0;i < nums.size(); i++){ if(nums[i] == 1) count++; else{ count = 0; } num = max(num,count); } return num; }};
阅读全文
0 0
- LeetCode 485 Max Consecutive Ones 最大连续1的个数
- [LeetCode] Max Consecutive Ones 最大的“1”连续个数
- LeetCode 485:Max Consecutive Ones(连续1的最大个数,边界条件)
- leetcode Max Consecutive Ones 最大连续长度
- Leetcode 485. Max Consecutive Ones 最大连续1 解题报告
- leetcode解题之485. Max Consecutive Ones Java版 (二进制连续最长1的个数)
- [LeetCode]485. Max Consecutive Ones(最大连续为1的数量)
- [LeetCode]485 Max Consecutive Ones
- [leetcode-485]Max Consecutive Ones
- [Leetcode 485 ]Max Consecutive Ones
- Leetcode 485 Max Consecutive Ones
- leetcode-485-Max Consecutive Ones
- leetcode 485 Max Consecutive Ones
- LeetCode 485 --- Max Consecutive Ones
- Leetcode #485 Max Consecutive Ones
- Leetcode 485 Max Consecutive Ones
- 【LeetCode】485 Max Consecutive Ones
- LeetCode-485 Max Consecutive Ones
- 三轴加速度传感器角度值转换原理
- Chrome浏览器安装路径修改方法
- switch语句
- Candy HDU
- centos 7RHEL7 忘记密码怎么修改root密码
- LeetCode 485 Max Consecutive Ones 最大连续1的个数
- 初次接触C语言如何入门
- 学习笔记8
- Android组件通信
- Java set 基本使用方法
- Angular4学习笔记(九)- 生命周期钩子简介
- MVC、MVP及MVVM
- ArcGIS 10.2 AE开发几个配置问题
- 使用wininet实现网络资源的下载