Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions solution/0000-0099/0036.Valid Sudoku/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ tags:
<p><strong>示例 1:</strong></p>
<img src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0036.Valid%20Sudoku/images/250px-sudoku-by-l2g-20050714svg.png" style="height:250px; width:250px" />
<pre>
<strong>输入:</strong>board =
<strong>输入:</strong>board =
[["5","3",".",".","7",".",".",".","."]
,["6",".",".","1","9","5",".",".","."]
,[".","9","8",".",".",".",".","6","."]
Expand All @@ -57,7 +57,7 @@ tags:
<p><strong>示例 2:</strong></p>

<pre>
<strong>输入:</strong>board =
<strong>输入:</strong>board =
[["8","3",".",".","7",".",".",".","."]
,["6",".",".","1","9","5",".",".","."]
,[".","9","8",".",".",".",".","6","."]
Expand Down
4 changes: 2 additions & 2 deletions solution/0000-0099/0036.Valid Sudoku/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ tags:
<p><strong class="example">Example 1:</strong></p>
<img src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0036.Valid%20Sudoku/images/250px-Sudoku-by-L2G-20050714.svg.png" style="height:250px; width:250px" />
<pre>
<strong>Input:</strong> board =
<strong>Input:</strong> board =
[[&quot;5&quot;,&quot;3&quot;,&quot;.&quot;,&quot;.&quot;,&quot;7&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;]
,[&quot;6&quot;,&quot;.&quot;,&quot;.&quot;,&quot;1&quot;,&quot;9&quot;,&quot;5&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;]
,[&quot;.&quot;,&quot;9&quot;,&quot;8&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;6&quot;,&quot;.&quot;]
Expand All @@ -53,7 +53,7 @@ tags:
<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> board =
<strong>Input:</strong> board =
[[&quot;8&quot;,&quot;3&quot;,&quot;.&quot;,&quot;.&quot;,&quot;7&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;]
,[&quot;6&quot;,&quot;.&quot;,&quot;.&quot;,&quot;1&quot;,&quot;9&quot;,&quot;5&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;]
,[&quot;.&quot;,&quot;9&quot;,&quot;8&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;6&quot;,&quot;.&quot;]
Expand Down
2 changes: 1 addition & 1 deletion solution/3000-3099/3019.Number of Changing Keys/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ tags:
<pre>
<strong>输入:</strong>s = "aAbBcC"
<strong>输出:</strong>2
<strong>解释:</strong>
<strong>解释:</strong>
从 s[0] = 'a' 到 s[1] = 'A',不存在按键变更,因为不计入 caps lock 或 shift 。
从 s[1] = 'A' 到 s[2] = 'b',按键变更。
从 s[2] = 'b' 到 s[3] = 'B',不存在按键变更,因为不计入 caps lock 或 shift 。
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ tags:
<pre>
<strong>Input:</strong> s = &quot;aAbBcC&quot;
<strong>Output:</strong> 2
<strong>Explanation:</strong>
<strong>Explanation:</strong>
From s[0] = &#39;a&#39; to s[1] = &#39;A&#39;, there is no change of key as caps lock or shift is not counted.
From s[1] = &#39;A&#39; to s[2] = &#39;b&#39;, there is a change of key.
From s[2] = &#39;b&#39; to s[3] = &#39;B&#39;, there is no change of key as caps lock or shift is not counted.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ tags:
<pre>
<strong>Input:</strong> nums = [1,3,2,4]
<strong>Output:</strong> 1
<strong>Explanation:</strong> We can select the subset {1}, which can be placed in the array as [1] which follows the pattern. Hence the answer is 1. Note that we could have also selected the subsets {2}, {3}, or {4}, there may be multiple subsets which provide the same answer.
<strong>Explanation:</strong> We can select the subset {1}, which can be placed in the array as [1] which follows the pattern. Hence the answer is 1. Note that we could have also selected the subsets {2}, {3}, or {4}, there may be multiple subsets which provide the same answer.
</pre>

<p>&nbsp;</p>
Expand Down
2 changes: 2 additions & 0 deletions solution/3600-3699/3658.GCD of Odd and Even Sums/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: 简单
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3658.GCD%20of%20Odd%20and%20Even%20Sums/README.md
rating: 1220
source: 第 464 场周赛 Q1
---

<!-- problem:start -->
Expand Down
2 changes: 2 additions & 0 deletions solution/3600-3699/3658.GCD of Odd and Even Sums/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: Easy
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3658.GCD%20of%20Odd%20and%20Even%20Sums/README_EN.md
rating: 1220
source: Weekly Contest 464 Q1
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: 中等
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3659.Partition%20Array%20Into%20K-Distinct%20Groups/README.md
rating: 1439
source: 第 464 场周赛 Q2
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: Medium
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3659.Partition%20Array%20Into%20K-Distinct%20Groups/README_EN.md
rating: 1439
source: Weekly Contest 464 Q2
---

<!-- problem:start -->
Expand Down
2 changes: 2 additions & 0 deletions solution/3600-3699/3660.Jump Game IX/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: 中等
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3660.Jump%20Game%20IX/README.md
rating: 2187
source: 第 464 场周赛 Q3
---

<!-- problem:start -->
Expand Down
2 changes: 2 additions & 0 deletions solution/3600-3699/3660.Jump Game IX/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: Medium
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3660.Jump%20Game%20IX/README_EN.md
rating: 2187
source: Weekly Contest 464 Q3
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: 困难
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3661.Maximum%20Walls%20Destroyed%20by%20Robots/README.md
rating: 2525
source: 第 464 场周赛 Q4
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: Hard
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3661.Maximum%20Walls%20Destroyed%20by%20Robots/README_EN.md
rating: 2525
source: Weekly Contest 464 Q4
---

<!-- problem:start -->
Expand Down
179 changes: 179 additions & 0 deletions solution/3600-3699/3663.Find The Least Frequent Digit/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
---
comments: true
difficulty: 简单
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3663.Find%20The%20Least%20Frequent%20Digit/README.md
---

<!-- problem:start -->

# [3663. 出现频率最低的数字](https://leetcode.cn/problems/find-the-least-frequent-digit)

[English Version](/solution/3600-3699/3663.Find%20The%20Least%20Frequent%20Digit/README_EN.md)

## 题目描述

<!-- description:start -->

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

<p>以整数形式返回所选的数字。</p>

<p>数字 <code>x</code> 的出现频率是指它在&nbsp;<code>n</code> 的十进制表示中的出现次数。</p>

<p>&nbsp;</p>

<p><strong class="example">示例 1:</strong></p>

<div class="example-block">
<p><strong>输入:</strong> <span class="example-io">n = 1553322</span></p>

<p><strong>输出:</strong> 1</p>

<p><strong>解释:</strong></p>

<p>在 <code>n</code> 中,出现频率最低的数字是 1,它只出现了一次。所有其他数字都出现了两次。</p>
</div>

<p><strong class="example">示例 2:</strong></p>

<div class="example-block">
<p><strong>输入:</strong> <span class="example-io">n = 723344511</span></p>

<p><strong>输出:</strong> 2</p>

<p><strong>解释:</strong></p>

<p>在 <code>n</code> 中,出现频率最低的数字是 7、2 和 5,它们都只出现了一次。</p>
</div>

<p>&nbsp;</p>

<p><strong>提示:</strong></p>

<ul>
<li><code>1 &lt;= n &lt;= 2<sup>31</sup> - 1</code></li>
</ul>

<!-- description:end -->

## 解法

<!-- solution:start -->

### 方法一:计数

我们用一个数组 $\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)$。

<!-- tabs:start -->

#### Python3

```python
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
```

#### Java

```java
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;
}
}
```

#### C++

```cpp
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;
}
};
```

#### Go

```go
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
}
```

#### TypeScript

```ts
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;
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Loading