国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

23. Merge k Sorted Lists

qingshanli1988 / 2398人閱讀

摘要:題目合并排序鏈表并返回一個(gè)排序列表。分析和描述它的復(fù)雜性。直接對個(gè)鏈表合并,找到個(gè)鏈表頭最小的,將值追加到放在新創(chuàng)建的鏈表中,并把頭移到下一個(gè)節(jié)點(diǎn),直到所有的鏈表頭運(yùn)行后發(fā)現(xiàn)超時(shí)嘗試兩兩合并兩個(gè)鏈表,知道最終合并成一個(gè)運(yùn)行通過

題目

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
合并k排序鏈表并返回一個(gè)排序列表。分析和描述它的復(fù)雜性。

直接對k個(gè)鏈表合并,找到k個(gè)鏈表頭最小的,將值追加到放在新創(chuàng)建的鏈表中,并把頭移到下一個(gè)節(jié)點(diǎn),直到所有的鏈表頭none

# Definition for singly-linked list.
class ListNode(object):
    def __init__(self, x):
        self.val = x
        self.next = None

class Solution(object):
    def mergeKLists(self, lists):
        """
        :type lists: List[ListNode]
        :rtype: ListNode
        """
        head = None
        walk_list = [lists[i] for i in range(len(lists))]
        pre = None
        while len(filter(lambda x: x is not None, walk_list)):
            for i in range(len(walk_list)):
                if walk_list[i] is not None:
                    min_val = walk_list[i].val
                    min_index = i
                    break
            for i in range(len(walk_list)):
                if walk_list[i] and walk_list[i].val < min_val:
                    min_val = walk_list[i].val
                    min_index = i
            l = ListNode(min_val)
            walk_list[min_index] = walk_list[min_index].next
            if head is None:
                head = l
                pre = head
            else:
                pre.next = l
                pre = l
        return head

運(yùn)行后發(fā)現(xiàn)超時(shí)

嘗試兩兩合并兩個(gè)鏈表,知道最終合并成一個(gè)

class Solution(object):
    def mergeKLists(self, lists):
        """
        :type lists: List[ListNode]
        :rtype: ListNode
        """

        if not lists:
            return None
        i = 0
        j = len(lists) - 1
        r_list = lists
        while i < j:
            l = []
            while i < j:
                node = self.mergetwolists(r_list[i], r_list[j])
                l.append(node)
                i += 1
                j -= 1
            if i == j:
                l.append(r_list[i])
            r_list = l
            i = 0
            j = len(r_list) - 1
        return r_list[0]

    def mergetwolists(self, l1, l2):
        if l1 == None:
            return l2
        if l2 == None:
            return l1
        l1_head = l1
        l2_head = l2
        head = None
        pre = None
        while l1_head and l2_head:
            if l1_head.val < l2_head.val:
                l = ListNode(l1_head.val)
                l1_head = l1_head.next
            else:
                l = ListNode(l2_head.val)
                l2_head = l2_head.next

            if pre == None:
                pre = l
                head = l
            else:
                pre.next = l
                pre = l

            if l1_head is None:
                pre.next = l2_head
            if l2_head is None:
                pre.next = l1_head
        return head

運(yùn)行通過

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://m.specialneedsforspecialkids.com/yun/40724.html

相關(guān)文章

  • [LeetCode] 23. Merge k Sorted Lists

    摘要:思路這題中的中,個(gè)還有其中個(gè)別的等于的情況,所以要判斷一下再加入代碼 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. Heap Time Complexity: Update the heap costs O(nklogk) Space ...

    Codeing_ls 評論0 收藏0
  • 355. Design Twitter , 用23. Merge k Sorted Lists和OO

    摘要:的想法就是用每次得到最小的鏈表頭的值,輸出。每個(gè)都有一個(gè)關(guān)注人列表,用儲存。用戶可以發(fā)消息,關(guān)注別人,取消關(guān)注別人。可以系統(tǒng)得到關(guān)注人的消息合集,這個(gè)方法必須在這個(gè)層級。因?yàn)橛脩糁恢雷约旱男畔ⅰ? Merge k Sorted Lists /** * Definition for singly-linked list. * public class ListNode { * ...

    1fe1se 評論0 收藏0
  • LeetCode 之 JavaScript 解答第23題 —— 合并K個(gè)有序鏈表(Merge K S

    摘要:分治算法遞歸每層操作分解將原問題分解成一系列的子問題。分治算法滿足的條件可分解原問題與分解成的小問題具有相同的模式無關(guān)聯(lián)原問題分解成的子問題可以獨(dú)立求解,子問題之間沒有相關(guān)性,這一點(diǎn)是分治算法跟動態(tài)規(guī)劃的明顯區(qū)別。 Time:2019/4/10Title: Merge K Sorted ListsDifficulty: DifficultyAuthor: 小鹿 題目:Merge K...

    zhou_you 評論0 收藏0
  • [Leetcode] Merge Two Sorted Lists Merge K Sorted L

    摘要:注意因?yàn)槎阎惺擎湵砉?jié)點(diǎn),我們在初始化堆時(shí)還要新建一個(gè)的類。代碼初始化大小為的堆拿出堆頂元素將堆頂元素的下一個(gè)加入堆中 Merge Two Sorted Lists 最新更新請見:https://yanjia.me/zh/2019/01/... Merge two sorted linked lists and return it as a new list. The new list...

    stefanieliang 評論0 收藏0
  • [LintCode] Merge K Sorted Lists [DC/Heap]

    摘要:分治做法中,函數(shù)依然是將鏈表結(jié)點(diǎn)兩兩進(jìn)行比較,然后在函數(shù)中迭代兩個(gè)二分后的結(jié)果。 Problem Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. Example Given lists: [ 2->4->null, null, ...

    happyhuangjinjin 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<