zoukankan      html  css  js  c++  java
  • *Merge k Sorted Lists

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

    leetcode上返回类型不太一样,改编一下第三个解法:4ms

    public class Solution {
        public ListNode mergeKLists(ListNode[] lists) 
        {
                if (lists == null || lists.length == 0) {
                return null;
            }
            
            ListNode[] newlists = new ListNode[0];
            while(lists.length>1)
            {
               if(lists.length%2==0)
               {
                   newlists = new ListNode[lists.length/2];
                   for(int i=0;i<lists.length-1;i += 2)
                   {
                        ListNode node = mergeTwoLists(lists[i],lists[i+1]);
                        newlists[i/2] = node;
                   }
               }
               else
               {
                   newlists = new ListNode[lists.length/2+1];
                   for(int i=0;i<lists.length-1;i += 2)
                   {
                        ListNode node = mergeTwoLists(lists[i],lists[i+1]);
                        newlists[i/2] = node;
                   }
                   newlists[newlists.length-1] = lists[lists.length-1];
               }
               lists = newlists;
                
            }
            return lists[0];
            
        }
        
        
        public ListNode mergeTwoLists(ListNode l1, ListNode l2) 
        {
            ListNode dummyhead = new ListNode(0);
            ListNode head = dummyhead;
            while(l1!=null&&l2!=null)
            {
                if(l1.val<l2.val)
                {
                    head.next = l1;
                    l1 = l1.next;
                }
                else
                {
                    head.next = l2;
                    l2 = l2.next;
                }
                head = head.next;
            }
            
            if(l1!=null) head.next = l1;
            if(l2!=null) head.next = l2;
            return dummyhead.next;
            
        }
    }

    解法二:用heap, 12ms, 时间复杂度O(nlog(k))

    Suppose the total number of nodes is n The total time complexity if (n * log k) .For a priority queue, insertion takes logK time

    /** 
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    public class Solution {
        private Comparator<ListNode> ListNodeComparator = new Comparator<ListNode>() {
            public int compare(ListNode left, ListNode right) {
                if (left == null) {
                    return 1;
                } else if (right == null) {
                    return -1;
                }
                return left.val - right.val;
            }
        };
        
        public ListNode mergeKLists(ListNode[] lists) 
        {
            if(lists==null||lists.length==0)return null;
            Queue<ListNode> heap = new PriorityQueue<ListNode>(lists.length,ListNodeComparator);
            for(int i=0;i<lists.length;i++)
            {
                if(lists[i]!=null)
                {
                    heap.add(lists[i]);
                }
            }
            ListNode dummy = new ListNode(0);
            ListNode tail = dummy;
            while(!heap.isEmpty())
            {
                ListNode head = heap.poll();
                tail.next = head;
                tail = head;
                if (head.next != null) {
                    heap.add(head.next);
                }
            }
            return dummy.next;
        }
    }
  • 相关阅读:
    Nginx 反向代理、负载均衡、页面缓存、URL重写及读写分离详解
    大型网站系统架构技术原理透析
    大中型网站架构探秘
    大型网站架构不得不考虑的10个问题
    (推荐)高并发高流量网站架构详解
    可扩展、高可用、负载均衡网站架构设计方案
    nginx内置常用变量
    Linux下nginx支持.htaccess文件实现伪静态的方法!
    扩展js,实现c#中的string.format方便拼接字符串
    Winform退出运行后,删除运行目录(批处理方法)
  • 原文地址:https://www.cnblogs.com/hygeia/p/5062768.html
Copyright © 2011-2022 走看看