Skip to content

Commit e09da42

Browse files
committed
chore: update 3289. The Two Sneaky Numbers of Digitville
1 parent 03ba6b3 commit e09da42

4 files changed

Lines changed: 123 additions & 0 deletions

File tree

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
---
2+
title: 3289. The Two Sneaky Numbers of Digitville
3+
description: In the town of Digitville, there was a list of numbers called nums containing integers from 0 to n - 1. Each number was supposed to appear exactly once in the list, however, two mischievous numbers sneaked in an additional time, making the list longer than usual. As the town detective, your task is to find these two sneaky numbers. Return an array of size two containing the two numbers (in any order), so peace can return to Digitville.
4+
keywords:
5+
[
6+
LeetCode,
7+
3289. The Two Sneaky Numbers of Digitville,
8+
Easy,
9+
Array,
10+
Hash Table,
11+
Math,
12+
]
13+
tags: [LeetCode, Easy, Array, Hash Table, Math]
14+
---
15+
16+
# [{frontMatter.title}](https://leetcode.com/problems/special-array-i)
17+
18+
import Tabs from "@theme/Tabs";
19+
import TabItem from "@theme/TabItem";
20+
import CodeBlock from "@theme/CodeBlock";
21+
import DifficultyBadge from "@site/src/components/Badges/DifficultyBadge";
22+
import Solution from "!!raw-loader!./solution.js";
23+
import Description from "./_Description.md";
24+
import Examples from "./_Examples.md";
25+
26+
<DifficultyBadge difficulty="Easy" />
27+
28+
<Tabs>
29+
<TabItem value="description" label="題目描述" default>
30+
<details open>
31+
<summary>Description</summary>
32+
<Description />
33+
<Examples />
34+
</details>
35+
</TabItem>
36+
37+
<TabItem value="solution" label="解答">
38+
## Solution
39+
<CodeBlock language="js">{Solution}</CodeBlock>
40+
</TabItem>
41+
</Tabs>
42+
43+
## 解題思路
44+
45+
想法是使用 counter 來記錄數字出現的數量
46+
47+
```js
48+
for (let i = 0; i < nums.length; i++) {
49+
if (counter[nums[i]]) {
50+
counter[nums[i]]++;
51+
} else {
52+
counter[nums[i]] = 1;
53+
}
54+
}
55+
```
56+
57+
遍歷一遍陣列後再次遍歷 `counter`,若出現兩次則記錄起來,最後回傳答案。
58+
59+
```js
60+
for (let key in counter) {
61+
if (counter[key] === 2) {
62+
result.push(parseInt(key));
63+
}
64+
}
65+
```
66+
67+
## 心得
68+
69+
我的答案好像滿冗長的,不知是否可以簡化。
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
In the town of Digitville, there was a list of numbers called nums containing integers from 0 to n - 1. Each number was supposed to appear exactly once in the list, however, two mischievous numbers sneaked in an additional time, making the list longer than usual.
2+
3+
As the town detective, your task is to find these two sneaky numbers. Return an array of size two containing the two numbers (in any order), so peace can return to Digitville.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#### Example 1:
2+
3+
> **Input:** nums = [0,1,1,0]
4+
> **Output:** [0,1]
5+
> **Explanation:**
6+
> The numbers 0 and 1 each appear twice in the array.
7+
8+
#### Example 2:
9+
10+
> **Input:** nums = [0,3,2,1,3,2]
11+
> **Output:** [2,3]
12+
> **Explanation:**
13+
> The numbers 2 and 3 each appear twice in the array.
14+
15+
#### Example 3:
16+
17+
> **Input:** nums = [7,1,5,4,3,4,6,0,9,5,8,2]
18+
> **Output:** [4,5]
19+
> **Explanation:**
20+
> The numbers 4 and 5 each appear twice in the array.
21+
22+
## Constraints:
23+
24+
- `2 <= n <= 100`
25+
- `nums.length == n + 2`
26+
- `0 <= nums[i] < n`
27+
- The input is generated such that `nums` contains **exactly** two repeated elements.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number[]}
4+
*/
5+
var getSneakyNumbers = function (nums) {
6+
const counter = {};
7+
const result = [];
8+
9+
for (let i = 0; i < nums.length; i++) {
10+
if (counter[nums[i]]) {
11+
counter[nums[i]]++;
12+
} else {
13+
counter[nums[i]] = 1;
14+
}
15+
}
16+
17+
for (let key in counter) {
18+
if (counter[key] === 2) {
19+
result.push(parseInt(key));
20+
}
21+
}
22+
23+
return result;
24+
};

0 commit comments

Comments
 (0)