leetcode 1218. 最长定差子序列
作者:互联网
//时间复杂度有要求
class Solution {
public int longestSubsequence(int[] arr, int difference) {
int n=arr.length;
int ans=0;
HashMap<Integer,Integer> map=new HashMap<>();
for(int i=0;i<n;i++){
if(map.containsKey(arr[i]-difference)){
map.put(arr[i],map.get(arr[i]-difference)+1);
ans=Math.max(ans,map.get(arr[i]));
}
if(!map.containsKey(arr[i])) map.put(arr[i],0);
}
return ans+1;
}
}
标签:map,arr,HashMap,int,1218,longestSubsequence,差子,leetcode 来源: https://blog.csdn.net/sinat_32775443/article/details/121168379