How to use JavaScript BigInt and Number.prototype.toString to handle the super large integer problems All In One
How to use JavaScript BigInt and Number.prototype.toString to handle the super large integer problems All In One
如何使用 JavaScript
BigInt
和Number.prototype.toStringg
处理超大整数问题
errors
function plusOne(digits: number[]): number[] {
let n = parseInt(digits.join(``));
return `${n + 1}`.split(``).map(Number);
};
// // 大数相加超出 JavaScript 中的最大安全整数 (2**53 – 1) 最大值 bug ❌
solutions
function plusOne(digits: number[]): number[] {
const arr: number[] = digits;
// 进位
let inc = 1;
for (let i = arr.length - 1; i >= 0; i--) {
const n = arr[i] + inc;
if(n >= 10 && inc) {
arr[i] = n - 10;
inc = 1;
} else {
arr[i] = n;
inc = 0;
}
}
// 最高一位进位
if(inc) {
arr.unshift(inc);
}
return arr;
};
function plusOne(digits: number[]): number[] {
let rest: number = 1;
let temp: number = 0;
for(let i = digits.length -1; i >= 0; i--) {
temp = digits[i] + rest;
rest = temp > 9 ? 1 : 0;
digits[i] = temp > 9 ? 0 : temp;
}
// 插入 1 进位
if(rest) {
digits.unshift(1);
}
return digits;
};
function plusOne(digits: number[]): number[] {
return (BigInt(digits.join(``)) + BigInt(1)).toString().split(``).map(Number);
};
demos
function plusOne(digits: number[]): number[] {
let rest: number = 1;
let temp: number = 0;
for(let i = digits.length -1; i >= 0; i--) {
temp = digits[i] + rest;
rest = temp > 9 ? 1 : 0;
digits[i] = temp > 9 ? 0 : temp;
}
// 插入 1 进位
if(rest) {
digits.unshift(1);
}
return digits;
};
// BigInt ❓ https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt
// 大数相加超出 JavaScript 中的最大安全整数 (2**53 – 1) 最大值 bug ❌
/*
6145390195186705543 + 1
// 6145390195186705000
*/
// function plusOne(digits: number[]): number[] {
// let n = parseInt(digits.join(``));
// return `${n + 1}`.split(``).map(Number);
// };
/*
Wrong Answer
71 / 111 testcases passed
Input
digits =
[6,1,4,5,3,9,0,1,9,5,1,8,6,7,0,5,5,4,3]
Output
[6,1,4,5,3,9,0,1,9,5,1,8,6,7,0,5,0,0,0]
Expected
[6,1,4,5,3,9,0,1,9,5,1,8,6,7,0,5,5,4,4]
*/
https://leetcode.com/problems/plus-one/description/
BigInt
digits = [6,1,4,5,3,9,0,1,9,5,1,8,6,7,0,5,5,4,3]
// (19) [6, 1, 4, 5, 3, 9, 0, 1, 9, 5, 1, 8, 6, 7, 0, 5, 5, 4, 3]
BigInt(digits.join(''))
// 6145390195186705543n
BigInt(digits.join('')).toString().split("").map(Number)
// (19) [6, 1, 4, 5, 3, 9, 0, 1, 9, 5, 1, 8, 6, 7, 0, 5, 5, 4, 3]
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt
Number.MAX_SAFE_INTEGER
The Number.MAX_SAFE_INTEGER
static data property represents the maximum safe integer
in JavaScript (2**53 – 1
).
Number.MAX_SAFE_INTEGER 静态数据
属性表示 JavaScript 中的最大安全整数
(2**53 – 1
)。
Number.MAX_SAFE_INTEGER
9007199254740991
2 ** 64 - 1
18446744073709552000
2 ** 32 - 1
4294967295
refs
©xgqfrms 2012-2025
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/18230786
未经授权禁止转载,违者必究!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
2023-06-04 Vim insert mode All In One
2023-06-04 Linux shell command ln All In One
2023-06-04 Linux shell command cut All In One
2023-06-04 How to fix the bug that there is no signal when the Raspberry Pi is connected to a monitor with an HDMI cable All In One
2023-06-04 Raspberry Pi command line tools vcgencmd All In One
2022-06-04 SwiftUI custom MapAnnotation All In One
2021-06-04 js undefined bug All In One