标签:leetcode top rip iii shm 差距 find absolute tco
原题:
https://leetcode.com/problems/contains-duplicate-iii/?tab=Description
Given an array of integers, find out whether there are two distinct indices i and j in the array such that the absolute difference between nums[i] and nums[j] is at most t and the absolute difference between i and j is at most k.
解法:
https://discuss.leetcode.com/topic/15199/ac-o-n-solution-in-java-using-buckets-with-explanation
真的很巧妙。这类题目一般是放一个hashmap,然后在hashmap里面找。找到了就ok,然后距离超过了,就移除。
但是这一道题目求的是差值不超过一定范围的。很巧妙,用了bucket,就是除以这个差值,然后只要满足条件的,就收缩到同样的桶里面了,然后最多再检查一下前面和后面的。
注意,一个桶也最多一个,因为落进一个桶,那么肯定满足条件了。
标签:leetcode top rip iii shm 差距 find absolute tco
原文地址:http://www.cnblogs.com/charlesblc/p/6522009.html