跳转至

3663. 出现频率最低的数字

题目描述

给你一个整数 n,找出在其十进制表示中出现频率 最低 的数字。如果多个数字的出现频率相同,则选择 最小 的那个数字。

以整数形式返回所选的数字。

数字 x 的出现频率是指它在 n 的十进制表示中的出现次数。

 

示例 1:

输入: n = 1553322

输出: 1

解释:

n 中,出现频率最低的数字是 1,它只出现了一次。所有其他数字都出现了两次。

示例 2:

输入: n = 723344511

输出: 2

解释:

n 中,出现频率最低的数字是 7、2 和 5,它们都只出现了一次。

 

提示:

  • 1 <= n <= 231 - 1

解法

方法一:计数

我们用一个数组 \(\textit{cnt}\) 来统计每个数字出现的频率。遍历数字 \(n\) 的每一位,更新 \(\textit{cnt}\) 数组。

然后,我们用一个变量 \(f\) 来记录当前出现频率最低的数字的频率,以及一个变量 \(\textit{ans}\) 来记录对应的数字。

接下来,我们遍历 \(\textit{cnt}\) 数组,如果 \(0 \lt \textit{cnt}[x] \lt f\),说明我们找到了出现频率更低的数字,更新 \(f = \textit{cnt}[x]\),以及 \(\textit{ans} = x\)

遍历结束后,返回答案 \(\textit{ans}\) 即可。

时间复杂度 \(O(\log n)\),空间复杂度 \(O(1)\)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Solution:
    def getLeastFrequentDigit(self, n: int) -> int:
        cnt = [0] * 10
        while n:
            n, x = divmod(n, 10)
            cnt[x] += 1
        ans, f = 0, inf
        for x, v in enumerate(cnt):
            if 0 < v < f:
                f = v
                ans = x
        return ans
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
class Solution {
    public int getLeastFrequentDigit(int n) {
        int[] cnt = new int[10];
        for (; n > 0; n /= 10) {
            ++cnt[n % 10];
        }
        int ans = 0, f = 1 << 30;
        for (int x = 0; x < 10; ++x) {
            if (cnt[x] > 0 && cnt[x] < f) {
                f = cnt[x];
                ans = x;
            }
        }
        return ans;
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
class Solution {
public:
    int getLeastFrequentDigit(int n) {
        int cnt[10]{};
        for (; n > 0; n /= 10) {
            ++cnt[n % 10];
        }
        int ans = 0, f = 1 << 30;
        for (int x = 0; x < 10; ++x) {
            if (cnt[x] > 0 && cnt[x] < f) {
                f = cnt[x];
                ans = x;
            }
        }
        return ans;
    }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
func getLeastFrequentDigit(n int) (ans int) {
    cnt := [10]int{}
    for ; n > 0; n /= 10 {
        cnt[n%10]++
    }
    f := 1 << 30
    for x, v := range cnt {
        if v > 0 && v < f {
            f = v
            ans = x
        }
    }
    return
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
function getLeastFrequentDigit(n: number): number {
    const cnt: number[] = Array(10).fill(0);
    for (; n; n = (n / 10) | 0) {
        cnt[n % 10]++;
    }
    let [ans, f] = [0, Number.MAX_SAFE_INTEGER];
    for (let x = 0; x < 10; ++x) {
        if (cnt[x] > 0 && cnt[x] < f) {
            f = cnt[x];
            ans = x;
        }
    }
    return ans;
}

评论