[算法]大整數相加


1. 36進制大整數相加

兩個36進制的大整數以字符串的形式給出,求出兩個大整數的和,並以字符串方式輸出。(頭條面試題)

比如:12346 + GSFTYHS = GSGW1LY

public class Format36 {
    public static void main(String[] args) {
        System.out.println(sum("12346", "GSFTYHS"));
    }
    
    public static String sum(String num1, String num2){
        String ascii = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        if(num1.length() > num2.length()){
            StringBuilder sb = new StringBuilder(num2);
            for (int i = 0; i < num1.length() - num2.length(); i++) {
                sb.insert(0, '0');
            }
            num2 = sb.toString();
        }
        if(num1.length() < num2.length()){
            StringBuilder sb = new StringBuilder(num1);
            for (int i = 0; i < num2.length() - num1.length(); i++) {
                sb.insert(0, '0');
            }
            num1 = sb.toString();
        }
        
        int add = 0;//進位
        StringBuilder sb = new StringBuilder();
        for (int i = num1.length() - 1; i >= 0; i--) {
            int n1 = ascii.indexOf(num1.charAt(i));
            int n2 = ascii.indexOf(num2.charAt(i));
            int sum = n1 + n2 + add;
            System.out.println(sum);
            sb.insert(0, ascii.charAt(sum % 36));
            add = sum / 36;
        }
        if(add != 0)
            sb.insert(0, add);
        return sb.toString();
    }
}

2. 兩個單鏈表求和

鏡像:http://www.cnblogs.com/DarrenChan/p/5724502.html

給定兩個非空鏈表來代表兩個非負整數。數字最高位位於鏈表開始位置。它們的每個節點只存儲單個數字。將這兩數相加會返回一個新的鏈表。

你可以假設除了數字 0 之外,這兩個數字都不會以零開頭。

進階:

如果輸入鏈表不能修改該如何處理?換句話說,你不能對列表中的節點進行翻轉。

示例:

輸入: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
輸出: 7 -> 8 -> 0 -> 7

LeetCode:https://leetcode-cn.com/problems/add-two-numbers-ii/description/

思路:

用棧實現:

public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        Stack<ListNode> stack1 = new Stack<>();
        Stack<ListNode> stack2 = new Stack<>();

        while (l1 != null) {
            stack1.push(l1);
            l1 = l1.next;
        }

        while (l2 != null) {
            stack2.push(l2);
            l2 = l2.next;
        }

        int add = 0;// 進位
        int value1 = 0;
        int value2 = 0;
        ListNode ago = null;
        ListNode node = null;
        while (!stack1.isEmpty() || !stack2.isEmpty()) {
            if (!stack1.isEmpty()) {
                value1 = stack1.pop().val;
            } else {
                value1 = 0;
            }

            if (!stack2.isEmpty()) {
                value2 = stack2.pop().val;
            } else {
                value2 = 0;
            }

            int sum = value1 + value2 + add;
            node = new ListNode(sum % 10);
            add = sum / 10;

            node.next = ago;
            ago = node;
        }

        if (add > 0) {
            node = new ListNode(add);
            node.next = ago;
            ago = node;
        }

        return node;
    }

反轉單鏈表實現:

public ListNode addTwoNumbers2(ListNode l1, ListNode l2) {
        l1 = reverseList(l1);
        l2 = reverseList(l2);

        int add = 0;// 進位
        int value1 = 0;
        int value2 = 0;
        ListNode ago = null;
        ListNode node = null;
        while (l1 != null || l2 != null) {
            if (l1 != null) {
                value1 = l1.val;
                l1 = l1.next;
            } else {
                value1 = 0;
            }

            if (l2 != null) {
                value2 = l2.val;
                l2 = l2.next;
            } else {
                value2 = 0;
            }

            int sum = value1 + value2 + add;
            node = new ListNode(sum % 10);
            add = sum / 10;

            node.next = ago;
            ago = node;
        }

        if (add > 0) {
            node = new ListNode(add);
            node.next = ago;
            ago = node;
        }
        
        //把鏈表改回來
        l1 = reverseList(l1);
        l2 = reverseList(l2);

        return node;
    }
    
    public ListNode reverseList(ListNode head){
        ListNode pre = null;
        ListNode next = null;
        while(head != null){
            next = head.next;
            head.next = pre;
            pre = head;
            head = next;
        }
        return pre;
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM