关于前端:js两数相加

44次阅读

共计 1873 个字符,预计需要花费 5 分钟才能阅读完成。

两数相加详情

十进制两数相加
二进制两数相加
链表式两数相加

解题思路

如果写过大数相加(字符串相加),你可能会这么写

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
}

那如果换成二进制呢?是不是这样就好

代码

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

仔细的小伙伴曾经看到端倪了,就是在取余和进位那里做了扭转,其余的没动,所以我总结出一个 N 进制相加的通用办法

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

另外一种字符串相加

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

链表式两数相加

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

正文完
 0