JS add two numbers

Time:2022-5-27

Overview of adding two numbers

Add two decimal numbers
Binary addition
Adding two numbers in linked list

Problem solving ideas

JS add two numbers

If you write too many numbers, add them(String addition), you might write that

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
}

What if you change it to 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
};

Careful friends have seen the clue, that is, they have made changes in remainder and carry, and the others have not moved, so I have summarized a general method of n-ary addition

/**
 * @param {string} a
 * @param {string} b
 *@ param {number} 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
};

Another kind of string addition

/**
 * @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
};

Adding two numbers in linked list

/**
 * 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
};

Recommended Today

Vue2 technology finishing 3 – Advanced chapter – update completed

3. Advanced chapter preface Links to basic chapters:https://www.cnblogs.com/xiegongzi/p/15782921.html Link to component development:https://www.cnblogs.com/xiegongzi/p/15823605.html 3.1. Custom events of components 3.1.1. Binding custom events There are two implementation methods here: one is to use v-on with vuecomponent$ Emit implementation [PS: this method is a little similar to passing from child to parent]; The other is to use ref […]