就是统计以每个节点结尾的满足需求的。居然是结尾,不是起始,而且从-1开始,这都是点,记住
class Solution {
public int numSubarrayBoundedMax(int[] A, int L, int R) {
//思想就是如果当前节点在l到r之间,那么所有以他结尾的都是可以的,然后如果小于l的那种,要看他前面最近连续的(被last标记了),两者相等
int res=0;
int i=0;
int start=-1;
int last=-1;//表示未遇到》r的时候遍历的最后》l的
for(i=0;i<A.length;i++){
if(A[i]>R){
last=start=i;
}
if(A[i]>=L){
last=i;
}
res+=last-start;
}
return res;
}
}
下面这个和上面同理的,但是下面的更容易理解吧
class Solution {
public int numSubarrayBoundedMax(int[] A, int L, int R) {
int res=0;
int i=0;
int start=-1;
int last=-1;
for(int i=0;i<A.length;i++){
if(A[i]>R){
last=start=i;
}
if(A[i]>L){
last=i;
}
res+=last-start;
}
}
}