首页 > 其他 > 详细

【Todo】找出一定距离内相差距离有效的情况

时间:2017-03-08 19:26:35      阅读:158      评论:0      收藏:0      [点我收藏+]

原题:

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,就是除以这个差值,然后只要满足条件的,就收缩到同样的桶里面了,然后最多再检查一下前面和后面的。

注意,一个桶也最多一个,因为落进一个桶,那么肯定满足条件了。

 

【Todo】找出一定距离内相差距离有效的情况

原文:http://www.cnblogs.com/charlesblc/p/6522009.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!