首页 > 其他分享 >LeetCode #485 最大连续 1 的个数

LeetCode #485 最大连续 1 的个数

时间:2023-04-12 15:01:17浏览次数:34  
标签:count nums int max 个数 Maxcount 485 LeetCode

解题思路
基础题,最后加一个特殊情况处理就好,时间复杂度O(n)

代码

 

 

class Solution {
public:
int findMaxConsecutiveOnes(vector<int>& nums) {
int count=0;
int Maxcount=0;
for(int i =0; i < nums.size(); i++){
if(nums[i] == 1){
count ++;
}else{
Maxcount = max(Maxcount,count);
count = 0;
}
}
Maxcount = max(Maxcount,count); //防止【0,0,0,0,1】这种情况,Maxcount记录不上最后一位
return Maxcount;
}
};

 

标签:count,nums,int,max,个数,Maxcount,485,LeetCode
From: https://www.cnblogs.com/miracle-cst/p/17309814.html

相关文章

  • #yyds干货盘点# LeetCode面试题:矩阵置零
    1.简述:给定一个 mxn的矩阵,如果一个元素为0,则将其所在行和列的所有元素都设为0。请使用原地算法。 示例1:输入:matrix=[[1,1,1],[1,0,1],[1,1,1]]输出:[[1,0,1],[0,0,0],[1,0,1]]示例2:输入:matrix=[[0,1,2,0],[3,4,5,2],[1,3,1,5]]输出:[[0,0,0,0],[0,4,5,0],[0,3,1,0]]......
  • 4月11日leetcode练习
    设计一个支持push,pop,top操作,并能在常数时间内检索到最小元素的栈。实现MinStack类:MinStack()初始化堆栈对象。voidpush(intval)将元素val推入堆栈。voidpop()删除堆栈顶部的元素。inttop()获取堆栈顶部的元素。intgetMin()获取堆栈中的最小元素。 来源:力扣(Le......
  • leetcode 183
    从不订购的客户 selectc.NameasCustomersfromCustomerscleftjoinOrdersoonc.Id=o.CustomerIdwhereo.CustomerIdisnull selectcustomers.namecustomersfromcustomerswherecustomers.idnotin(selectorders.customeridfromorders) ......
  • leetcode 181
    超过经理收入的员工 selecte1.nameasEmployeefromEmployeee1,Employeee2wheree1.managerId=e2.idande1.salary>e2.salary selecte1.nameasEmployeefromEmployeee1leftjoinEmployeee2one1.managerId=e2.idwheree1.salary>e2.salary......
  • 【LeetCode回溯算法#extra01】集合划分问题【火柴拼正方形、划分k个相等子集、公平发
    火柴拼正方形https://leetcode.cn/problems/matchsticks-to-square/你将得到一个整数数组matchsticks,其中matchsticks[i]是第i个火柴棒的长度。你要用所有的火柴棍拼成一个正方形。你不能折断任何一根火柴棒,但你可以把它们连在一起,而且每根火柴棒必须使用一次。如......
  • [LeetCode] 2390. Removing Stars From a String
    Youaregivenastring s,whichcontainsstars *.Inoneoperation,youcan:Chooseastarin s.Removetheclosest non-star charactertoits left,aswellasremovethestaritself.Return thestringafter all starshavebeenremoved.Note:Thei......
  • 【剑指 Offer】 15. 二进制中1的个数
    【题目】编写一个函数,输入是一个无符号整数(以二进制串的形式),返回其二进制表达式中数字位数为'1'的个数(也被称为汉明重量).)。 提示:   请注意,在某些语言(如Java)中,没有无符号整数类型。在这种情况下,输入和输出都将被指定为有符号整数类型,并且不应影响您的实现,因为无论整数是......
  • 差分数组-leetcode1094
    车上最初有 capacity 个空座位。车 只能 向一个方向行驶(也就是说,不允许掉头或改变方向)给定整数 capacity 和一个数组 trips , trip[i]=[numPassengersi,fromi,toi] 表示第 i 次旅行有 numPassengersi 乘客,接他们和放他们的位置分别是 fromi 和 toi 。这些位......
  • leetcode_打卡1
    leetcode_打卡1题目:1768.交替合并字符串解答:思路:模拟即可,字符串的提取:a.charAt(i)classSolution{publicStringmergeAlternately(Stringword1,Stringword2){Stringresult="";intm=word1.length();intn=word2.length();......
  • #yyds干货盘点# LeetCode程序员面试金典:合并两个有序链表
    题目:将两个升序链表合并为一个新的升序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。  示例1:输入:l1=[1,2,4],l2=[1,3,4]输出:[1,1,2,3,4,4]示例2:输入:l1=[],l2=[]输出:[]示例3:输入:l1=[],l2=[0]输出:[0]代码实现:classSolution{publicLis......