Topic description
Given a non-negative integer represented by the 整数
非空
array of ---c6891fcefd64989411662cc3c97ada66---, add one to the number.
The highest digit is stored in the first position of the array, and each element in the array stores only a single number.
You can assume that this integer will not start with zero except for the integer 0.
Example 1:
输入:digits = [1,2,3]
输出:[1,2,4]
解释:输入数组表示数字 123。
Example 2:
输入:digits = [4,3,2,1]
输出:[4,3,2,2]
解释:输入数组表示数字 4321。
Example 3:
输入:digits = [0]
输出:[1]
Leetcode original title address: https://leetcode.cn/problems/plus-one
Using Bigint to solve the loss of precision of large numbers
When Benzong saw this question, black question marks came to his mind, what? Such a simple question, no way! No way! (Bai Yansong doubts) Isn't this:
- Convert the array to a string and then convert it to a number, then add one (because numbers can only add one)
- Then turn the number back and turn it into an array
As a result, Ben Zong resorted to flying fingerings and tapped on the keyboard of Ben Zong's secret treasure and ancient immortal artifact mechanical tea shaft. In an instant, a strange sound shot into the sky, and terrifying pressure swept the earth. Some of the programming ape beasts with poor strength around, a mouthful of blood spurted out from the seven orifices, the body flew out, and plowed out a ravine dozens of meters long and more than half a meter deep on the ground, and finally slammed into the company's utility room. The walls were filled with smoke and dust. The smoke dissipated, and the walls were covered with cracks like cobwebs. Three seconds later, the cracked wall that was struggling to support finally collapsed, burying some of the programmer beasts who were like gossamers under the ruins.
Benzong couldn't help but be overjoyed, Feitian fingering, so terrifying, fairy keyboard, so powerful!
When Benzong's mind moved, in a matter of seconds, the mysterious and mysterious code appeared out of thin air:
var plusOne = function (digits) {
digits = digits.join('') * 1 // 数组转字符串再转数字
digits = digits + 1 // 数字加一
return (digits + '').split('') // 再把数字转成字符串并分割成数组即可
};
Benzong looked proudly at the mysterious code he created, stroked his beard, and clicked submit calmly. He thought it would pass successfully, but the next picture made Benzong take a deep breath:
Failed test case!
Benzong frowned and found that this question is not so simple, because Benzong suddenly remembered a "Xuantian js ancient scripture" circulated in the ancient Douma Continent. There is such an obscure and incomprehensible truth in the book. :
js中数字类型无法表示大数,即超过16位数字,会出现精度丢失问题
Sure enough!
The use case of the error report is to reach a horrible 19 digits, far exceeding 16 digits, so there is a loss of precision, so the answer is incorrect.
It seems that the person who asked the question should be the legendary bug powerhouse! This sect has been traversing this bucket-size continent for many years, but I didn't expect that the car would overturn in the gutter today. Wouldn't it make the younger generation laugh and be generous?
No, this sect has a heart, and he thinks in his heart: This problem cannot be left!
Suddenly, an old voice came out from the void: I am an old code freak, and I feel that I have a good chance with you. Today, I will give you a copy of the js practice Dafa "M Dien"!
Write more about the great kindness of the seniors, and this sect reluctantly accepts it.
I don't know when, there is a link on my computer screen: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/BigInt
Don't care so much, this person is not malicious...
So with the help of js practice Dafa "M Dien", Benzong solved this problem
var plusOne = function (digits) {
let number = BigInt(digits.join('')); // 使用BigInt防止精度丢失
number = number + BigInt(1) // BigInt与BigInt同类型相加
let res = (number + '').split('') // 再转回数组即可
return res
};
Compared with the wrong approach above, it is to use the
Bigint
object to solve the problem of large numbers with more than 16 bits. Therefore, when you encounter problems with large numbers, you can consider using the bigint object to control, so that the precision will not be lost. The knowledge points of closing bigint will not be repeated here. You can take a look at the MDN connection above.It's actually quite boring to brush the questions, so I wrote an article "Code Cultivation" for everyone to have fun
^_^
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。