leetcode-n2-兩數相加

leetcode-兩數相加

下面的類,能夠重用,修改一下能夠運行在在線IDE,給出了測試main()網絡

題目源地址測試

提交記錄指針

/*
 給出兩個 非空 的鏈表用來表示兩個非負的整數。其中,它們各自的位數是按照 逆序 的方式存儲的,而且它們的每一個節點只能存儲 一位 數字。

 若是,咱們將這兩個數相加起來,則會返回一個新的鏈表來表示它們的和。

 您能夠假設除了數字 0 以外,這兩個數都不會以 0 開頭。

 示例:

 輸入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
 輸出:7 -> 0 -> 8
 緣由:342 + 465 = 807

 來源:力扣(LeetCode)
 連接:https://leetcode-cn.com/problems/add-two-numbers
 著做權歸領釦網絡全部。商業轉載請聯繫官方受權,非商業轉載請註明出處。
 */

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */

class ListNode {
    int val;

    ListNode next;

    ListNode(int x) {
        val = x;
    }
}

class Solution {

    public static void main(String[] args) {
        Solution solution = new Solution();

        //基本測試
        ListNode res = solution.addTwoNumbers(solution.long2ListNode(342), solution.long2ListNode(465));
        sout(res);

        //特殊:單鏈表
        res = solution.addTwoNumbers(solution.long2ListNode(619), solution.long2ListNode(0));
        sout(res);
        //特殊:邊界進位
        res = solution.addTwoNumbers(solution.long2ListNode(5), solution.long2ListNode(5));
        sout(res);

        res = solution.addTwoNumbers(solution.long2ListNode(1), solution.long2ListNode(99));
        sout(res);
    }

    private static void sout(ListNode l3) {
        while (l3 != null) {
            System.out.println(l3.val);
            l3 = l3.next;
        }
        System.out.println("---------");
    }

    /**
     * 說明: 使用進位
     * @author suwenguang
     * @date 2019/6/10
     * @return n2.ListNode <- 返回類型
     */
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        //參數校驗
        if (l1 == null || l2 == null) {
            return null;
        }
        if (l1.val < 0 || l2.val < 0) {
            return null;
        }
        //主邏輯
        //進位鏈表
        ListNode carrier = new ListNode(0);
        //結果鏈表
        ListNode res = new ListNode(0);
        //臨時鏈表
        ListNode temp = res;

        while (l1 != null && l2 != null) {
            //相加進位
            int val = l1.val + l2.val + carrier.val;
            if (val >= 10) {
                carrier.next = new ListNode(1);
            } else {
                carrier.next = new ListNode(0);
            }
            //賦值res
            int remain = val % 10;
            temp.next = new ListNode(remain);

            //指針移動
            temp = temp.next;
            l1 = l1.next;
            l2 = l2.next;
            carrier = carrier.next;
        }
        while (l1 != null) {
            int val = carrier.val + l1.val;
            if (val >= 10) {
                carrier.next = new ListNode(1);
            } else {
                carrier.next = new ListNode(0);
            }

            //賦值
            int remain = val % 10;
            temp.next = new ListNode(remain);

            //指針移動
            l1 = l1.next;
            carrier = carrier.next;
            temp = temp.next;

        }
        while (l2 != null) {
            int val = carrier.val + l2.val;
            if (val >= 10) {
                carrier.next = new ListNode(1);
            } else {
                carrier.next = new ListNode(0);
            }

            //賦值
            int remain = val % 10;
            temp.next = new ListNode(remain);

            //指針移動
            l2 = l2.next;
            carrier = carrier.next;
            temp = temp.next;

        }

        //斷定是否還有進位
        if (carrier.val == 1) {
            temp.next = new ListNode(1);
        }

        return res.next;
    }

    /**
     * 說明:暴力解法
     * @author suwenguang
     * @date 2019/6/10
     * @return n2.ListNode <- 返回類型
     */
    @Deprecated
    public ListNode addTwoNumbersFail(ListNode l1, ListNode l2) {
        //參數校驗
        if (l1 == null || l2 == null) {
            return null;
        }
        if (l1.val < 0 || l2.val < 0) {
            return null;
        }
        //將鏈表轉爲整數
        long a = listNode2Long(l1);
        long b = listNode2Long(l2);

        //相加
        long res = a + b;

        //結果轉鏈表返回
        ListNode head = long2ListNode(res);

        return head.next;
    }

    /**
     * 說明:  ListNode 轉 long
     * @author suwenguang
     * @date 2019/6/10
     * @return long <- 返回類型
     */
    private long listNode2Long(ListNode l2) {
        int i = 1;
        long res = 0;
        ListNode tempNode;
        tempNode = l2;
        while (tempNode != null) {
            res = res + (tempNode.val * i);
            tempNode = tempNode.next;
            i *= 10;
        }
        return res;
    }

    /**
     * 說明: long 轉 ListNode
     * @author suwenguang
     * @date 2019/6/10
     * @return n2.ListNode <- 返回類型
     */
    private ListNode long2ListNode(long res) {
        ListNode tempNode;
        long temp = 11;

        ListNode head = new ListNode(-1);
        tempNode = head;

        while (res / 10 > 0) {
            temp = res % 10;
            ListNode next = new ListNode((int) temp);
            tempNode.next = next;
            tempNode = next;
            res /= 10;
        }
        ListNode end = new ListNode((int) res);
        tempNode.next = end;
        return head.next;
    }
}

2019-06-10 19:37:14 星期一code

相關文章
相關標籤/搜索