当前位置:网站首页>LeetCode_485_Maximum number of consecutive 1s
LeetCode_485_Maximum number of consecutive 1s
2022-08-08 03:32:00 【Fitz1318】
题目链接
题目描述
给定一个二进制数组 nums , 计算其中最大连续 1 的个数.
示例 1:
输入:nums = [1,1,0,1,1,1]
输出:3
解释:开头的两位和最后的三位都是连续 1 ,所以最大连续 1 的个数是 3.
示例 2:
输入:nums = [1,0,1,1,0,1]
输出:2
提示:
1 <= nums.length <= 10^5nums[i]不是0就是1.
解题思路
- 记录最大的连续1的个数
ansand the current continuous1的个数now - 如果当前元素是1,则
now++,Otherwise use the previous continuous1的个数更新ans,并令now = 0 - 遍历数组结束之后,需要再次使用
now来更新ans,因为数组的最后一个元素可能是1,且最长连续1的子数组可能出现在数组的末尾,If it doesn't update after traversing the arrayans,则会导致结果错误
AC代码
class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
int ans = 0;
int now = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == 1) {
now++;
} else {
ans = Math.max(ans, now);
now = 0;
}
}
ans = Math.max(ans, now);
return ans;
}
}
边栏推荐
- 妙才周刊
- egg-session stores data to redis
- 剑指Offer 18.删除链表的节点
- C# "Original CSharp" 4th Chapter It is common for people to change in time, but it is difficult to know the succession of humanities
- 模拟登录——添加cookies,使用postmanget请求网页数据
- After nibbling on this Ali manual, the new year will enter Ali from seven sides
- 基于图像二维熵的视频信号丢失检测(Signal Loss Detection)
- 面向6G的通信感知一体化架构与关键技术
- ICML 2022 | LeNSE: Subgraph-Based Large-Scale Combinatorial Optimization: Achieving Over 140x Speedup
- 1.9 and orderly array insert PTA problem sets
猜你喜欢

PC Museum (5) 1975 IMSAI 8080

121. The Best Time to Buy and Sell Stock, the Best opportunity to Buy and Sell stocks

2022年最新《谷粒学院开发教程》:9 - 前台课程模块

After nibbling on this Ali manual, the new year will enter Ali from seven sides

prometheus学习1部署了解

巧记硬链接与软链接

RV-GAN: Segmentation of retinal vascular structures in fundus photographs using a novel multi-scale generative adversarial network

意识的概念框架:我们的意识从注意图式产生?

121. Best Time to Buy and Sell Stock买卖股票的最佳时机

NVIDIA NCCL优化——利用共享内存实现比NCCL更快的集合通信
随机推荐
海外元宇宙媒体宣发一定要关注到宣传的整体性
保姆级教程!Golang微服务简洁架构实战
Seven Steps to Hands-On Machine Learning
json工具类
The fourth chapter of the original CSharp c # common years is hard to know the humanistic one after another
mmedicting的get_flops.py的使用
C#《原CSharp》第四回 人常见岁月更替 却难知人文相继
LED驱动程序进一步优化-分层-分离
巧记硬链接与软链接
测试岗电话面试——必问题型
C# "Original CSharp" 4th Chapter It is common for people to change in time, but it is difficult to know the succession of humanities
工厂模式设计思路
期货公司有各种的开户手续费
新手使用 go channel 需要注意的问题
🤫 linkET | 完美解决ggcor安装失败方案(附教程)
找素数问题
136. Single Number A number that only appears once
Typical Data Warehouse Modeling Methodology
new和malloc的区别
VSCode打开 C(嵌入式) 工程的一些记录