JS斩杀LeetCode(2):Add Two Numbers

栏目: JavaScript · 发布时间: 6年前

内容简介:JS斩杀LeetCode(2):Add Two Numbers

题目:

You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

示例:

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8

原题链接:

https://leetcode.com/problems/add-two-numbers/#/description

简单讲一讲题目是什么意思。数字被逆序拆分保存在链表的各个节点中,现有两条链表,要将这两条链表相加返回新的链表。

以示例来说原数字为342和465,所以相加结果为807。

解题关键:① 怎么将每个节点连接组成一条新链表;② 考虑两节点相加时的进位。

解法①

/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */
/**
 * @param {ListNode} l1
 * @param {ListNode} l2
 * @return {ListNode}
 */
var addTwoNumbers = function(l1, l2) {
    var result_list = null;
    var cur_node = null;
    var cur_carry = 0;
    var v1 = 0;
    var v2 = 0;
    
    var checkSum = function(sum) {
        if (parseInt(sum/10) > 0) {
            cur_carry = parseInt(sum/10);
            sum = sum % 10;
        } else {
            cur_carry = 0;
        }
        return sum;
    };
    
    var getValAndMoveToNext = function (list1, list2) {
        v1 = list1 ? list1.val: 0;
        v2 = list2 ? list2.val: 0;
        l1 = list1 ? list1.next: null;
        l2 = list2 ? list2.next: null;
    };
    
    if (l1 || l2) {
        getValAndMoveToNext(l1, l2);
        result_list = new ListNode(checkSum(v1+v2));
        cur_node = result_list;
        while (l1 || l2) {
            getValAndMoveToNext(l1, l2);
            var sum = v1 + v2 + cur_carry;
            cur_node.next = new ListNode(checkSum(sum));
            cur_node = cur_node.next;
        }
        if (cur_carry !== 0) {
            cur_node.next = new ListNode(cur_carry);
        }
    }
    return result_list;
};

时间复杂度为O(n),通过checkSum方法计算出当前节点的数值,以及进位情况:若sum除以10,商不为0,则需要考虑进位情况,当前节点的数值则除10取余。

result_list为最后返回的链表,cur_node的为当前所处的节点,通过以下代码将节点连成链表:

cur_node.next = new ListNode(checkSum(sum));
cur_node = cur_node.next;

解法②

/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */
/**
 * @param {ListNode} l1
 * @param {ListNode} l2
 * @return {ListNode}
 */
var addOperation = function(l1, l2, carry) {
    if (!l1 && !l2 && !carry) {
        return null;
    }
    l1 = l1 || new ListNode(0);
    l2 = l2 || new ListNode(0);
    carry = carry || 0;
    let l3 = new ListNode((l1.val + l2.val + carry)%10);
    carry = parseInt((l1.val + l2.val + carry)/10);
    if (l1.next || l2.next || carry) {
        l3.next = addOperation(l1.next, l2.next, carry);    
    }
    return l3;
}
 
var addTwoNumbers = function(l1, l2) {
    return addOperation(l1, l2);
};
递归的解法,时间复杂度为O(n),和解法①的效率差不多,但代码更简洁一些。

以上所述就是小编给大家介绍的《JS斩杀LeetCode(2):Add Two Numbers》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Elements of Information Theory

Elements of Information Theory

Thomas M. Cover、Joy A. Thomas / Wiley-Blackwell / 2006-7 / GBP 76.50

The latest edition of this classic is updated with new problem sets and material The Second Edition of this fundamental textbook maintains the book's tradition of clear, thought-provoking instr......一起来看看 《Elements of Information Theory》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码