列表

详情


2367. 算术三元组的数目

给你一个下标从 0 开始、严格递增 的整数数组 nums 和一个正整数 diff 。如果满足下述全部条件,则三元组 (i, j, k) 就是一个 算术三元组

返回不同 算术三元组 的数目

 

示例 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 。

 

提示:

原站题解

去查看

上次编辑到这里,代码来自缓存 点击恢复默认模板
class Solution { public: int arithmeticTriplets(vector<int>& nums, int diff) { } };

swift 解法, 执行用时: 8 ms, 内存消耗: 13.8 MB, 提交时间: 2023-09-24 23:42:27

class Solution {
    func arithmeticTriplets(_ nums: [Int], _ diff: Int) -> Int {

        // 定义出现次数变量, 定义哈希表temp 添加nums前2个元素, 
        // temp初始为 [nums[0] : 1, nums[1] : 1]
        var res = 0, temp = [nums[0] : 1, nums[1] : 1]
       
        // 遍历, 因为前2个元素已经加入
        for i in 2..<nums.count {

            // 如果哈希表存在 nums[i] - diff, nums[i] - diff * 2
            if temp[nums[i] - diff] != nil && temp[nums[i] - diff * 2] != nil { 

                // 结果 + 1  
                res += 1
            }

            // 新增键值对, 当前数字做`Key`, 1为`value`
            temp[nums[i]] = 1
        }

        // 返回结果
        return res

    }
}

scala 解法, 执行用时: 564 ms, 内存消耗: 55.2 MB, 提交时间: 2023-09-24 23:40:50

object Solution {
    def arithmeticTriplets(nums: Array[Int], diff: Int): Int = {
        var right = nums.groupBy(x=>x).mapValues(_.length).toMap;
        var left = Map[Int, Int]()
        var ans = 0
        for (i <- nums){
            right = right + (i -> (right.getOrElse(i, 0) - 1))
            ans += left.getOrElse(i-diff, 0) * right.getOrElse(i+diff, 0)
            left = left + (i -> (left.getOrElse(i, 0) + 1))
        }
        ans
    }
}

rust 解法, 执行用时: 0 ms, 内存消耗: 2.2 MB, 提交时间: 2023-09-12 17:53:00

// hash
impl Solution {
    pub fn arithmetic_triplets(nums: Vec<i32>, diff: i32) -> i32 {
        use std::collections::HashSet;
        let (mut cnt, mut cache) = (0, HashSet::new());
        nums.into_iter().for_each(|num| {
            if cache.contains(&(num - diff)) && cache.contains(&(num - 2 * diff)) { cnt += 1 };
            cache.insert(num);
        });
        cnt
    }
}


/* 三指针
impl Solution {
    pub fn arithmetic_triplets(nums: Vec<i32>, diff: i32) -> i32 {
        let (mut i, mut j, mut k, mut cnt) = (0, 1, 2, 0);
        while k < nums.len() {
            while j < k && nums[j] + diff < nums[k] { j += 1; }
            if nums[j] + diff == nums[k] {
                while i < j && nums[i] + diff < nums[j] { i += 1; }
                if nums[i] + diff == nums[j] { cnt += 1; }
            }
            k += 1;
        }
        cnt
    }
}
*/

php 解法, 执行用时: 8 ms, 内存消耗: 18.8 MB, 提交时间: 2023-09-12 17:51:39

class Solution {

    /**
     * @param Integer[] $nums
     * @param Integer $diff
     * @return Integer
     */
    function arithmeticTriplets($nums, $diff) {
        $map = [];
        foreach ($nums as $num) {
            $map[$num] = 1;
        }
        $ans = 0;
        foreach ($nums as $num) {
            if (
                isset($map[$num + $diff]) &&
                isset($map[$num - $diff])
            )
                $ans++;
        }
        return $ans;
    }
}

golang 解法, 执行用时: 0 ms, 内存消耗: 2.1 MB, 提交时间: 2022-08-12 11:37:49

func arithmeticTriplets(nums []int, diff int) (ans int) {
	i, j := 0, 1
	for _, x := range nums[2:] {
		for nums[j]+diff < x {
			j++
		}
		if nums[j]+diff > x {
			continue
		}
		for nums[i]+diff*2 < x {
			i++
		}
		if nums[i]+diff*2 == x {
			ans++
		}
	}
	return
}

golang 解法, 执行用时: 0 ms, 内存消耗: 2.3 MB, 提交时间: 2022-08-12 11:37:26

func arithmeticTriplets(nums []int, diff int) (ans int) {
	set := map[int]bool{}
	for _, x := range nums {
		if set[x-diff] && set[x-diff*2] {
			ans++
		}
		set[x] = true
	}
	return
}

golang 解法, 执行用时: 0 ms, 内存消耗: 2.3 MB, 提交时间: 2022-08-12 11:36:34

func arithmeticTriplets(nums []int, diff int) (ans int) {
	set := map[int]bool{}
	for _, x := range nums {
		set[x] = true
	}
	for _, x := range nums {
		if set[x-diff] && set[x+diff] {
			ans++
		}
	}
	return
}

python3 解法, 执行用时: 32 ms, 内存消耗: 14.8 MB, 提交时间: 2022-08-12 11:36:07

class Solution:
    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:
        s = set(nums)
        return sum(x - diff in s and x + diff in s for x in nums)

上一题