Golang Leetcode 220. Contains Duplicate III.go

2019-04-12 11:34:46 浏览数 (1)

版权声明:原创勿转 https://cloud.tencent.com/developer/article/1412917

思路

双重循环,维持一个滑动窗口

code

代码语言:javascript复制
func containsNearbyAlmostDuplicate(nums []int, k int, t int) bool {
	if len(nums) == 0 {
		return false
	}
	for i := 1; i < len(nums); i   {
		j := 0
		if i-k > 0 {
			j = i - k
		}
		for ; j >= 0 && j < i; j   {
			if abs(nums[i]-nums[j]) <= t {
				return true
			}
		}
	}
	return false
}
func abs(x int) int {
	if x >= 0 {
		return x
	}
	return -x
}

0 人点赞