-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode2130.java
More file actions
52 lines (45 loc) · 1.49 KB
/
LeetCode2130.java
File metadata and controls
52 lines (45 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import util.ListNode;
public class LeetCode2130 {
public static void main(String[] args) {
// 输入:head = [5,4,2,1]
// 输出:6
System.out.println(new Solution2130().pairSum(ListNode.buildLinkedList(new Integer[] { 5, 4, 2, 1 })));
// 输入:head = [4,2,2,3]
// 输出:7
System.out.println(new Solution2130().pairSum(ListNode.buildLinkedList(new Integer[] { 4, 2, 2, 3 })));
// 输入:head = [1,100000]
// 输出:100001
System.out.println(new Solution2130().pairSum(ListNode.buildLinkedList(new Integer[] { 1, 100000 })));
}
}
/**
* 先用快慢指针找到中间点,再用数组存储中间结果
*/
class Solution2130 {
public int pairSum(ListNode head) {
ListNode dummyNode = new ListNode();
dummyNode.next = head;
int halfLen = 0;
ListNode fast = dummyNode;
ListNode slow = dummyNode;
while (fast.next != null && fast.next.next != null) {
fast = fast.next.next;
slow = slow.next;
halfLen++;
}
int[] sum = new int[halfLen];
ListNode p1 = dummyNode;
ListNode p2 = slow;
for (int i = 0; i < halfLen; i++) {
p1 = p1.next;
p2 = p2.next;
sum[i] += p1.val;
sum[halfLen - 1 - i] += p2.val;
}
int ans = sum[0];
for (int i = 1; i < halfLen; i++) {
ans = Math.max(sum[i], ans);
}
return ans;
}
}