Skip to content

Files

Latest commit

9f1c7ec · Apr 19, 2024

History

History
This branch is 1 commit ahead of, 1319 commits behind doocs/leetcode:main.

1426.Counting Elements

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
Apr 19, 2024
Apr 19, 2024
Jan 13, 2024
Dec 4, 2023
Jan 13, 2024
Dec 4, 2023
Jan 13, 2024
Jan 13, 2024
Dec 4, 2023
Dec 4, 2023

English Version

题目描述

给你一个整数数组 arr, 对于元素 x ,只有当 x + 1 也在数组 arr 里时,才能记为 1 个数。

如果数组 arr 里有重复的数,每个重复的数单独计算。

 

示例 1:

输入:arr = [1,2,3]
输出:2
解释:1 和 2 被计算次数因为 2 和 3 在数组 arr 里。

示例 2:

输入:arr = [1,1,3,3,5,5,7,7]
输出:0
解释:所有的数都不算, 因为数组里没有 2、4、6、8。

 

提示:

  • 1 <= arr.length <= 1000
  • 0 <= arr[i] <= 1000

解法

方法一:计数

我们可以用一个哈希表或数组 c n t 记录数组 a r r 中的每个数出现的次数,然后遍历 c n t 中的每个数 x ,如果 x + 1 也在 c n t 中,那么就将 c n t [ x ] 加到答案中。

时间复杂度 O ( n ) ,空间复杂度 O ( n ) 。其中 n 是数组 a r r 的长度。

class Solution:
    def countElements(self, arr: List[int]) -> int:
        cnt = Counter(arr)
        return sum(v for x, v in cnt.items() if cnt[x + 1])
class Solution {
    public int countElements(int[] arr) {
        int[] cnt = new int[1001];
        for (int x : arr) {
            ++cnt[x];
        }
        int ans = 0;
        for (int x = 0; x < 1000; ++x) {
            if (cnt[x + 1] > 0) {
                ans += cnt[x];
            }
        }
        return ans;
    }
}
class Solution {
public:
    int countElements(vector<int>& arr) {
        int cnt[1001]{};
        for (int x : arr) {
            ++cnt[x];
        }
        int ans = 0;
        for (int x = 0; x < 1000; ++x) {
            if (cnt[x + 1]) {
                ans += cnt[x];
            }
        }
        return ans;
    }
};
func countElements(arr []int) (ans int) {
	mx := slices.Max(arr)
	cnt := make([]int, mx+1)
	for _, x := range arr {
		cnt[x]++
	}
	for x := 0; x < mx; x++ {
		if cnt[x+1] > 0 {
			ans += cnt[x]
		}
	}
	return
}
function countElements(arr: number[]): number {
    const mx = Math.max(...arr);
    const cnt = Array(mx + 1).fill(0);
    for (const x of arr) {
        ++cnt[x];
    }
    let ans = 0;
    for (let i = 0; i < mx; ++i) {
        if (cnt[i + 1] > 0) {
            ans += cnt[i];
        }
    }
    return ans;
}
use std::collections::HashMap;

impl Solution {
    pub fn count_elements(arr: Vec<i32>) -> i32 {
        let mut cnt = HashMap::new();
        for &num in &arr {
            *cnt.entry(num).or_insert(0) += 1;
        }
        cnt.iter()
            .filter(|(&x, _)| cnt.contains_key(&(x + 1)))
            .map(|(_, &v)| v)
            .sum()
    }
}
/**
 * @param {number[]} arr
 * @return {number}
 */
var countElements = function (arr) {
    const mx = Math.max(...arr);
    const cnt = Array(mx + 1).fill(0);
    for (const x of arr) {
        ++cnt[x];
    }
    let ans = 0;
    for (let i = 0; i < mx; ++i) {
        if (cnt[i + 1] > 0) {
            ans += cnt[i];
        }
    }
    return ans;
};
class Solution {
    /**
     * @param Integer[] $arr
     * @return Integer
     */
    function countElements($arr) {
        $cnt = array_count_values($arr);
        $ans = 0;
        foreach ($cnt as $x => $v) {
            if (isset($cnt[$x + 1])) {
                $ans += $v;
            }
        }
        return $ans;
    }
}