2024/11/1 灵茶题单 滑动窗口 恰好型滑动窗口
This commit is contained in:
parent
c6efbd9789
commit
4dec1f8774
|
@ -0,0 +1,30 @@
|
|||
package com.cool.ling_cha_mount.sliding_windows;
|
||||
|
||||
/**
|
||||
* Created with IntelliJ IDEA.
|
||||
*
|
||||
* @Author: Cool
|
||||
* @Date: 2024/11/01/23:33
|
||||
* @Description: 930. 和相同的二元子数组
|
||||
*/
|
||||
public class Num930 {
|
||||
public int numSubarraysWithSum(int[] nums, int goal) {
|
||||
int res = 0;
|
||||
int left1 = 0;
|
||||
int left2 = 0;
|
||||
int sum1 = 0;
|
||||
int sum2 = 0;
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
sum1 += nums[i];
|
||||
sum2 += nums[i];
|
||||
while (left1 <= i && sum1 > goal) {
|
||||
sum1 -= nums[left1++];
|
||||
}
|
||||
while (left2 <= i && sum2 >= goal) {
|
||||
sum2 -= nums[left2++];
|
||||
}
|
||||
res += left2 - left1;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue