mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2026-03-13 10:02:05 +08:00
添加 problem 220
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
package leetcode
|
||||
|
||||
func containsNearbyAlmostDuplicate(nums []int, k int, t int) bool {
|
||||
if len(nums) <= 1 {
|
||||
return false
|
||||
}
|
||||
if k <= 0 {
|
||||
return false
|
||||
}
|
||||
n := len(nums)
|
||||
for i := 0; i < n; i++ {
|
||||
count := 0
|
||||
for j := i + 1; j < n && count < k; j++ {
|
||||
if abs(nums[i]-nums[j]) <= t && j != i {
|
||||
return true
|
||||
}
|
||||
count++
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question220 struct {
|
||||
para220
|
||||
ans220
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para220 struct {
|
||||
one []int
|
||||
k int
|
||||
t int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans220 struct {
|
||||
one bool
|
||||
}
|
||||
|
||||
func Test_Problem220(t *testing.T) {
|
||||
|
||||
qs := []question220{
|
||||
|
||||
question220{
|
||||
para220{[]int{7, 1, 3}, 2, 3},
|
||||
ans220{true},
|
||||
},
|
||||
|
||||
question220{
|
||||
para220{[]int{-1, -1}, 1, -1},
|
||||
ans220{false},
|
||||
},
|
||||
|
||||
question220{
|
||||
para220{[]int{1, 2, 3, 1}, 3, 0},
|
||||
ans220{true},
|
||||
},
|
||||
|
||||
question220{
|
||||
para220{[]int{1, 0, 1, 1}, 1, 2},
|
||||
ans220{true},
|
||||
},
|
||||
|
||||
question220{
|
||||
para220{[]int{1, 5, 9, 1, 5, 9}, 2, 3},
|
||||
ans220{false},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 220------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans220, q.para220
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, containsNearbyAlmostDuplicate(p.one, p.k, p.t))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
||||
31
Algorithms/220. Contains Duplicate III/README.md
Normal file
31
Algorithms/220. Contains Duplicate III/README.md
Normal file
@@ -0,0 +1,31 @@
|
||||
# [220. Contains Duplicate III](https://leetcode.com/problems/contains-duplicate-iii/)
|
||||
|
||||
## 题目
|
||||
|
||||
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.
|
||||
|
||||
Example 1:
|
||||
|
||||
```c
|
||||
Input: nums = [1,2,3,1], k = 3, t = 0
|
||||
Output: true
|
||||
```
|
||||
|
||||
Example 2:
|
||||
|
||||
```c
|
||||
Input: nums = [1,0,1,1], k = 1, t = 2
|
||||
Output: true
|
||||
```
|
||||
Example 3:
|
||||
|
||||
```c
|
||||
Input: nums = [1,5,9,1,5,9], k = 2, t = 3
|
||||
Output: false
|
||||
```
|
||||
|
||||
## 题目大意
|
||||
|
||||
给出一个数组 num,再给 K 和 t。问在 num 中能否找到一组 i 和 j,使得 num[i] 和 num[j] 的绝对差值最大为 t,并且 i 和 j 之前的绝对差值最大为 k。
|
||||
|
||||
这道题就按照题意来,两层 for 循环。
|
||||
Reference in New Issue
Block a user