当前位置:网站首页>数组与字符串15-最大连续1的个数
数组与字符串15-最大连续1的个数
2022-08-03 05:25:00 【花开花落夏】
求最大连续1的个数
一 题目
源自leetcode官网
给定一个二进制数组, 计算其中最大连续 1 的个数。二 解题
思路:遍历数组,使用一个left指针,记录连续为1的子字符串里,开头的字符位置,当遍历到连续为1的子字符串结束时,计算出此子字符串的长度,与最大值max做对比。最终得到最长的连续为1的字符串的长度。
class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
int max = 0;
int left = 0;
for(int i=0;i<nums.length;i++){
if(nums[i]==1 && (i==0 || nums[i-1]==0)){
left=i;
}else if(nums[i]==0&&i>0&&nums[i-1]==1){
if(max<i-left){
max = i-left;
}
}
}
if(nums[nums.length-1]==1 && max<nums.length-left){
max=nums.length-left;
}
return max;
}
}
边栏推荐
猜你喜欢
ZEMAX | 在 OpticStudio 中使用自由曲面进行设计
ZEMAX | 如何使用渐晕系数
自监督论文阅读笔记Efficient Self-supervised Vision Pretraining with Local Masked Reconstruction
【第四周】MobileNet和HybridSN
损失函数(第五周)
自监督论文阅读笔记Index Your Position: A Novel Self-Supervised Learning Method for Remote Sensing Images Sema
自监督论文阅读笔记 S3Net:Self-supervised Self-ensembling Network for Semi-supervised RGB-D Salient Object Det
IPC 通信 - IPC
滚动条 scrollbar 和scrollbar-thumb 样式
Dynamic adjustment subject web system?Look at this one is enough
随机推荐
MCU接收串口字符型数据转换成数据型数据
A.1#【内存管理】——1.1.1 node:struct pglist_data
Automatic ticket issuance based on direct reduction of China Southern Airlines app
寄存器常见指令
自监督论文阅读笔记FIAD net: a Fast SAR ship detection network based on feature integration attention and self
ZEMAX | 如何围绕空间中的任何点旋转任何元素
classpath: comparison with classpath*
【第三周】ResNet+ResNeXt
ZEMAX | 如何倾斜和偏心序列光学元件
自监督论文阅读笔记 Self-Supervised Deep Learning for Vehicle Detection in High-Resolution Satellite Imagery
A.1#【内存管理】——1.1.4 node: 初始化
002_旭日X3派初探:TogetherROS安装
自监督论文阅读笔记Reading and Writing: Discriminative and Generative Modelingfor Self-Supervised Text Recogn
自监督论文阅读笔记 TASK-RELATED SELF-SUPERVISED LEARNING FOR REMOTE SENSING IMAGE CHANGE DETECTION
cmdline -[command line,__fdt_pointer,initial_boot_params] boot_command_line 获取
浮点型数据在内存中存储的表示
2021-04-30
什么是参数化设计,通过实操了解一下? | SOLIDWORKS 操作视频
9. Please introduce the class loading process, what is the parent delegation model?
cb板上常用的电子元器件都有哪些?