Skip to content

Commit 0b43489

Browse files
committed
fd
1 parent d6bf5e7 commit 0b43489

File tree

2 files changed

+68
-14
lines changed

2 files changed

+68
-14
lines changed

leetcode/solution/src/MergeKSortedList.java

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
public class MergeKSortedList {
99

1010
// 耗时19ms
11-
// 时间复杂度为O(knlgn)
11+
// 时间复杂度为O(knlgn),空间复杂度O(k)
1212
/**
1313
* 这里要注意lists中可能有node为null
1414
*/
@@ -39,4 +39,43 @@ public int compare(ListNode node1, ListNode node2) {
3939

4040
return dummy.next;
4141
}
42+
43+
/**
44+
* 第二种方法
45+
* 时间复杂度O(Nlgk),空间O(l)
46+
* 耗时10ms
47+
*/
48+
public ListNode mergeKLists2(ListNode[] lists) {
49+
return helper(lists, 0, lists.length - 1);
50+
}
51+
52+
private ListNode helper(ListNode[] lists, int start, int end) {
53+
if (start > end) {
54+
return null;
55+
}
56+
if (start == end) {
57+
return lists[start];
58+
}
59+
int mid = start + (end - start) / 2;
60+
ListNode l1 = helper(lists, start, mid);
61+
ListNode l2 = helper(lists, mid + 1, end);
62+
return mergeTwoLists(l1, l2);
63+
}
64+
65+
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
66+
ListNode dummy = new ListNode(0);
67+
ListNode p = l1, q = l2, cur = dummy;
68+
for ( ; p != null && q != null; ) {
69+
if (p.val < q.val) {
70+
cur.next = p;
71+
p = p.next;
72+
} else {
73+
cur.next = q;
74+
q = q.next;
75+
}
76+
cur = cur.next;
77+
}
78+
cur.next = p != null ? p : q;
79+
return dummy.next;
80+
}
4281
}

leetcode/src/Main.java

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,38 @@
22

33
public class Main {
44

5-
public int maxSubArray(int[] nums) {
6-
int[] dp = new int[nums.length];
7-
int max = Integer.MIN_VALUE;
8-
for (int i = 0; i < nums.length; i++) {
9-
dp[i] = Math.max(i > 0 ? dp[i - 1] + nums[i] : nums[i], nums[i]);
10-
max = Math.max(max, dp[i]);
5+
public ListNode mergeKLists(ListNode[] lists) {
6+
return helper(lists, 0, lists.length - 1);
7+
}
8+
9+
private ListNode helper(ListNode[] lists, int start, int end) {
10+
if (start > end) {
11+
return null;
12+
}
13+
if (start == end) {
14+
return lists[start];
1115
}
12-
return max;
16+
int mid = start + (end - start) / 2;
17+
ListNode l1 = helper(lists, start, mid);
18+
ListNode l2 = helper(lists, mid + 1, end);
19+
return mergeTwoLists(l1, l2);
1320
}
1421

15-
public int maxSubArray2(int[] nums) {
16-
int max = Integer.MIN_VALUE, prev = 0;
17-
for (int i = 0; i < nums.length; i++) {
18-
prev = Math.max(i > 0 ? prev + nums[i] : nums[i], nums[i]);
19-
max = Math.max(max, prev);
22+
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
23+
ListNode dummy = new ListNode(0);
24+
ListNode p = l1, q = l2, cur = dummy;
25+
for ( ; p != null && q != null; ) {
26+
if (p.val < q.val) {
27+
cur.next = p;
28+
p = p.next;
29+
} else {
30+
cur.next = q;
31+
q = q.next;
32+
}
33+
cur = cur.next;
2034
}
21-
return max;
35+
cur.next = p != null ? p : q;
36+
return dummy.next;
2237
}
2338

2439
public static void main(String[] args) {

0 commit comments

Comments
 (0)