modify code

master
algorithmzuo 2 years ago
parent fe69f9aacd
commit cf1077f6f9

@ -5,11 +5,32 @@ import java.util.PriorityQueue;
public class Code01_CoverMax {
public static void main(String[] args) {
// 堆
// log N
PriorityQueue<Integer> heap = new PriorityQueue<>((a, b) -> a - b);
heap.add(8);
heap.add(2);
heap.add(3);
heap.add(9);
heap.add(4);
heap.add(4);
while (!heap.isEmpty()) {
System.out.println(heap.poll());
}
}
public static int maxCover(int[][] m) {
// m = [ [4, 7], [3, 6], [1, 5] , [2, 9] ]
// 开始位置排序!
// m = [ [1, 5], [2, 9], [3, 6] , [4, 7] ]
Arrays.sort(m, (a, b) -> (a[0] - b[0]));
// 小根堆!建立!
PriorityQueue<Integer> heap = new PriorityQueue<>();
int max = 0;
for (int[] line : m) {
// line : line[0] line[1]
// <= line[0]
while (!heap.isEmpty() && heap.peek() <= line[0]) {
heap.poll();
}

@ -5,12 +5,15 @@ import java.util.PriorityQueue;
public class Code02_LessMoneySplitGold {
public static int lessMoney(int[] arr) {
// 小根堆!
PriorityQueue<Integer> pQ = new PriorityQueue<>();
for (int i = 0; i < arr.length; i++) {
pQ.add(arr[i]);
// 所有数字扔到小根堆里
for (int num : arr) {
pQ.add(num);
}
int sum = 0;
int cur = 0;
// 小根堆只剩一个数字的时候停!
while (pQ.size() > 1) {
cur = pQ.poll() + pQ.poll();
sum += cur;

@ -21,7 +21,9 @@ public class Code05_AddTwoNumbers {
public static ListNode addTwoNumbers(ListNode head1, ListNode head2) {
int len1 = listLength(head1);
int len2 = listLength(head2);
// 长链表 找到l
ListNode l = len1 >= len2 ? head1 : head2;
// 短链表 找到s
ListNode s = l == head1 ? head2 : head1;
ListNode curL = l;
ListNode curS = s;
@ -36,6 +38,7 @@ public class Code05_AddTwoNumbers {
curL = curL.next;
curS = curS.next;
}
// 短链表没了
while (curL != null) {
curNum = curL.val + carry;
curL.val = (curNum % 10);
@ -43,6 +46,7 @@ public class Code05_AddTwoNumbers {
last = curL;
curL = curL.next;
}
// 长链表没了
if (carry != 0) {
last.next = new ListNode(1);
}

@ -45,8 +45,8 @@ public class Code07_CopyListWithRandom {
Node cur = head;
Node next = null;
// 1 -> 2 -> 3 -> null
// 1 -> 1' -> 2 -> 2' -> 3 -> 3'
while (cur != null) {
// 1 -> 1' -> 2(next) -> ...
next = cur.next;
cur.next = new Node(cur.val);
cur.next.next = next;
@ -54,11 +54,15 @@ public class Code07_CopyListWithRandom {
}
cur = head;
Node copy = null;
// 1 1' 2 2' 3 3'
// 1 1' 2 2' 3 3' null
// 依次设置 1' 2' 3' random指针
while (cur != null) {
// 1 -> 1' -> 2(next)
next = cur.next.next;
// cur 1
// copy 1'
copy = cur.next;
// 1'.random =
copy.random = cur.random != null ? cur.random.next : null;
cur = next;
}
@ -67,9 +71,14 @@ public class Code07_CopyListWithRandom {
// 老 新 混在一起next方向上random正确
// next方向上把新老链表分离
while (cur != null) {
// 1 -> 1' -> 2(next) -> 2'
next = cur.next.next;
// cur 1
// copy 1'
copy = cur.next;
// 1 -> 2
cur.next = next;
// 1' -next->
copy.next = next != null ? next.next : null;
cur = next;
}

@ -1,59 +0,0 @@
package 03.mca_02;
// 测试链接https://leetcode.com/problems/reverse-nodes-in-k-group/
public class Code08_ReverseNodesInKGroup {
// 不要提交这个类
public static class ListNode {
public int val;
public ListNode next;
}
public static ListNode reverseKGroup(ListNode head, int k) {
ListNode start = head;
ListNode end = getKGroupEnd(start, k);
if (end == null) {
return head;
}
// 第一组凑齐了!
head = end;
reverse(start, end);
// 上一组的结尾节点
ListNode lastEnd = start;
while (lastEnd.next != null) {
start = lastEnd.next;
end = getKGroupEnd(start, k);
if (end == null) {
return head;
}
reverse(start, end);
lastEnd.next = end;
lastEnd = start;
}
return head;
}
public static ListNode getKGroupEnd(ListNode start, int k) {
while (--k != 0 && start != null) {
start = start.next;
}
return start;
}
public static void reverse(ListNode start, ListNode end) {
end = end.next;
ListNode pre = null;
ListNode cur = start;
ListNode next = null;
while (cur != end) {
next = cur.next;
cur.next = pre;
pre = cur;
cur = next;
}
start.next = end;
}
}

@ -1,52 +0,0 @@
package 03.mca_02;
import java.util.Comparator;
import java.util.PriorityQueue;
// 测试链接https://leetcode.com/problems/merge-k-sorted-lists/
public class Code09_MergeKSortedLists {
public static class ListNode {
public int val;
public ListNode next;
}
public static class ListNodeComparator implements Comparator<ListNode> {
@Override
public int compare(ListNode o1, ListNode o2) {
return o1.val - o2.val;
}
}
public static ListNode mergeKLists(ListNode[] lists) {
if (lists == null) {
return null;
}
PriorityQueue<ListNode> heap = new PriorityQueue<>(new ListNodeComparator());
for (int i = 0; i < lists.length; i++) {
if (lists[i] != null) {
heap.add(lists[i]);
}
}
if (heap.isEmpty()) {
return null;
}
ListNode head = heap.poll();
ListNode pre = head;
if (pre.next != null) {
heap.add(pre.next);
}
while (!heap.isEmpty()) {
ListNode cur = heap.poll();
pre.next = cur;
pre = cur;
if (cur.next != null) {
heap.add(cur.next);
}
}
return head;
}
}

@ -208,7 +208,7 @@ public class Code01_ReverseList {
DoubleNode node4 = generateRandomDoubleList(len, value);
List<Integer> list4 = getDoubleListOriginOrder(node4);
node4 = reverseDoubleList(node4);
node4 = testReverseDoubleList(node4);
if (!checkDoubleListReverse(list4, node4)) {
System.out.println("Oops4!");
}

Loading…
Cancel
Save