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
- leetcode第五周解题总结(138, 23)
- leetcode第六周解题总结(98)
- leetcode第七周解题总结
- leetcode第八周解题总结
- leetcode第十周解题总结
- leetcode第十七周解题总结
- leetcode第二周解题总结(3,8,65)
- leetcode第三周解题总结(5,7,66)
- leetcode第九周解题总结(Bit Manipulation位运算)
- leetcode第十三周解题总结--动态规划(二)
- leetcode第十五周解题总结--二分查找(二)
- leetcode第十二周解题总结--动态规划
- leetcode第十六周解题总结--图
- leetcode第十八周解题总结-贪心算法
- leetcode解题总结(持续更新)
- LeetCode解题的总结(不定期更新)
- LeetCode解题思路总结
- #个人赛第五场解题总结#
- 面试
- 异步任务神器 Celery 快速入门
- Tinker接入详解
- HDFS文件读写过程
- 关于 Token,你应该知道的十件事
- leetcode第五周解题总结(138, 23)
- Three.js进阶篇之4
- jsp之javaBean分页查询
- 每天学点Linux之cd、pwd命令
- 国内各类 WebShell 密码大全 爆破、社工用 webshell-password
- [转载]java笔试题:输出1-100之内的所有质数
- 打印星星六边形
- Android 登录处理
- 1047. 编程团体赛(20)