C#LeetCode刷题之#485-最大连续1的个数(Max Consecutive Ones)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3714 访问。
给定一个二进制数组, 计算其中最大连续1的个数。
输入: [1,1,0,1,1,1]
输出: 3
解释: 开头的两位和最后的三位都是连续1,所以最大连续1的个数是 3.
注意:
输入的数组只包含 0 和1。
输入数组的长度是正整数,且不超过 10,000。
Given a binary array, find the maximum number of consecutive 1s in this array.
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
示例
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3714 访问。
public class Program {public static void Main(string[] args) {int[] nums = null;nums = new int[] { 1, 1, 0, 1, 1, 1 };var res = FindMaxConsecutiveOnes(nums);Console.WriteLine(res);res = FindMaxConsecutiveOnes2(nums);Console.WriteLine(res);Console.ReadKey();}private static int FindMaxConsecutiveOnes(int[] nums) {int count = 0;int max = 0;for(int i = 0; i < nums.Length; i++) {if(nums[i] == 0) {if(count > max) max = count;count = 0;} else {count++;}}return count > max ? count : max;}private static int FindMaxConsecutiveOnes2(int[] nums) {//同FindMaxConsecutiveOnes,只是写法稍微好看一点int max = 0;for(int i = 0, count = 0; i < nums.Length; i++) {count = nums[i] == 1 ? count + 1 : 0;max = count > max ? count : max;}return max;}}
以上给出2种算法实现,以下是这个案例的输出结果:
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3714 访问。
3
3
分析:
显而易见,以上2种算法的时间复杂度均为: 。
C#LeetCode刷题之#485-最大连续1的个数(Max Consecutive Ones)相关推荐
- LeetCode刷题实战603:连续空余座位
算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试.所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 ! 今天和大家 ...
- C#LeetCode刷题之#674-最长连续递增序列( Longest Continuous Increasing Subsequence)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3734 访问. 给定一个未经排序的整数数组,找到最长且连续的的递增 ...
- C#LeetCode刷题之#695-岛屿的最大面积( Max Area of Island)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3736 访问. 给定一个包含了一些 0 和 1的非空二维数组 gr ...
- leetcode(1)485——最大连续 1 的个数(C++)
文章目录 前言 程序 疑惑 vector& nums int v : nums 前言 程序, 边刷边总结边学语言. 程序 class Solution {public:int findMaxC ...
- 每天Leetcode 刷题 初级算法篇-位1的个数
题目要求: 力扣题解:循环和位移动 代码 /*** @program: mydemo* @description: 位1的个数* @author: Mr.zeng* @create: 2021-02- ...
- C#LeetCode刷题-数组
数组篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 43.1% 简单 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组 ...
- LeetCode刷题实战488:祖玛游戏
算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试.所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 ! 今天和大家 ...
- LeetCode刷题实战524:通过删除字母匹配到字典里最长单词
算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试.所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 ! 今天和大家 ...
- LeetCode刷题实战181: 超过经理收入的员工
算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试.所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 ! 今天和大家 ...
最新文章
- IE10浏览器兼容模式
- java78条注意事项
- mysql联合查询sql优化
- 输入法图标也会莫名其妙地丢失?
- linux下Intellij Idea 14的安装
- HDU Problem - 3763 CD(二分)
- mysql java dbutil_Java -- DBUtils 框架 操作MySQL数据库
- POJ NOI0101-08 字符三角形
- 洛谷 P2894 酒店 Hotel
- MySQL--pt-osc工具学习
- 好用的百度文库下载工具: 易读
- linux内存的优化大师,Linux性能优化大师(调整操作系统参数)
- 自然语言理解和自然语言处理_4种自然语言处理和理解的方法
- 计蒜客 青出于蓝胜于蓝(dfs序+树状数组)
- 【python基础】猜数字游戏
- 微信小程序获取用户信息后的解密操作
- PyQt学习随笔:QWidget的QFont的kerning、Antialiasing属性用途
- 从window到linux的折腾
- 15、TSA数据上传(https://www.ncbi.nlm.nih.gov/genbank/tsaguide/#SP)
- 网易云音乐每日歌曲推荐的原理是什么?