【leetcode】2.两数相加

我的解法

转化为十进制数

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        int a=0,b=0;

        int n = 1;
        while(l1 != null){
            a += l1.val*n;
            n *= 10;
            l1 = l1.next;
        }

        n=1;
        while(l2 != null){
            a += l2.val*n;
            n *= 10;
            l2 = l2.next;
        }

        int sum = a+b;
        System.out.println(sum);
        ListNode ans = new ListNode();
        
        ans.val = sum % 10;
        sum /= 10;
        ListNode pre = ans;

        while(sum != 0){
            ListNode p = new ListNode();
            p.val = sum % 10;
            pre.next = p;
            sum /= 10;
            pre = p;
        }

        return ans;
    }
}

第一次提交:
在这里插入图片描述
盲猜 int 不够


那就转成为 long

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        long a=0;

        int n = 1;
        while(l1 != null){
            a += (long)l1.val*n;
            n *= 10;
            l1 = l1.next;
        }

        n=1;
        long b = 0;
        while(l2 != null){
            b += (long)l2.val*n;
            n *= 10;
            l2 = l2.next;
        }

        long sum = a+b;
        ListNode ans = new ListNode();
        
        ans.val = (int)(sum % 10);
        sum /= 10;
        ListNode pre = ans;

        while(sum != 0){
            ListNode p = new ListNode();
            p.val = (int)(sum % 10);
            pre.next = p;
            sum /= 10;
            
            pre = p;
        }

        return ans;
    }
}

在这里插入图片描述
这结果,显然出题人不想让我用这种做法。

模拟加法器

灵感:链表合并

时间复杂度 O ( N 1 + N 2 ) O(N1+N2) O(N1+N2)

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        int carry = 0;//进位

        ListNode ans = new ListNode();
        ans.val = l1.val+l2.val;
        if(ans.val >= 10){
            carry = 1;
            ans.val %= 10;
        }
        ans.next = null;
        ListNode pre = ans;//结果表的游标
        l1 = l1.next;
        l2 = l2.next;

        while(l1 != null && l2 != null){
            ListNode p = new ListNode();
            p.val = l1.val + l2.val + carry;
            carry = 0;
            if(p.val >= 10){
                carry = 1;
                p.val %= 10;
            }
            p.next = null;

            pre.next = p;
            pre = p;

            l1 = l1.next;
            l2 = l2.next;
        }

        while(l1 != null){
            ListNode p = new ListNode();
            p.val = l1.val + carry;
            carry = 0;
            if(p.val >= 10){
                carry = 1;
                p.val %= 10;
            }
            pre.next = p;
            pre = p;

            l1 = l1.next;
        }

        while(l2 != null){
            ListNode p = new ListNode();
            p.val = l2.val + carry;
            carry = 0;
            if(p.val >= 10){
                carry = 1;
                p.val %= 10;
            }
            pre.next = p;
            pre = p;

            l2 = l2.next;
        }

        if(carry == 1){
            ListNode p = new ListNode();
            p.val = 1;
            p.next = null;
            pre.next = p; 
        }

        return ans;
    }
}

在这里插入图片描述

题解

解法一样,但代码逻辑比我简单

class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode head = null, tail = null;
        int carry = 0;
        while (l1 != null || l2 != null) {
        	//这两步,多一个判断,省略了长链合并的过程
            int n1 = l1 != null ? l1.val : 0;
            int n2 = l2 != null ? l2.val : 0;
            int sum = n1 + n2 + carry;
            if (head == null) {
                head = tail = new ListNode(sum % 10);
            } else {
                tail.next = new ListNode(sum % 10);
                tail = tail.next;
            }
            carry = sum / 10;
            if (l1 != null) {
                l1 = l1.next;
            }
            if (l2 != null) {
                l2 = l2.next;
            }
        }
        if (carry > 0) {
            tail.next = new ListNode(carry);
        }
        return head;
    }
}

作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/add-two-numbers/solution/liang-shu-xiang-jia-by-leetcode-solution/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

问题

  • 一个没见过的对象初始化,tail.next = new ListNode(sum % 10); ,两个不同类型属性,这样初始化时会进行自动类型匹配?
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AmosTian

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值