<div style="display: none;" hidden="true" aria-hidden="true" data-nosnippet>Are you an LLM? You can read better optimized documentation at /pages/798e78.md for this page in Markdown format</div>
算术三元组的数目
| Category | Difficulty | Likes | Dislikes |
|---|---|---|---|
| algorithms | Easy (84.18%) | 17 | - |
<details style="color: rgb(225, 228, 232); font-family: -apple-system, "system-ui", "Segoe WPC", "Segoe UI", system-ui, Ubuntu, "Droid Sans", sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><summary><strong>Tags</strong></summary><p style="margin-top: 0px; margin-bottom: 0.7em;"><a href="https://leetcode.com/tag/Unknown" title="https://leetcode.com/tag/Unknown" style="color: var(--vscode-textLink-foreground); text-decoration: none;"><code style="color: var(--vscode-textLink-foreground); font-family: var(--vscode-editor-font-family, "SF Mono", Monaco, Menlo, Consolas, "Ubuntu Mono", "Liberation Mono", "DejaVu Sans Mono", "Courier New", monospace); font-size: 1em; line-height: 1.357em; white-space: pre-wrap;"></code></a></p></details>
<details style="color: rgb(225, 228, 232); font-family: -apple-system, "system-ui", "Segoe WPC", "Segoe UI", system-ui, Ubuntu, "Droid Sans", sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><summary><strong>Companies</strong></summary><p style="margin-top: 0px; margin-bottom: 0.7em;"><code style="color: var(--vscode-textPreformat-foreground); font-family: var(--vscode-editor-font-family, "SF Mono", Monaco, Menlo, Consolas, "Ubuntu Mono", "Liberation Mono", "DejaVu Sans Mono", "Courier New", monospace); font-size: 1em; line-height: 1.357em; white-space: pre-wrap;"></code></p></details>
给你一个下标从 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 <= 2000 <= nums[i] <= 2001 <= diff <= 50nums严格 递增
/*
* @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