xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

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 BigIntNumber.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]

 */

image

https://leetcode.com/problems/plus-one/description/

(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 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

image

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER

refs

https://stackoverflow.com/questions/51984393/why-i-cant-convert-string-to-number-without-losing-precision-in-js



©xgqfrms 2012-2021

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2024-06-04 15:03  xgqfrms  阅读(8)  评论(2编辑  收藏  举报