2024/10/25 灵茶题单 不定长滑动窗口

This commit is contained in:
Cool 2024-10-25 17:40:45 +08:00
parent db30196d08
commit ec621a7da2
1 changed files with 25 additions and 0 deletions

View File

@ -0,0 +1,25 @@
package com.cool.ling_cha_mount.sliding_windows;
/**
* Created with IntelliJ IDEA.
*
* @Author: Cool
* @Date: 2024/10/25/17:40
* @Description: 1004. 最大连续1的个数 III
* @Score 1656
*/
public class Num1004 {
public int longestOnes(int[] nums, int k) {
int zNum=0;
int res=0;
int left=0;
for(int i=0;i<nums.length;i++){
zNum+=(nums[i]^1);
while(zNum>k){
zNum-=(nums[left++]^1);
}
res=Math.max(res,i-left+1);
}
return res;
}
}