算法(十八)

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

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

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

示例:

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

class ListNode{
//    ListNode next = null;
//    int val;
//    public ListNode(int x){
//        val = x;
//    }
//}
public static void main(String[] args) {
        int[] a = new int[]{9,9};
        int[] b = new int[]{1,2,3,4};
        int i = 0;
        ListNode head = new ListNode(0);
        ListNode p = head;
        while (i < a.length) {
            ListNode newNode = new ListNode(a[i]);
            p.next = newNode;
            p = p.next;
            i++;
        }
        i = 0;
        ListNode head2 = new ListNode(0);
        ListNode p2 = head2;
        while (i < b.length) {
            ListNode newNode = new ListNode(b[i]);
            p2.next = newNode;
            p2 = p2.next;
            i++;
        }

        ListNode p1 = head.next;
        System.out.print("鏈表a爲: ");
        while (p1 != null) {
            System.out.print(p1.val + " ");
            p1 = p1.next;
        }
        System.out.println();
        ListNode p22 = head2.next;
        System.out.print("鏈表b爲: ");
        while (p22 != null) {
            System.out.print(p22.val + " ");
            p22 = p22.next;
        }
        ListNode resNode = addTwoNumbers(head.next, head2.next);
        System.out.println();
        System.out.print("相加後鏈表爲: ");
        if(resNode != null){
            while (resNode != null) {
                System.out.print(resNode.val + " ");
                resNode = resNode.next;
            }
        }else{
            System.out.println("錯誤!");
        }
    }

    private static ListNode addTwoNumbers(ListNode head, ListNode head2) {
        ListNode newHead = new ListNode(0);
        ListNode p = head, q=head2, cur = newHead;
        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;
            cur.next = new ListNode(sum % 10);
            cur = cur.next;
            if(p!=null) p=p.next;
            if(q!=null) q=q.next;
        }
        if(carry>0){
            cur.next = new ListNode(carry);
        }
        return newHead.next;
    }

}

2、編寫一個函數來查找字符串數組中的最長公共前綴。如果不存在公共前綴,返回空字符串 ""

示例 1:

輸入: ["flower","flow","flight"]
輸出: "fl"
示例 2:

輸入: ["dog","racecar","car"]
輸出: ""
解釋: 輸入不存在公共前綴。

  • 水平掃描法

public String longestCommonPrefix(String[] strs) {
   if (strs.length == 0) return "";
   String prefix = strs[0];
   for (int i = 1; i < strs.length; i++)
       while (strs[i].indexOf(prefix) != 0) {
           prefix = prefix.substring(0, prefix.length() - 1);
           if (prefix.isEmpty()) return "";
       }        
   return prefix;
}

時間複雜度:O(S);S 是所有字符串中字符數量的總和。

空間複雜度:O(1)。我們只需要使用常數級別的額外空間。

  • 分治法
public static void main(String[] args) {
        String[] strs = new String[]{"fwower","flow","flight"};
        String res = longestCommonPrefix(strs);
        System.out.println(res);
    }

    public static String longestCommonPrefix(String[] strs) {
        if (strs == null || strs.length == 0) return "";
        return longestCommonPrefix(strs, 0 , strs.length - 1);
    }

    private static String longestCommonPrefix(String[] strs, int l, int r) {
        if (l == r) {
            return strs[l];
        }
        else {
            int mid = (l + r)/2;
            String lcpLeft =   longestCommonPrefix(strs, l , mid);
            String lcpRight =  longestCommonPrefix(strs, mid + 1,r);
            return commonPrefix(lcpLeft, lcpRight);
        }
    }

    static String commonPrefix(String left, String right) {
        int min = Math.min(left.length(), right.length());
        for (int i = 0; i < min; i++) {
            if ( left.charAt(i) != right.charAt(i) )
                return left.substring(0, i);
        }
        return left.substring(0, min);
    }

時間複雜度:O(S)O(S),SS 是所有字符串中字符數量的總和,S=m*n。

空間複雜度:O(m \cdot log(n))。

內存開支主要是遞歸過程中使用的棧空間所消耗的。 一共會進行 log(n)log(n) 次遞歸,每次需要 mm 的空間存儲返回結果,所以空間複雜度爲 O(m\cdot log(n))。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章