diff --git a/src/main/java/com/never/basic/PriorityQueueDemo.java b/src/main/java/com/never/basic/PriorityQueueDemo.java new file mode 100644 index 0000000..95c0a17 --- /dev/null +++ b/src/main/java/com/never/basic/PriorityQueueDemo.java @@ -0,0 +1,109 @@ +package com.never.basic; + +import lombok.AllArgsConstructor; +import lombok.Data; + +import java.util.Comparator; +import java.util.PriorityQueue; + +/** + * @Description: + * @author: Bo Li + * @date: 2022年08月30日 10:46 + */ +public class PriorityQueueDemo { + public static void main(String[] args) { + PriorityQueue queue = new PriorityQueue<>(new ValComparator()); + int[] arr1 = {1,3,5,8}; + int[] arr2 = {2,4,5,7}; + int[] arr3 = {3,4,9}; + Node head1 = array2NodeList(arr1); + Node head2 = array2NodeList(arr2); + Node head3 = array2NodeList(arr3); + queue.add(head1); + queue.add(head2); + queue.add(head3); + Node currentNode = queue.poll(); + Node head = currentNode; + if(currentNode.next != null ){ + queue.add(currentNode.next); + } + while(!queue.isEmpty()){ + Node node = queue.poll(); + if(node.next != null){ + queue.add(node.next); + } + currentNode.next = node; + currentNode = node; + } + show(head); + } + + + + /** + * @Description: 单向链表遍历 + * @param head + * @return + */ + private static void show(Node head){ + while(head.next != null){ + System.out.print(head.val+"-->"); + head = head.next; + } + } + + /** + * @Description: 有序数组转单向链表 + * @param arr + * @return + */ + private static Node array2NodeList(int[] arr){ + Node head = null; + Node currentNode = null; + Node preNode = null; + if(arr.length ==0 || arr == null){ + return head; + } + + if(arr.length ==1){ + head.val = arr[0]; + head.next = null; + return head; + }else{ + + for(int i = arr.length-1 ; i >= 0 ; i--){ + if(i == arr.length -1){ + currentNode = new Node(arr[i],null); + preNode = currentNode; + } + preNode =currentNode; + currentNode = new Node(arr[i],preNode); + } + head = currentNode; + } + return head; + } + + @Data + @AllArgsConstructor + private static class Node{ + private int val; + private Node next; + } + /*** + * @Description: 基于Node的比较器 + * @Param: + * @return: + * @Author: libospinach + * @Date: 2022/8/30 + */ + private static class ValComparator implements Comparator{ + @Override + public int compare(Node o1, Node o2) { + return o1.val-o2.val; + } + } +} + + diff --git a/src/main/java/com/never/basic/zeroOneGenerate.java b/src/main/java/com/never/basic/zeroOneGenerate.java index f687c8c..4caebed 100644 --- a/src/main/java/com/never/basic/zeroOneGenerate.java +++ b/src/main/java/com/never/basic/zeroOneGenerate.java @@ -1,12 +1,13 @@ package com.never.basic; +import java.util.PriorityQueue; + /** * @Description: TODO * @author: Bo Li * @date: 2022年08月29日 9:13 */ public class zeroOneGenerate { - public static void main(String[] args) { int[] arr = {0,0}; for(int i = 0; i < 100000; i++){ diff --git a/target/classes/com/never/basic/BitOperation.class b/target/classes/com/never/basic/BitOperation.class index 9e75683..e281167 100644 Binary files a/target/classes/com/never/basic/BitOperation.class and b/target/classes/com/never/basic/BitOperation.class differ diff --git a/target/classes/com/never/basic/BubbleSort.class b/target/classes/com/never/basic/BubbleSort.class index b537128..e624e17 100644 Binary files a/target/classes/com/never/basic/BubbleSort.class and b/target/classes/com/never/basic/BubbleSort.class differ diff --git a/target/classes/com/never/basic/CommonUtil.class b/target/classes/com/never/basic/CommonUtil.class index 15993b3..cdf407a 100644 Binary files a/target/classes/com/never/basic/CommonUtil.class and b/target/classes/com/never/basic/CommonUtil.class differ diff --git a/target/classes/com/never/basic/NRecursion.class b/target/classes/com/never/basic/NRecursion.class index ec9fb4e..ffeb105 100644 Binary files a/target/classes/com/never/basic/NRecursion.class and b/target/classes/com/never/basic/NRecursion.class differ diff --git a/target/classes/com/never/basic/SelectSort.class b/target/classes/com/never/basic/SelectSort.class index facf0ba..e8eb646 100644 Binary files a/target/classes/com/never/basic/SelectSort.class and b/target/classes/com/never/basic/SelectSort.class differ diff --git a/target/classes/com/never/basic/SortDemo.class b/target/classes/com/never/basic/SortDemo.class index 4e4d68a..33b091d 100644 Binary files a/target/classes/com/never/basic/SortDemo.class and b/target/classes/com/never/basic/SortDemo.class differ