Golang Leetcode 220. Contains Duplicate III.go

版权声明:原创勿转 https://blog.csdn.net/anakinsun/article/details/89043375

思路

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

code

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
}

更多内容请移步我的repo: https://github.com/anakin/golang-leetcode

猜你喜欢

转载自blog.csdn.net/anakinsun/article/details/89043375