题目:https://leetcode-cn.com/problems/longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit/
题意:对于给定数组,和指定的绝对值差,找到最大的子串,其中最大的绝对值差不超过指定的绝对值差
AC代码:
int ans=0;
int lower=0;//找到最小的数的下标
if(nums.length==1)return 1;
for(int i=0;i<nums.length;i++) {
for(int j=i-1;j>=lower;j--) {
if(nums[i]==nums[j]) {//之前出现过该情况,直接退出,不用改动lower
break;
}
if(Math.abs(nums[j]-nums[i])>limit) {//判断
lower=j+1;
break;
}
}
ans=Math.max(ans,i-lower+1);//找到最大的长度
}
return ans;