題目描述
合併 k 個排序鏈表,返回合併後的排序鏈表。請分析和描述算法的複雜度。
示例:
輸入:
[
1->4->5,
1->3->4,
2->6
]
輸出: 1->1->2->3->4->4->5->6
一 思路一 最笨方法
- 將所有節點添加到一個數組中
- 對數組中的節點從小到大進行排序
- 從數組中從小到大依次取出節點,串成鏈表
圖解
作爲一個開發者,有一個學習的氛圍跟一個交流圈子特別重要,這是一個我的iOS交流羣:413038000,不管你是大牛還是小白都歡迎入駐 ,分享BAT,阿里面試題、面試經驗,討論技術, 大家一起交流學習成長!
以下資料在羣文件可自行下載!
- 核心代碼如下
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// 構造三個鏈表
LinkNode *k1 = [[LinkNode alloc] initWithPrev:nil element:@(1) next:nil];
LinkNode *k2 = [[LinkNode alloc] initWithPrev:k1 element:@(4) next:nil];
LinkNode *k3 = [[LinkNode alloc] initWithPrev:k2 element:@(7) next:nil];
LinkNode *k4 = [[LinkNode alloc] initWithPrev:nil element:@(2) next:nil];
LinkNode *k5 = [[LinkNode alloc] initWithPrev:k4 element:@(5) next:nil];
LinkNode *k6 = [[LinkNode alloc] initWithPrev:k5 element:@(8) next:nil];
LinkNode *k7 = [[LinkNode alloc] initWithPrev:nil element:@(3) next:nil];
LinkNode *k8 = [[LinkNode alloc] initWithPrev:k7 element:@(6) next:nil];
LinkNode *k9 = [[LinkNode alloc] initWithPrev:k8 element:@(9) next:nil];
// 方法一
NSArray *linkNodes = @[k1,k4,k7];
LinkNode *k = [self mergeManyLists:linkNodes];
while (k) {
NSLog(@"%@",[k description]);
k = k.next;
}
}
/// 方法一:合併N個有序鏈表
- (LinkNode *)mergeManyLists:(NSArray *)linkNodes {
if (!linkNodes || linkNodes.count == 0) {
return nil;
}
NSMutableArray *nodes = [NSMutableArray array];
for (LinkNode *node in linkNodes) {
__strong LinkNode *strongNode = node;
while (strongNode != nil) {
[nodes addObject:strongNode];
strongNode = strongNode.next;
}
}
// 排序
NSArray *newNodes = [nodes sortedArrayUsingComparator:^NSComparisonResult(LinkNode *obj1, LinkNode *obj2) {
if (obj1.value > obj2.value) {
return NSOrderedDescending;
}
return NSOrderedAscending;
}];
LinkNode *head = [[LinkNode alloc] init];
LinkNode *cur = head;
for (LinkNode *node in newNodes) {
cur = cur.next = node;
}
return head.next;
}
- 運行結果
2019-11-12 23:12:02.674864+0800 02_MergeTwoLists[30509:1059927] (null)_1_2
2019-11-12 23:12:02.675147+0800 02_MergeTwoLists[30509:1059927] 1_2_3
2019-11-12 23:12:02.675315+0800 02_MergeTwoLists[30509:1059927] 2_3_4
2019-11-12 23:12:02.675583+0800 02_MergeTwoLists[30509:1059927] 3_4_5
2019-11-12 23:12:02.675706+0800 02_MergeTwoLists[30509:1059927] 4_5_6
2019-11-12 23:12:02.675810+0800 02_MergeTwoLists[30509:1059927] 5_6_7
2019-11-12 23:12:02.675941+0800 02_MergeTwoLists[30509:1059927] 6_7_8
2019-11-12 23:12:02.676053+0800 02_MergeTwoLists[30509:1059927] 7_8_9
2019-11-12 23:12:02.676159+0800 02_MergeTwoLists[30509:1059927] 8_9_null
時間複雜度:O(nlogn)
空間複雜度:O(n)
二 思路二 逐一比較
- 核心代碼如下
/// 方法二 逐一比較
- (LinkNode *)mergeManyLists2:(NSMutableArray<LinkNode *> *)linkNodes {
if (linkNodes == nil || linkNodes.count == 0) {
return nil;
}
LinkNode *head = [[LinkNode alloc] init];
LinkNode *cur = head;
while (true) {
int minIndex = -1;
for (int i = 0; i < linkNodes.count; i++) {
if (linkNodes[i] == nil || linkNodes[i].element == nil) {
continue;
}
if (minIndex == -1 || linkNodes[i].value < linkNodes[minIndex].value) {
minIndex = i;
}
}
if (minIndex == -1) {
break;
}
cur = cur.next = linkNodes[minIndex];
linkNodes[minIndex] = linkNodes[minIndex].next;
}
return head.next;
}
- 測試代碼
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
LinkNode *nilNode = [[LinkNode alloc] initWithPrev:nil element:nil next:nil];
// 構造三個鏈表
LinkNode *k1 = [[LinkNode alloc] initWithPrev:nil element:@(1) next:nil];
LinkNode *k2 = [[LinkNode alloc] initWithPrev:k1 element:@(4) next:nil];
LinkNode *k3 = [[LinkNode alloc] initWithPrev:k2 element:@(7) next:nil];
LinkNode *k4 = [[LinkNode alloc] initWithPrev:nil element:@(2) next:nil];
LinkNode *k5 = [[LinkNode alloc] initWithPrev:k4 element:@(5) next:nil];
LinkNode *k6 = [[LinkNode alloc] initWithPrev:k5 element:@(8) next:nil];
LinkNode *k7 = [[LinkNode alloc] initWithPrev:nil element:@(3) next:nil];
LinkNode *k8 = [[LinkNode alloc] initWithPrev:k7 element:@(6) next:nil];
LinkNode *k9 = [[LinkNode alloc] initWithPrev:k8 element:@(9) next:nil];
// 爲了需要,每個節點最後是一個空的節點
k3.next = nilNode;
k6.next = nilNode;
k9.next = nilNode;
// 方法二 逐一比較
NSMutableArray *linkNodes = [NSMutableArray arrayWithArray:@[k1,k4,k7]];
LinkNode *k = [self mergeManyLists2:linkNodes];
while (k) {
NSLog(@"%@",[k description]);
k = k.next;
}
}
- 運行結果如下
2019-11-17 10:43:34.914883+0800 02_MergeTwoLists[40077:1519139] (null)_1_2
2019-11-17 10:43:34.915081+0800 02_MergeTwoLists[40077:1519139] 1_2_3
2019-11-17 10:43:34.915177+0800 02_MergeTwoLists[40077:1519139] 2_3_4
2019-11-17 10:43:34.915274+0800 02_MergeTwoLists[40077:1519139] 3_4_5
2019-11-17 10:43:34.915491+0800 02_MergeTwoLists[40077:1519139] 4_5_6
2019-11-17 10:43:34.915623+0800 02_MergeTwoLists[40077:1519139] 5_6_7
2019-11-17 10:43:34.915744+0800 02_MergeTwoLists[40077:1519139] 6_7_8
2019-11-17 10:43:34.915865+0800 02_MergeTwoLists[40077:1519139] 7_8_9
2019-11-17 10:43:34.915981+0800 02_MergeTwoLists[40077:1519139] 8_9_(null)
2019-11-17 10:43:34.916102+0800 02_MergeTwoLists[40077:1519139] 9_(null)_null
三 思路三-逐一兩兩合併
- 核心代碼
/// 方法三 逐一兩兩合併
- (LinkNode *)mergeManyLists3:(NSMutableArray<LinkNode *> *)linkNodes {
if (linkNodes == nil || linkNodes.count == 0) {
return nil;
}
for (int i = 1; i < linkNodes.count; i++) {
linkNodes[0] = [self mergeTwoLists:linkNodes[0] k2:linkNodes[i]];
}
return linkNodes[0];
}
/// 方法一:遞歸
/// 1.只要是用到遞歸,首先要搞清楚一個問題,這個遞歸函數的功能是什麼
/// 2.遞歸基:邊界
- (LinkNode *)mergeTwoLists:(LinkNode *)k1 k2:(LinkNode *)k2 {
if (k1 == nil) return k2;
if (k2 == nil) return k1;
if (k1.value <= k2.value) {
k1.next = [self mergeTwoLists:k1.next k2:k2];
return k1;
} else {
k2.next = [self mergeTwoLists:k1 k2:k2.next];
return k2;
}
}
- 測試代碼
/// 方法三 逐一兩兩合併
NSMutableArray *linkNodes = [NSMutableArray arrayWithArray:@[k1,k4,k7]];
LinkNode *k = [self mergeManyLists3:linkNodes];
while (k) {
NSLog(@"%@",[k description]);
k = k.next;
}
- 運行結果
2019-11-17 11:19:14.687574+0800 02_MergeTwoLists[40935:1548483] null_1_2
2019-11-17 11:19:14.687798+0800 02_MergeTwoLists[40935:1548483] 1_2_3
2019-11-17 11:19:14.687948+0800 02_MergeTwoLists[40935:1548483] 2_3_4
2019-11-17 11:19:14.688098+0800 02_MergeTwoLists[40935:1548483] 3_4_5
2019-11-17 11:19:14.688245+0800 02_MergeTwoLists[40935:1548483] 4_5_6
2019-11-17 11:19:14.688401+0800 02_MergeTwoLists[40935:1548483] 5_6_7
2019-11-17 11:19:14.688515+0800 02_MergeTwoLists[40935:1548483] 6_7_8
2019-11-17 11:19:14.688620+0800 02_MergeTwoLists[40935:1548483] 7_8_9
2019-11-17 11:19:14.688727+0800 02_MergeTwoLists[40935:1548483] 8_9_null
四 思路四-優先級隊列(小頂堆)
五 思路五-分治策略
- 核心代碼
/// 方法五 - 分治策略
- (LinkNode *)mergeManyLists5:(NSMutableArray<LinkNode *> *)linkNodes {
if (linkNodes == nil || linkNodes.count == 0) {
return nil;
}
int step = 1;
while (step < linkNodes.count) {
int nextStep = step << 1;
for (int i = 0; i + step < linkNodes.count; i += nextStep) {
linkNodes[i] = [self mergeTwoLists:linkNodes[i] k2:linkNodes[i + step]];
}
step = nextStep;
}
return linkNodes[0];
}
- 運行結果
2019-11-17 21:57:36.968482+0800 02_MergeTwoLists[54576:1953555] null_1_2
2019-11-17 21:57:36.968663+0800 02_MergeTwoLists[54576:1953555] 1_2_3
2019-11-17 21:57:36.968778+0800 02_MergeTwoLists[54576:1953555] 2_3_4
2019-11-17 21:57:36.968890+0800 02_MergeTwoLists[54576:1953555] 3_4_5
2019-11-17 21:57:36.969008+0800 02_MergeTwoLists[54576:1953555] 4_5_6
2019-11-17 21:57:36.969148+0800 02_MergeTwoLists[54576:1953555] 5_6_7
2019-11-17 21:57:36.969267+0800 02_MergeTwoLists[54576:1953555] 6_7_8
2019-11-17 21:57:36.969376+0800 02_MergeTwoLists[54576:1953555] 7_8_9
2019-11-17 21:57:36.969485+0800 02_MergeTwoLists[54576:1953555] 8_9_null
作爲一個開發者,有一個學習的氛圍跟一個交流圈子特別重要,這是一個我的iOS交流羣:413038000,不管你是大牛還是小白都歡迎入駐 ,分享BAT,阿里面試題、面試經驗,討論技術, 大家一起交流學習成長!
推薦閱讀
iOS開發——最新 BAT面試題合集(持續更新中)
作者:路飛_Luck
鏈接:https://www.jianshu.com/p/43ecb9d85461
來源:簡書