当前位置:网站首页>Leetcode question brushing record | 485_ Maximum number of consecutive ones
Leetcode question brushing record | 485_ Maximum number of consecutive ones
2022-07-08 02:10:00 【coder_ sure】
List of articles
subject
Given a binary array nums , Calculate the maximum continuity 1 The number of .
Example 1:
Input :nums = [1,1,0,1,1,1]
Output :3
explain : The first two digits and the last three digits are consecutive 1 , So the greatest continuity 1 The number of is 3.
Example 2:
Input :nums = [1,0,1,1,0,1]
Output :2
Tips :
- 1 <= nums.length <= 105
- nums[i] No 0 Namely 1.
Their thinking
Train of thought reminder : Double pointer ( Fast pointer and slow pointer )
Train of thought details :
- Define two pointers , for example fast and slow(fast It is used to record several consecutive 1,slow It is used to record the number of final output )
- Place both pointers at the beginning of the array , That is to say, make
fast = 0,slow = 0 - The fast pointer traverses the entire array in turn , Every forward position , Value of fast pointer +1,
When to take action ? - When the fast pointer encounters
be equal to 0When the number of , Take action at this time : Store the value of the slow pointer as the maximum value between the fast pointer and the slow pointer at this timeslow = max(slow,fast);Then set the value of the fast pointer again 0. - When the cycle is completed , Store the value of the slow pointer as the maximum value between the fast pointer and the slow pointer at this time
slow = max(slow,fast); - Finally back to
slow
Code implementation
c++
class Solution {
public:
int findMaxConsecutiveOnes(vector<int>& nums) {
int fast=0,slow = 0;
int n = nums.size();
for(int i = 0;i<n;i++){
if(nums[i]==1){
fast++;
}
else{
slow = max(slow,fast);
fast = 0;
}
}
slow = max(slow,fast);
return slow;
}
};
python
class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
if nums is None or len(nums)==0:
return 0
fast = 0
slow = 0
for i in range (0,len(nums)):
if(nums[i]==1):
fast = fast+1
else:
slow = max(fast,slow)
fast = 0
slow = max(fast,slow)
return slow
边栏推荐
- Nanny level tutorial: Azkaban executes jar package (with test samples and results)
- How to use diffusion models for interpolation—— Principle analysis and code practice
- burpsuite
- COMSOL --- construction of micro resistance beam model --- final temperature distribution and deformation --- addition of materials
- Talk about the cloud deployment of local projects created by SAP IRPA studio
- cv2-drawline
- cv2读取视频-并保存图像或视频
- WPF custom realistic wind radar chart control
- #797div3 A---C
- Redisson分布式锁解锁异常
猜你喜欢

Nacos microservice gateway component +swagger2 interface generation

Kwai applet guaranteed payment PHP source code packaging

Partage d'expériences de contribution à distance

SQLite3 data storage location created by Android

Application of slip ring in direct drive motor rotor

Flutter 3.0框架下的小程序运行

Matlab r2021b installing libsvm

Introduction to grpc for cloud native application development

《ClickHouse原理解析与应用实践》读书笔记(7)

XXL job of distributed timed tasks
随机推荐
Keras' deep learning practice -- gender classification based on inception V3
The body has a mysterious margin of 8px
咋吃都不胖的朋友,Nature告诉你原因:是基因突变了
[reinforcement learning medical] deep reinforcement learning for clinical decision support: a brief overview
[knowledge map] interpretable recommendation based on knowledge map through deep reinforcement learning
Neural network and deep learning-5-perceptron-pytorch
[recommendation system paper reading] recommendation simulation user feedback based on Reinforcement Learning
Talk about the realization of authority control and transaction record function of SAP system
Redismission source code analysis
[target tracking] |dimp: learning discriminative model prediction for tracking
阿南的判断
Where to think
nmap工具介绍及常用命令
2022年5月互联网医疗领域月度观察
常见的磁盘格式以及它们之间的区别
Can you write the software test questions?
Keras深度学习实战——基于Inception v3实现性别分类
Ml self realization /knn/ classification / weightlessness
快手小程序担保支付php源码封装
VIM use