leetcode第五周解题总结(138, 23)

来源:互联网 发布:薛之谦的淘宝女装店 编辑:程序博客网 时间:2024/05/23 23:03

138. Copy List with Random Pointer

A linked list is given such that each node contains an additional
random pointer which could point to any node in the list or null.

Return a deep copy of the list.

题意解析:
拷贝一个带有随机指针的链表,难点就在于拷贝随机指针,要指向新链表的对应节点。

解题思路:
暴力解法,首先复制节点的值,新节点的random指针指向相应的旧节点,再两重循环保存每个节点的random指针,赋予新的链表。

/** * Definition for singly-linked list with a random pointer. * struct RandomListNode { *     int label; *     RandomListNode *next, *random; *     RandomListNode(int x) : label(x), next(NULL), random(NULL) {} * }; */class Solution {public:    RandomListNode *copyRandomList(RandomListNode *head) {        RandomListNode *result = new RandomListNode(-1);        RandomListNode *current =  head;        RandomListNode *new_current =  result;        vector<RandomListNode *> randomlist;        while(current != NULL) {            RandomListNode *temp = new RandomListNode(current->label);            temp->random = current;            new_current->next = temp;            current = current ->next;            new_current = new_current->next;        }        current =  result->next;        new_current =  result->next;        while(new_current != NULL) {            current =  result->next;            if(new_current->random->random == NULL) {                randomlist.push_back(NULL);                new_current = new_current->next;                continue;            }            while(current != NULL) {                if (current->random && new_current->random) {                    if(new_current->random->random == current->random) {                        randomlist.push_back(current);                        break;                    }                }                current = current ->next;            }            new_current = new_current->next;        }        new_current =  result->next;        int count = 0;        while(new_current != NULL) {            new_current->random = randomlist[count];            new_current = new_current->next;            count ++;        }        return result->next;    }};

23. Merge k Sorted Lists

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

题意解析:
合并k个排序链表

解题思路:
上周已经解答过合并两个排序链表的问题,那么可以利用分治的想法。
将一个集合拆分成为多个集合,每个集合内有两个链表,进行一次两两合并,得到k/2个链表,链表长度变为2n。
k/2*n + k/4*2n + k/8*4n + …… + 1*(2^logk)n
= nklogk

/** * Definition for singly-linked list. * struct ListNode { *     int val; *     ListNode *next; *     ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public:    ListNode* mergeKLists(vector<ListNode*>& lists) {        size_t len = lists.size();        if(len == 0) return NULL;        int start = 0;        int end = len - 1;        while (start < end){           lists[start] = mergeTwoLists(lists[start],lists[end]);           start++;           end--;           if(start >= end) start = 0;           if(end == 0) break;        }        return lists[0];    }    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {        if (l1 == NULL && l2 == NULL) return NULL;        ListNode* l = new ListNode(0);        ListNode* head = l;        while (l1 != NULL || l2 != NULL) {            if (l1 == NULL) {                head -> next = l2;                break;            }            if (l2 == NULL) {                head -> next = l1;                break;            }            if (l1->val < l2->val) {                head -> next = l1;                l1 = l1->next;            } else {                head -> next = l2;                l2 = l2->next;            }            head = head -> next;        }        return l->next;    }};
0 0
原创粉丝点击