算术三元组的数目
# 算术三元组的数目 (opens new window)
Category | Difficulty | Likes | Dislikes |
---|---|---|---|
algorithms | Easy (84.18%) | 17 | - |
Companies
给你一个下标从 0 开始、严格递增 的整数数组 nums
和一个正整数 diff
。如果满足下述全部条件,则三元组 (i, j, k)
就是一个 算术三元组 :
i < j < k
,nums[j] - nums[i] == diff
且nums[k] - nums[j] == diff
返回不同 算术三元组 的数目*。*
示例 1:
输入:nums = [0,1,4,6,7,10], diff = 3
输出:2
解释:
(1, 2, 4) 是算术三元组:7 - 4 == 3 且 4 - 1 == 3 。
(2, 4, 5) 是算术三元组:10 - 7 == 3 且 7 - 4 == 3 。
1
2
3
4
5
2
3
4
5
示例 2:
输入:nums = [4,5,6,7,8,9], diff = 2
输出:2
解释:
(0, 2, 4) 是算术三元组:8 - 6 == 2 且 6 - 4 == 2 。
(1, 3, 5) 是算术三元组:9 - 7 == 2 且 7 - 5 == 2 。
1
2
3
4
5
2
3
4
5
提示:
3 <= nums.length <= 200
0 <= nums[i] <= 200
1 <= diff <= 50
nums
严格 递增
Discussion (opens new window) | Solution (opens new window)
/*
* @Author: 仲灏<izhaong@outlook.com>🌶🌶🌶
* @Date: 2022-11-14 15:56:11
* @LastEditTime: 2022-11-14 16:06:11
* @LastEditors: 仲灏<izhaong@outlook.com>🌶🌶🌶
* @Description:
* @FilePath: /面试题1/Users/izhaong/izhaong/Project_me/leetcode/2367.算术三元组的数目.ts
*/
/*
* @lc app=leetcode.cn id=2367 lang=typescript
*
* [2367] 算术三元组的数目
*
* https://leetcode.cn/problems/number-of-arithmetic-triplets/description/
*
* algorithms
* Easy (84.18%)
* Likes: 17
* Dislikes: 0
* Total Accepted: 13.1K
* Total Submissions: 15.5K
* Testcase Example: '[0,1,4,6,7,10]\n3'
*
* 给你一个下标从 0 开始、严格递增 的整数数组 nums 和一个正整数 diff 。如果满足下述全部条件,则三元组 (i, j, k) 就是一个
* 算术三元组 :
*
*
* i < j < k ,
* nums[j] - nums[i] == diff 且
* nums[k] - nums[j] == diff
*
*
* 返回不同 算术三元组 的数目。
*
*
*
* 示例 1:
*
* 输入:nums = [0,1,4,6,7,10], diff = 3
* 输出:2
* 解释:
* (1, 2, 4) 是算术三元组:7 - 4 == 3 且 4 - 1 == 3 。
* (2, 4, 5) 是算术三元组:10 - 7 == 3 且 7 - 4 == 3 。
*
*
* 示例 2:
*
* 输入:nums = [4,5,6,7,8,9], diff = 2
* 输出:2
* 解释:
* (0, 2, 4) 是算术三元组:8 - 6 == 2 且 6 - 4 == 2 。
* (1, 3, 5) 是算术三元组:9 - 7 == 2 且 7 - 5 == 2 。
*
*
*
*
* 提示:
*
*
* 3 <= nums.length <= 200
* 0 <= nums[i] <= 200
* 1 <= diff <= 50
* nums 严格 递增
*
*
*/
// @lc code=start
function arithmeticTriplets(nums: number[], diff: number): number {
const numsLen = nums.length;
let res = 0;
for (let i = 0; i < numsLen; i++) {
for (let j = i + 1; j < numsLen; j++) {
for (let k = j + 1; k < numsLen; k++) {
if (nums[j] - nums[i] === diff && nums[k] - nums[j] === diff) {
res++;
}
}
}
}
return res;
}
function arithmeticTriplets(nums: number[], diff: number): number {
const numsLen = nums.length;
let res = 0;
for (let i = 0; i < numsLen; i++) {
const nj = nums[i] + diff;
const nk = nj + diff;
if (nums.indexOf(nj) !== -1 && nums.indexOf(nk) !== -1) {
res++;
}
}
return res;
}
// @lc code=end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
上次更新: 2022/12/09, 22:58:08