(一百零一)LeetCode 1 两数相加
发布日期:2021-06-30 15:25:25 浏览次数:2 分类:技术文章

本文共 4544 字,大约阅读时间需要 15 分钟。

1.题目

 

2.解答

/** * 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) {        ListNode lhead = new ListNode(0);        ListNode literator = lhead;        lhead.val = l1.val + l2.val;        while(l1.next!=null && l2.next!=null){            literator.next = new ListNode(l1.next.val + l2.next.val);            literator = literator.next;            l1 = l1.next;            l2 = l2.next;        }        if (l1.next == null && l2.next != null){            literator.next = l2.next;        }else if (l1.next != null && l2.next == null){            literator.next = l1.next;        }        return lhead;            }}

 

3.验证失败

没考虑十进一

 

4.修改

/** * 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) {        ListNode lhead = new ListNode(0);        ListNode literator = lhead;        lhead.val = l1.val + l2.val;        if (lhead.val >= 10){            lhead.val -= 10;            literator.next = new ListNode(1);        }        while(l1.next!=null && l2.next!=null){            literator.next = (literator.next == null) ?                 new ListNode(l1.next.val + l2.next.val) :new ListNode (literator.next.val + l1.next.val + l2.next.val) ;            literator = literator.next;            if(literator.val >= 10){                literator.val -= 10;                literator.next = new ListNode(1);            }            l1 = l1.next;            l2 = l2.next;        }        if (l1.next == null && l2.next != null){            if(literator.next != null){                l2.next.val++;                     }            literator.next = l2.next;            while (l2.next.val >= 10){                l2.next.val -= 10;                l2 = l2.next;                if(l2.next == null){                    l2.next = new ListNode(1);                }else{                    l2.next.val++;                }            }        }else if (l1.next != null && l2.next == null){            if(literator.next != null){                l1.next.val++;            }            literator.next = l1.next;            while (l1.next.val >= 10){                l1.next.val -= 10;                l1 = l1.next;                if(l1.next == null){                    l1.next = new ListNode(1);                }else{                    l1.next.val++;                }            }        }        return lhead;            }}

有冗余代码,遇十进一的代码写了四次,可以抽取出来。

/** * 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) {        ListNode lhead = new ListNode(0);        ListNode literator = lhead;        lhead.val = l1.val + l2.val;        while(l1.next!=null && l2.next!=null){            literator.next = new ListNode(l1.next.val + l2.next.val);            literator = literator.next;            l1 = l1.next;            l2 = l2.next;        }        if (l1.next == null && l2.next != null){            literator.next = l2.next;        }else if (l1.next != null && l2.next == null){            literator.next = l1.next;        }        tenToOne(lhead);        return lhead;            }        private void tenToOne(ListNode l){        ListNode literator = l;        while(literator != null){            if(literator.val >=10){                literator.val -= 10;                if(literator.next == null){                    literator.next = new ListNode(1);                    break;                }else{                    literator.next.val++;                }            }            literator = literator.next;        }        return;    }}

 

5.验证提交

优化前:

优化后

但是这个时间不准确,再次执行表明我的代码还是很low

因为我的优化只是代码量的优化,算法还是一样的。

 

参考答案:

public ListNode addTwoNumbers(ListNode l1, ListNode l2) {    ListNode dummyHead = new ListNode(0);    ListNode p = l1, q = l2, curr = dummyHead;    int carry = 0;    while (p != null || q != null) {        int x = (p != null) ? p.val : 0;        int y = (q != null) ? q.val : 0;        int sum = carry + x + y;        carry = sum / 10;        curr.next = new ListNode(sum % 10);        curr = curr.next;        if (p != null) p = p.next;        if (q != null) q = q.next;    }    if (carry > 0) {        curr.next = new ListNode(carry);    }    return dummyHead.next;}

 

转载地址:https://jiatai.blog.csdn.net/article/details/83211395 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:(一百零二)Android O wpa_supplicant初始化学习
下一篇:(一百)体验LeetCode

发表评论

最新留言

很好
[***.229.124.182]2024年04月11日 17时08分52秒