Skip to content

File tree

5 files changed

+64
-0
lines changed

5 files changed

+64
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -958,6 +958,10 @@ https://leetcode.cn/problems/merge-strings-alternately/
958958

959959
https://leetcode.cn/problems/insert-interval/
960960

961+
https://leetcode.cn/problems/shu-zu-zhong-shu-zi-chu-xian-de-ci-shu-lcof/
962+
963+
https://leetcode.cn/problems/shu-zu-zhong-shu-zi-chu-xian-de-ci-shu-ii-lcof/
964+
961965
#### 安装教程
962966

963967
1. 安装`deno`
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { counter } from "../substring-with-concatenation-of-all-words/counter.ts";
2+
3+
function singleNumber(nums: number[]): number {
4+
const cnt = counter(nums);
5+
6+
for (const [key, value] of cnt.entries()) {
7+
if (value === 1) return key;
8+
}
9+
return nums[0];
10+
}
11+
export default singleNumber;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { assertEquals } from "../deps.ts";
2+
import singleNumber from "./index.ts";
3+
4+
Deno.test("shu-zu-zhong-shu-zi-chu-xian-de-ci-shu-ii-lcof", () => {
5+
const data = [
6+
{
7+
input: [3, 4, 3, 3],
8+
output: 4,
9+
},
10+
{
11+
input: [9, 1, 7, 9, 7, 9, 7],
12+
output: 1,
13+
},
14+
];
15+
16+
for (let i = 0; i < data.length; i++) {
17+
const temp = data[i];
18+
const output = temp.output;
19+
const input = temp.input;
20+
const res = singleNumber(input);
21+
assertEquals(res, output);
22+
}
23+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { counter } from "../substring-with-concatenation-of-all-words/counter.ts";
2+
3+
function singleNumbers(nums: number[]): number[] {
4+
const cnt = counter(nums);
5+
6+
return Array.from(cnt)
7+
.filter((e) => e[1] === 1)
8+
.map((e) => e[0]);
9+
}
10+
export default singleNumbers;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
2+
import singleNumbers from "./index.ts";
3+
Deno.test("shu-zu-zhong-shu-zi-chu-xian-de-ci-shu-lcof", () => {
4+
const inputs = [
5+
[4, 1, 4, 6],
6+
[3, 2, 1, 3],
7+
];
8+
const outputs = [
9+
[1, 6],
10+
[1, 2],
11+
];
12+
assertEquals(
13+
outputs.map((a) => new Set(a)),
14+
inputs.map(singleNumbers).map((a) => new Set(a))
15+
);
16+
});

0 commit comments

Comments
 (0)