Skip to content

Commit 364c64d

Browse files
committed
fibonacciNumber(n: number): number | bigint,result < Number.MAX_SAFE_INTEGER
1 parent 3e1b6d7 commit 364c64d

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

fibonacci-number/index.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
// My code goes here
22
/**https://leetcode-cn.com/problems/fibonacci-number/ */
3-
export default function fibonacciNumber(n: number): number {
4-
return Number(getfbnq(BigInt(n)));
3+
export default function fibonacciNumber(n: number): number | bigint {
4+
const result = getfbnq(BigInt(n));
5+
if (result < Number.MAX_SAFE_INTEGER) {
6+
return Number(result);
7+
} else {
8+
return result;
9+
}
510
}
611

712
function getfbnq(num: bigint): bigint {

fibonacci-number/test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@ import { assertStrictEquals } from "../deps.ts";
22
import { fibonacciNumber } from "../mod.ts";
33

44
Deno.test("fibonacci-number", () => {
5-
const examples: { input: number; output: number }[] = [
5+
const examples: {
6+
input: number;
7+
output: ReturnType<typeof fibonacciNumber>;
8+
}[] = [
69
{ input: 2, output: 1 },
710
{ input: 3, output: 2 },
811
{ input: 4, output: 3 },
912
{ input: 30, output: 832040 },
1013
{ input: 20, output: 6765 },
14+
{ input: 99, output: 218922995834555169026n },
1115
];
1216
examples.forEach(({ input, output }) => {
1317
assertStrictEquals(fibonacciNumber(input), output);

two-sum/test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Deno.test("two-sum", () => {
99
{ input: [[3, 3], 6], output: [0, 1] },
1010
{ input: [[2, 7, 11, 15], 9], output: [0, 1] },
1111
{ input: [[3, 2, 4], 6], output: [1, 2] },
12-
{ input: [Array(25).fill(0).map((_v, i) => i), 30], output: [14,16] },
12+
{ input: [Array(25).fill(0).map((_v, i) => i), 30], output: [14, 16] },
1313
];
1414
examples.forEach(({ input, output }) => {
1515
assertEquals(new Set(twoSum(...input)), new Set(output));

0 commit comments

Comments
 (0)