modify code

master
algorithmzuo 4 years ago
parent fdab79726d
commit d6570a1f53

@ -29,7 +29,7 @@ public class Code02_Heap {
throw new RuntimeException("heap is full"); throw new RuntimeException("heap is full");
} }
heap[heapSize] = value; heap[heapSize] = value;
// value heapSize // value heapSize
heapInsert(heap, heapSize++); heapInsert(heap, heapSize++);
} }
@ -42,15 +42,10 @@ public class Code02_Heap {
return ans; return ans;
} }
// 新加进来的数现在停在了index位置请依次往上移动 // 新加进来的数现在停在了index位置请依次往上移动
// 移动到0位置或者干不掉自己的父亲了 // 移动到0位置或者干不掉自己的父亲了
private void heapInsert(int[] arr, int index) { private void heapInsert(int[] arr, int index) {
// [index] [index-1]/2 // [index] [index-1]/2
// index == 0 // index == 0
while (arr[index] > arr[(index - 1) / 2]) { while (arr[index] > arr[(index - 1) / 2]) {
swap(arr, index, (index - 1) / 2); swap(arr, index, (index - 1) / 2);
@ -124,24 +119,24 @@ public class Code02_Heap {
} }
public static class MyComparator implements Comparator<Integer> {
public static class MyComparator implements Comparator<Integer>{
@Override @Override
public int compare(Integer o1, Integer o2) { public int compare(Integer o1, Integer o2) {
return o2 - o1; return o2 - o1;
} }
} }
public static void main(String[] args) { public static void main(String[] args) {
// 小根堆 // 小根堆
PriorityQueue<Integer> heap = new PriorityQueue<>(new MyComparator()); PriorityQueue<Integer> heap = new PriorityQueue<>(new MyComparator());
heap.add(5); heap.add(5);
heap.add(5); heap.add(5);
heap.add(5); heap.add(5);
heap.add(3); heap.add(3);
// 5 , 3 // 5 , 3
System.out.println(heap.peek()); System.out.println(heap.peek());
heap.add(7); heap.add(7);
heap.add(0); heap.add(0);
@ -150,16 +145,10 @@ public class Code02_Heap {
heap.add(7); heap.add(7);
heap.add(0); heap.add(0);
System.out.println(heap.peek()); System.out.println(heap.peek());
while(!heap.isEmpty()) { while (!heap.isEmpty()) {
System.out.println(heap.poll()); System.out.println(heap.poll());
} }
int value = 1000; int value = 1000;
int limit = 100; int limit = 100;
int testTimes = 1000000; int testTimes = 1000000;

Loading…
Cancel
Save