2903. 找出满足差值条件的下标 I

找出满足差值条件的下标 I

解法一: 双层循环

go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
func findIndices(nums []int, indexDifference int, valueDifference int) []int {
n := len(nums)
for i := 0; i < n; i++ {
for j := i + indexDifference; j < n; j++ {
if abs(nums[i]-nums[j]) >= valueDifference {
return []int{i, j}
}
}
}
return []int{-1, -1}
}

func abs(num int) int {
if num >= 0 {
return num
}
return -num
}
作者

wuhunyu

发布于

2024-05-25

更新于

2025-01-15

许可协议