当前位置:网站首页>[leetcode] number of maximum consecutive ones
[leetcode] number of maximum consecutive ones
2022-07-02 00:33:00 【Xiao Zhu, Xiao Zhu will never admit defeat】
leetcode Medium maximum continuous 1 A summary of several categories of topics .
List of articles
Maximum continuous 1 The number of Ⅰ
1. Title Description
leetcode Topic link :485. Maximum continuous 1 The number of 
2. Thought analysis
Method 1 : One traverse
encounter nums[i] == 1, be count++, encounter nums[i] == 0, be count=0, Then update the maximum continuous 1 The number of .
Method 2 : Double pointer
Fixed left boundary , Then judge the right boundary , Update the difference between the maximum value and the right boundary minus the left boundary .
Method 3 : Dynamic programming
dp[i] Says to the first i Maximum continuity at the end of elements 1 The number of .
among : The double pointer method has the shortest running time .
3. Reference code
Method 1 : One traverse
class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
int res = 0, count = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == 1) {
count++;
} else {
count = 0;
}
res = Math.max(res, count);
}
return res;
}
}
Method 2 : Double pointer
class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
int res = 0;
for (int i = 0; i < nums.length; i++) {
int j = i;
while (j < nums.length && nums[j] == 1) {
j++;
}
res = Math.max(res, j - i);
i = j;
}
return res;
}
}
Method 3 : Dynamic programming
class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
int n = nums.length;
int[] dp = new int[n + 1];
int res = 0;
for (int i = 1; i <= n; i++) {
if (nums[i - 1] == 1) {
dp[i] = dp[i - 1] + 1;
res = Math.max(res, dp[i]);
}
}
return res;
}
}
Maximum continuous 1 The number of Ⅱ
1. Title Description
leetcode Topic link :Leetcode 487. Maximum continuous 1 The number of Ⅱ,plus Membership title , Look at the problem directly .
Given a binary array , You can at most 1 individual 0 Flip to 1, Find the largest one of them 1 The number of .
Input :[1,0,1,1,0]
Output :4
explain : Flip the first 0 You can get the longest continuous 1.
When flipped , Maximum continuous 1 The number of 4.
2. Thought analysis
3. Reference code
class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
int left = 0, right = 0;
int res = 0, count = 0;
while (right < nums.length) {
if (nums[right++] == 0) {
count++;
}
while (count > 1) {
if (nums[left++] == 0) {
count--;
}
}
res = Math.max(res, right - left);
}
return res;
}
}
Maximum continuous 1 The number of Ⅲ
1. Title Description
leetcode Topic link :1004. Maximum continuous 1 The number of III
2. Thought analysis
Maximum continuous 1 The number of Ⅱ An advanced version of , Yes k individual 0 Flip to 1.
3. Reference code
边栏推荐
- [CTF] bjdctf 2020 Bar _ Bacystack2
- 【微信授权登录】uniapp开发小程序,实现获取微信授权登录功能
- ThreadLocal内存泄漏是什么,怎么解决
- 数据库--SqlServer详解
- [opencv450] hog+svm and hog+cascade for pedestrian detection
- Niuke - Practice 101 - reasoning clown
- Windows10 install WSL (I) (wslregisterdistribution error)
- [template] adaptive Simpson integral
- 【CTF】bjdctf_2020_babystack2
- Flow control statement of SQL data analysis [if, case... When detailed]
猜你喜欢
![[embedded system course design] a single key controls the LED light](/img/c9/076618208bbab0b95faa5a7e644a07.png)
[embedded system course design] a single key controls the LED light

2022拼多多详情/拼多多商品详情/拼多多sku详情

GCC compilation

sso单点登录的实现。

九州云与英特尔联合发布智慧校园私有云框架,赋能教育新基建

How to type spaces in latex

Leetcode skimming: stack and queue 05 (inverse Polish expression evaluation)

Halcon knowledge: an attempt of 3D reconstruction

Qt5.12.9 migration tutorial based on Quanzhi H3

USB-IF协会与各种接口的由来
随机推荐
EMC circuit protection device for surge and impulse current protection
Window sorting functions rank and deny for SQL data analysis_ rank、raw_ Number and lag, lead window offset function [usage sorting]
Guide d'installation du serveur SQL
时间复杂度与空间复杂度
What does open loop and closed loop mean?
Review data desensitization system
Data analysis methodology and previous experience summary [notes dry goods]
leetcode96不同的二叉搜索树
SQL Server 安装指南
The origin of usb-if Association and various interfaces
SQL Server Installation Guide
实例讲解将Graph Explorer搬上JupyterLab
求逆序数的三个方法
Use the htaccess file to prohibit the script execution permission in the directory
LDR6035智能蓝牙音响可充可放(5.9.12.15.20V)快充快放设备充电
What is the purpose of ERP project implementation plan?
Soft exam information system project manager_ Compiled abbreviations of the top ten management processes to help memory recitation - -- software test advanced information system project manager 054
Relevant settings of wechat applet cache expiration time (recommended)
Node -- add compressed file
From 20s to 500ms, I used these three methods