LeetCode 算法题解 & js 大数相加计算解析 All In One
LeetCode 算法题解 & js 大数相加计算解析 All In One
js 大数相加
原理
JavaScript 的安全整数范围是 -253-1 ~ 253-1 ,即: -9007199254740991
~ 9007199254740991
;
换句话说,整数超过这个范围就会丢失精度,那超过这个范围的基本运算就会出错了;
换一种思路,用字符串表示数字,如 "9007199254740999",也不存在四舍五入精度丢失
的问题,这样无论整数数值是多大都没任何的影响;
把整数数字表示字符串之后,那么整数数字
的运算就转换成字符串
的运算;
big number add
从最后一个数字开始向前依次相加,
逢10进1
,下一位数字相加的时候加上次计算的进位
;
const bigNumsAdd = (num1, num2) => {
// 获取最大长度
const len = Math.max(num1.length, num2.length);
// 对齐
num1 = num1.padStart(len, 0);
num2 = num2.padStart(len, 0);
// 进位
let carry = 0;
let result = ``;
let temp = 0;
for(let i=len-1; i>=0; i--){
temp = carry + parseInt(num1[i]) + parseInt(num2[i]);
// 字符串相加,要注意先后顺序
result = (temp % 10) + result;
// 取余进位
carry = parseInt(temp / 10);
}
// 判断是否进位
return result = (carry === 1 ? '1' : '') + result;
}
// test
const n1 = "9007199254740990"
const n2 = "1229007199254740993443"
bigNumsAdd(n1, n2);
// "1229016206453995734433"
// 不支持负数 ⚠️ ❌
const n1 = "-9007199254740990"
const n2 = "1229007199254740993443"
bigNumsAdd(n1, n2);
// 'NaNNaNNaNNaNNaNNaN6206453995734433'
// (算法性能优化: 取短的,减少循环次数)
solutions
function addStrings(num1: string, num2: string): string {
return (BigInt(num1) + BigInt(num2)).toString();
};
Runtime 52 ms Beats 96.39%
of users with TypeScript
Memory 51.12 MB Beats 97.94%
of users with TypeScript
leetcode
https://leetcode.com/problems/add-strings/
https://leetcode-cn.com/problems/add-strings/
/**
* @param {string} num1
* @param {string} num2
* @return {string}
*/
var addStrings = function(num1, num2) {
let res = '';
let temp = 0;
const arr1 = num1.split('');
const arr2 = num2.split('');
while (arr1.length || arr2.length || temp) {
// ~~ ??? bitwise not 双非位运算
temp += ~~arr1.pop() + ~~arr2.pop();
// 字符串相加,要注意先后顺序
res = (temp % 10) + res;
// 进位
temp = temp > 9;
}
// console.log('res =', res, typeof res);
return res === "0" ? res : res.replace(/^0+/, '');
}
(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!
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
MathML
Mathematical Markup Language (MathML) Version 3.0 2nd Edition
W3C Recommendation 10 April 2014
HTML var
tag
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/var
<table width="100%">
<tbody>
<tr>
<td id="eqnoc2"><var>E</var> = <var>m</var><var>c</var><sup>2</sup></td>
</tr>
</tbody>
</table>
E = mc2 |
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/kbd
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/code
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/samp
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/sup
<p><var>a<sup>2</sup></var> + <var>b<sup>2</sup></var> = <var>c<sup>2</sup></var></p>
a2 + b2 = c2
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/sub
H<sub>2</sub>O
H2O
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/wbr
MathJax
xml for math
https://mathjax.github.io/MathJax-demos-web/tex-chtml.html
refs
https://www.cnblogs.com/xgqfrms/p/18230786
©xgqfrms 2012-2020
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/12995736.html
未经授权禁止转载,违者必究!