【题解】LeetCode-两数相加(add-two-numbers)

2.两数相加
给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。

如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。

您可以假设除了数字 0 之外,这两个数都不会以 0 开头。

示例:

输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
输出:7 -> 0 -> 8
原因:342 + 465 = 807

思路一
看到这题,一般来说有过dfs经验的基本上应该可以反应为:先递归转成数字,再运算出结果,然后把结果转成链表。本着探(wu)索(liao)的精神,我尝试了这种方法,遗憾的是虽然没有超时,但int,long都会越界。这说明什么?说明人家数据还可以嘛~

解法一
比较暴力,没什么好说的。但是用BigInteger才过的,不推荐。

import java.math.BigInteger;
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        BigInteger ans = getVal(l1, BigInteger.ONE).add(getVal(l2, BigInteger.ONE));
        return buildList(ans);
    }
    
	// 表 -> 数字
    public BigInteger getVal(ListNode list, BigInteger loc) {
        if (null == list) {
            return BigInteger.ZERO;
        }
        return  loc.multiply(BigInteger.valueOf(list.val)).add(getVal(list.next, loc.multiply(BigInteger.TEN)));
    }
    
	// 数字 -> 表
    public ListNode buildList(BigInteger n) {
        if (BigInteger.ZERO.equals(n)) {
            return new ListNode(0);
        }
        BigInteger MOD = BigInteger.TEN;
        ListNode header = new ListNode(n.mod(MOD).intValue());
        ListNode list = header;
        n = n.divide(MOD);
        while (!BigInteger.ZERO.equals(n)) {
            list.next = new ListNode((int) (n.mod(MOD).intValue()));
            list = list.next;
            n = n.divide(MOD);
        }
        return header;
    }
}

思路二
可能是太久没刷题了~
仔细一看这不是大一大数相加类型的入门题嘛

解法二
解决大数问题是把数字当字符串处理,加减乘都是按位运算的,而且这里人家还给你倒序了,岂不是美滋滋~ 这题其实和归并排序的合并思路是一样的,只是比较取值变成了加法运算;
遍历链表,对应位相加再保留进位标志。需要注意的是,如果最后进位标志不是为0的话,还需要添加一个节点以保留进位。

PS:LeetCode就是写桩有点麻烦,还好保留上面解法一的代码做桩~

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        if (null == l1) {
            return l2;
        }
        if (null == l2) {
            return l1;
        }
        int inCome = 0, ans = 0;
        ListNode header = null, list = null;
        do{
            if (null == l1) {
                linkedNext(list, l2, inCome);
                return header;
            }
            if (null == l2) {
                linkedNext(list, l1, inCome);
                return header;
            }
            ans = inCome + l1.val + l2.val;
            inCome = ans / 10;
            if(null != list){
                list.next = new ListNode(ans % 10);
                list = list.next;
            }else{
                list = new ListNode(ans % 10);
                header = list;
            }
            l1 = l1.next;
            l2 = l2.next;
        }while(null != l1 || null != l2);
        if(0!=inCome){
            linkedNext(list, null, inCome);
        }
        return header;
    }

    private void linkedNext(ListNode list, ListNode next, int inCome) {
        if(null == next){
            if(0 != inCome) {
                list.next = new ListNode(inCome);
            }
            return;
        }
        ListNode temp = next;
        int ans;
        while (true) {
            ans = temp.val + inCome;
            temp.val = ans % 10;
            inCome = ans / 10;
            if(inCome == 0){
                // 不需要进位了
                break;
            }
            if(null == temp.next){
                // 需要加结点才能补足进位
                break;
            }
            temp = temp.next;
        }
        // 加结点补足进位
        if(inCome != 0){
            temp.next = new ListNode(inCome);
        }
        list.next = next;
    }
}

最后,
上面两个解法都过了,猜一猜谁更优?【!!!不要用看煞笔的眼神看笔者!!!】

重申一下,笔者并不是ACM选手,之所以写题解是为了缓解一下被业务轰炸了的大脑,如果你要找最优解,一般不要往这个博客跑,哈哈哈~~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值