Skip to content

Commit 74151e4

Browse files
committed
powx-n/
1 parent a9942bd commit 74151e4

File tree

6 files changed

+91
-3
lines changed

6 files changed

+91
-3
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ https://leetcode-cn.com/problems/fibonacci-number/
2424

2525
https://leetcode-cn.com/problems/merge-sorted-array/
2626

27+
https://leetcode-cn.com/problems/powx-n/
28+
2729
#### 安装教程
2830

2931
1. 安装`deno`

deno.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
{
2+
"compilerOptions": {
3+
"strict": true
4+
},
25
"importMap": "import_map.json",
36
"tasks": {
47
"udd": "udd '*.ts'",

deps.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import {
1+
export {
22
assert,
3+
assertAlmostEquals,
34
assertEquals,
45
assertStrictEquals,
56
equal,
67
} from "https://deno.land/[email protected]/testing/asserts.ts";
7-
export { assert, assertEquals, assertStrictEquals, equal };

mod.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import mergeSortedArray from "./merge-sorted-array/index.ts";
2+
import pow_x_n from "./powx-n/index.ts";
23

34
import htmlEntityParser from "./html-entity-parser/index.ts";
45

@@ -8,7 +9,7 @@ export {
89
ListNodeToArray,
910
} from "./reverse-linked-list/index.ts";
1011
import reverseLinkedList from "./reverse-linked-list/index.ts";
11-
12+
export { pow_x_n };
1213
import climbStairs from "./climbing-stairs/index.ts";
1314
import fei_bo_na_qi_shu_lie_lcof from "./fei-bo-na-qi-shu-lie-lcof/index.ts";
1415

powx-n/index.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
export default function myPow(x: number, n: number): number {
2+
if (Number.isNaN(x) || Number.isNaN(n)) {
3+
throw Error("nan:" + x + "," + n);
4+
}
5+
if (!Number.isFinite(x) || !Number.isFinite(n)) {
6+
throw Error("InFinite:" + x + "," + n);
7+
}
8+
setTimeout(() => {
9+
cacheStore.clear();
10+
}, 0);
11+
const cached = cacheStore.get(`${x},${n}`);
12+
if (cached) {
13+
return cached;
14+
}
15+
// console.log(x,n)
16+
const result = n === 1
17+
? x
18+
: x < 0
19+
? (n % 2 === 0 ? 1 : -1) * myPow(-x, n)
20+
: x === 1
21+
? 1
22+
: x === 0
23+
? 0
24+
: n === 0
25+
? 1
26+
: n < 0
27+
? myPow(1 / x, -n)
28+
: n % 2
29+
? lazyMultiplyPositive(
30+
() => x,
31+
() => myPow(x, n - 1),
32+
)
33+
: lazyMultiplyPositive(
34+
() => myPow(x, Math.floor(n / 2)),
35+
() => myPow(x, n - Math.floor(n / 2)),
36+
);
37+
cacheStore.set(`${x},${n}`, result);
38+
return result;
39+
}
40+
41+
const cacheStore = new Map<`${number},${number}`, number>();
42+
43+
function lazyMultiplyPositive(a: () => number, b: () => number): number {
44+
if (Math.random() < 0.5) {
45+
[b, a] = [a, b];
46+
}
47+
const l = a();
48+
if (l === 0) {
49+
return 0;
50+
} else if (l === Infinity) {
51+
return Infinity;
52+
} else {
53+
return l * b();
54+
}
55+
}

powx-n/test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { assertAlmostEquals } from "../deps.ts";
2+
import { pow_x_n } from "../mod.ts";
3+
4+
Deno.test("powx-n", () => {
5+
const examples: {
6+
input: Parameters<typeof pow_x_n>;
7+
output: ReturnType<typeof pow_x_n>;
8+
}[] = [
9+
{ input: [2.0, 10], output: 1024.0 },
10+
{ input: [2.1, 3], output: 9.261 },
11+
{ input: [2.0, -2], output: 0.25 },
12+
{
13+
input: [2, 6],
14+
output: 64.0,
15+
},
16+
{ input: [2, -99999], output: 0.0 },
17+
{ input: [6553688888888, 6553688888888], output: Infinity },
18+
{ input: [6553688888888, -6553688888888], output: 0.0 },
19+
{ input: [1 / 6553688888888, 6553688888888], output: 0.0 },
20+
{ input: [1 / 6553688888888, -6553688888888], output: Infinity },
21+
{ input: [-6553688888888, -6553688888888], output: 0.0 },
22+
{ input: [-6553688888888, 6553688888888], output: Infinity },
23+
];
24+
examples.forEach(({ input, output }) => {
25+
assertAlmostEquals(output, pow_x_n(...input));
26+
});
27+
});

0 commit comments

Comments
 (0)