Sum of two numbers
Add two numbers in decimal
Add two binary numbers
Add two numbers in a linked list
Their thinking
If you’ve ever written addition of large numbers, you might write something like this
var addBinary = function(a, b) {
let maxLen = Math.max(a.length,b.length)
a = a.padStart(maxLen, '0')
b = b.padStart(maxLen, '0')
let flag = 0
let res = ' '
let i = maxLen - 1
while(i >= 0) {
flag = Number(a[i]) + Number(b[i]) + flag
res = flag % 10 + res;
flag = Math.floor(flag/10)
i--
}
res = flag == 1 ? '1' + res : res
return res
}
Copy the code
What about binary? Is that all right
code
/ * * *@param {string} a
* @param {string} b
* @return {string}* /
var addBinary = function(a, b) {
let maxLen = Math.max(a.length,b.length)
a = a.padStart(maxLen, '0')
b = b.padStart(maxLen, '0')
let flag = 0
let res = ' '
let i = maxLen - 1
while(i >= 0) {
flag = Number(a[i]) + Number(b[i]) + flag
res = flag % 2 + res;
flag = Math.floor(flag/2)
i--
}
res = flag == 1 ? '1' + res : res
return res
};
Copy the code
If you are careful, you can already see that there is a change in the mod and carry, but nothing else, so I have a general method for adding N bases
/ * * *@param {string} a
* @param {string} b
* @param {number} Radix radix number *@return {string}* /
var addBinary = function(a, b, radix) {
let maxLen = Math.max(a.length,b.length)
a = a.padStart(maxLen, '0')
b = b.padStart(maxLen, '0')
let flag = 0
let res = ' '
let i = maxLen - 1
while(i >= 0) {
flag = Number(a[i]) + Number(b[i]) + flag
res = flag % radix + res;
flag = Math.floor(flag/radix)
i--
}
res = flag == 1 ? '1' + res : res
return res
};
Copy the code
Another way to add strings
The most important thing is these three steps
flag = x + y + flag
res = flag % 10 + res;
flag = Math.floor(flag/10)
Copy the code
The complete code is as follows:
/ * * *@param {string} num1
* @param {string} num2
* @return {string}* /
var addStrings = function(a, b) {
let l1 = a.length - 1
let l2 = b.length - 1
let flag = 0
let res = ' '
while(l1 >= 0 || l2 >= 0) {
let x = l1 >=0 ? + a[l1] : 0
let y = l2 >=0 ? + b[l2] : 0
flag = x + y + flag
res = flag % 10 + res;
flag = Math.floor(flag/10)
l1--
l2--
}
res = flag == 1 ? '1' + res : res
return res
};
Copy the code
Add two numbers in a linked list
Linked list of two numbers added, the solution is similar to the above, there are similarities and similarities
/** * Definition for singly-linked list. * function ListNode(val, next) { * this.val = (val===undefined ? 0 : val) * this.next = (next===undefined ? null : next) * } */
/ * * *@param {ListNode} l1
* @param {ListNode} l2
* @return {ListNode}* /
var addTwoNumbers = function(l1, l2) {
let dummy = new ListNode()
let cur = dummy;
let flag = 0
while(l1 || l2) {
let n1 = l1 && l1.val || 0
let n2 = l2 && l2.val || 0
let total = n1 + n2 + flag
cur.next = new ListNode(total % 10)
cur = cur.next
l1 = l1 && l1.next
l2 = l2 && l2.next
flag = Math.floor(total / 10)}if (flag) {
cur.next = new ListNode(flag)
}
return dummy.next
};
Copy the code