題目: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;