LeetCode23 Merge k Sorted Lists 把K個有序鏈表串連成一個,leetcode23sorted

來源:互聯網
上載者:User

LeetCode23 Merge k Sorted Lists 把K個有序鏈表串連成一個,leetcode23sorted

題目:

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.

翻譯:

把K個有序的鏈表合成一個,返回。

思路:

這道題和昨天的Merge 2 Lists有些類似。但是k不確定,如果用每個都去遍曆的話,肯定是不會通過的。

So、可以想到的是歸併方法。

還記得大一剛開始組長布置的歸併作業,那時候剛入門,看的雲裡霧裡。

正好藉此機會把歸併好好的複習一下。

先說一下這道題的解法:

因為是鏈表List,可以通過分治,把他分解成2個一組,然後對每組的2個進行Merge 2Lists。

接著在4個一組,因為上一步已經形成了2個鏈,所以此時也可通過Merge 2Lists進行,遞迴即可得到結果。

代碼:

     public ListNode mergeKLists(ListNode[] lists) {  if(lists == null || lists.length == 0)  return null;  return MergeSort(lists, 0, lists.length-1);   }  public static ListNode MergeSort(ListNode[] lists,int l ,int r)  {  if(l>=r)  return lists[l];  int m = (l+r)/2;  ListNode ll = MergeSort(lists, l, m);  ListNode rr = MergeSort(lists, m+1, r);  return Merge(ll,rr);      }  public static ListNode Merge(ListNode l1,ListNode l2)  {  ListNode root =new ListNode(0);  ListNode pre = root;  pre.next = l1;  while(true)  {  if(l1 == null)  {  pre.next = l2;  break;  }  if(l2 == null)  {  pre.next = l1;  break;  }  if(l1.val < l2.val)  {  pre.next = l1;  l1 = l1.next;  }  else  {  pre.next = l2;  l2 = l2.next;   }  pre = pre.next;    }  return root.next;    }

好了,這道題的代碼就是上面。


這裡,還是要把歸併寫一寫。因為畢竟以後要用到。

歸併排序wiki解釋

個人的理解就是把一個長的分解成小部分,即分治,分成2個一組,把每組排序後,在進行4個一組,排序。

最後得到整體的有序排列。

百度一張圖片 ,估計對理解有協助


Wiki的解釋:

歸併排序具體工作原理如下(假設序列共有n個元素):

  1. 將序列每相鄰兩個數字進行歸併操作,形成個序列,排序後每個序列包含兩個元素
  2. 將上述序列再次歸併,形成個序列,每個序列包含四個元素
  3. 重複步驟2,直到所有元素排序完畢
於是乎,自己寫一個歸併排序吧。

public class MergeSort {<span style="white-space:pre"></span><span style="white-space:pre"></span>static int number=0;<span style="white-space:pre"></span>public static void MergeSort(int arr[],int l , int r)<span style="white-space:pre"></span>{<span style="white-space:pre"></span>if(l >= r)<span style="white-space:pre"></span>return ;<span style="white-space:pre"></span>int mid = (l+r)/2;<span style="white-space:pre"></span>MergeSort(arr, l, mid);//分成組<span style="white-space:pre"></span>MergeSort(arr, mid+1, r);<span style="white-space:pre"></span>Merge(arr, l, mid, r);//組內排序<span style="white-space:pre"></span>}<span style="white-space:pre"></span>public static void Merge(int arr[],int l,int mid, int r)<span style="white-space:pre"></span>{<span style="white-space:pre"></span>int []tep = new int[arr.length];<span style="white-space:pre"></span>int mm = mid+1;<span style="white-space:pre"></span>int count = l,ll = l;<span style="white-space:pre"></span>while(l <= mid && mm <= r)<span style="white-space:pre"></span>{<span style="white-space:pre"></span>if(arr[l]  <= arr[mm])<span style="white-space:pre"></span>tep[count++]  = arr[l++];<span style="white-space:pre"></span>else<span style="white-space:pre"></span>tep[count++] = arr[mm++];<span style="white-space:pre"></span>}<span style="white-space:pre"></span>while(l<= mid)<span style="white-space:pre"></span>{<span style="white-space:pre"></span>tep[count++] = arr[l++];<span style="white-space:pre"></span>}<span style="white-space:pre"></span>while(mm <=r)<span style="white-space:pre"></span>{<span style="white-space:pre"></span>tep[count++] = arr[mm++];<span style="white-space:pre"></span>}<span style="white-space:pre"></span> System.out.println("第"+(++number)+"趟\t");<span style="white-space:pre"></span>while(ll<=r)<span style="white-space:pre"></span>{<span style="white-space:pre"></span>    arr[ll] = tep[ll];<span style="white-space:pre"></span>    System.out.print(arr[ll] + "\t");<span style="white-space:pre"></span>    ll++;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>    System.out.println();<span style="white-space:pre"></span><span style="white-space:pre"></span>}<span style="white-space:pre"></span>/**<span style="white-space:pre"></span> * @param args<span style="white-space:pre"></span> */<span style="white-space:pre"></span>public static void main(String[] args) {<span style="white-space:pre"></span>// TODO 自動產生的方法存根<span style="white-space:pre"></span>int[] a = {9,20,32,12,53,23,54,2};<span style="white-space:pre"></span>    MergeSort(a, 0, a.length-1);<span style="white-space:pre"></span>for(int t:a)<span style="white-space:pre"></span>{<span style="white-space:pre"></span>System.out.println(t);<span style="white-space:pre"></span>}<span style="white-space:pre"></span>}}
輸出結果:



輸出中可以清楚的看出分組後排序的效果。

話說提交後看到一群人用C寫才十幾毫秒就通過了。果然屌!

等會研究研究他們的寫法。然後在修改。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.