From d6f22d3f0417818e6972cce14a7272b9005c1512 Mon Sep 17 00:00:00 2001
From: msb_16686 <636909e3>
Date: Thu, 17 Nov 2022 08:45:40 +0800
Subject: [PATCH] =?UTF-8?q?=E7=AE=97=E6=B3=95=EF=BC=9A=E6=89=8B=E5=86=99?=
=?UTF-8?q?=E5=A0=86?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
Algorithm/pom.xml | 4 +
.../src/main/java/algorithm/C02_Stack.java | 45 +++++++
.../src/main/java/algorithm/C21_Node.java | 62 +++++++++
Algorithm/src/main/java/common/Node.java | 10 ++
Algorithm/src/main/java/common/Student.java | 41 ++++++
.../src/main/java/heap/C01_Comparator.java | 47 +++++++
Algorithm/src/main/java/heap/C02_heap.java | 7 ++
Algorithm/src/main/java/heap/HeapGreater.java | 119 ++++++++++++++++++
8 files changed, 335 insertions(+)
create mode 100644 Algorithm/src/main/java/algorithm/C02_Stack.java
create mode 100644 Algorithm/src/main/java/algorithm/C21_Node.java
create mode 100644 Algorithm/src/main/java/common/Node.java
create mode 100644 Algorithm/src/main/java/common/Student.java
create mode 100644 Algorithm/src/main/java/heap/C01_Comparator.java
create mode 100644 Algorithm/src/main/java/heap/C02_heap.java
create mode 100644 Algorithm/src/main/java/heap/HeapGreater.java
diff --git a/Algorithm/pom.xml b/Algorithm/pom.xml
index 49e18b2..ddbc6c5 100644
--- a/Algorithm/pom.xml
+++ b/Algorithm/pom.xml
@@ -7,6 +7,10 @@
org.example
Algorithm
1.0-SNAPSHOT
+
+ 17
+ 17
+
\ No newline at end of file
diff --git a/Algorithm/src/main/java/algorithm/C02_Stack.java b/Algorithm/src/main/java/algorithm/C02_Stack.java
new file mode 100644
index 0000000..ac9dcc9
--- /dev/null
+++ b/Algorithm/src/main/java/algorithm/C02_Stack.java
@@ -0,0 +1,45 @@
+package algorithm;
+
+import java.util.Stack;
+
+public class C02_Stack {
+
+ /**
+ * 设计一个特殊的栈,实现基本功能和返回栈最小元素
+ * pop、push、getMin操作时间复杂度都为O(1)
+ *
+ * 两个栈来实现:一个数据栈,一个最小值栈
+ * 压入数据栈时,比较当前值和最小栈的栈顶,将小的值压入栈顶
+ */
+
+ public static class MyStack1 {
+ private Stack dataStack;
+ private Stack minStack;
+
+ public MyStack1() {
+ dataStack = new Stack<>();
+ minStack = new Stack<>();
+ }
+
+ public int push(int e) {
+ dataStack.push(e);
+ Integer peek = minStack.peek();
+ if (peek == null) {
+ minStack.push(e);
+ } else {
+ minStack.push(Math.min(e, peek));
+ }
+ return e;
+ }
+
+ public int pop() {
+ Integer res = dataStack.pop();
+ Integer min = minStack.pop();
+ return res;
+ }
+
+ public int getMin() {
+ return minStack.peek();
+ }
+ }
+}
diff --git a/Algorithm/src/main/java/algorithm/C21_Node.java b/Algorithm/src/main/java/algorithm/C21_Node.java
new file mode 100644
index 0000000..c8ef9e7
--- /dev/null
+++ b/Algorithm/src/main/java/algorithm/C21_Node.java
@@ -0,0 +1,62 @@
+package algorithm;
+
+import common.Node;
+
+public class C21_Node {
+
+ public static void main(String args[]) {
+ System.out.println("开始:");
+ for (int i = 0; i < 20; i++) {
+ Node head = createRandomNode(8, 6);
+ printNodes(head);
+ System.out.print(" ===>> ");
+ Node node = removeValue(head, 5);
+ printNodes(node);
+ System.out.println();
+ }
+ }
+
+ public static void printNodes(Node head) {
+ Node pre = head;
+ while (pre != null) {
+ System.out.print(pre.value + " ");
+ pre = pre.next;
+ }
+ }
+
+ public static Node createRandomNode(int len, int max) {
+ Node head = new Node(randomNum(max));
+ Node next = head;
+ for (int i = 1; i < len; i++) {
+ int n = randomNum(max);
+ next.next = new Node(n);
+ next = next.next;
+ }
+ return head;
+ }
+
+ // 生成一个 [0, n) 的随机数
+ public static int randomNum(int n) {
+ return (int) (Math.random() * n);
+ }
+
+ public static Node removeValue(Node head, int num) {
+ while (head != null) {
+ if (head.value != num) {
+ break;
+ }
+ head = head.next;
+ }
+ Node pre = head;
+ Node cur = head;
+ while (cur != null) {
+ if (cur.value == num) {
+ pre.next = cur.next;
+ } else {
+ pre = cur;
+ }
+ cur = cur.next;
+ }
+ return head;
+ }
+}
diff --git a/Algorithm/src/main/java/common/Node.java b/Algorithm/src/main/java/common/Node.java
new file mode 100644
index 0000000..18186fc
--- /dev/null
+++ b/Algorithm/src/main/java/common/Node.java
@@ -0,0 +1,10 @@
+package common;
+
+public class Node {
+ public int value;
+ public Node next;
+
+ public Node(int value) {
+ this.value = value;
+ }
+}
diff --git a/Algorithm/src/main/java/common/Student.java b/Algorithm/src/main/java/common/Student.java
new file mode 100644
index 0000000..8e0a4ae
--- /dev/null
+++ b/Algorithm/src/main/java/common/Student.java
@@ -0,0 +1,41 @@
+package common;
+
+public class Student {
+
+ private int id;
+ private int age;
+ private String name;
+
+ public Student() {
+ }
+
+ public Student(int id, int age, String name) {
+ this.id = id;
+ this.age = age;
+ this.name = name;
+ }
+
+ public int getId() {
+ return id;
+ }
+
+ public void setId(int id) {
+ this.id = id;
+ }
+
+ public int getAge() {
+ return age;
+ }
+
+ public void setAge(int age) {
+ this.age = age;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+}
diff --git a/Algorithm/src/main/java/heap/C01_Comparator.java b/Algorithm/src/main/java/heap/C01_Comparator.java
new file mode 100644
index 0000000..a7f63ab
--- /dev/null
+++ b/Algorithm/src/main/java/heap/C01_Comparator.java
@@ -0,0 +1,47 @@
+package heap;
+
+
+import common.Student;
+
+import java.util.Arrays;
+import java.util.Comparator;
+
+public class C01_Comparator {
+
+ public static class Student {
+
+ public int id;
+ public int age;
+ public String name;
+
+ public Student(int id, int age, String name) {
+ this.id = id;
+ this.age = age;
+ this.name = name;
+ }
+ }
+
+ /**
+ * 比较器代码
+ */
+ public static class OrderAsc implements Comparator {
+
+ @Override
+ public int compare(Student o1, Student o2) {
+ return o1.id == o2.id ? o1.age - o2.age : o1.id - o2.id;
+ }
+ }
+
+ public static void main(String[] args) {
+ Student s1 = new Student(1, 21, "A");
+ Student s2 = new Student(2, 30, "B");
+ Student s3 = new Student(3, 21, "C");
+
+ Student[] s = new Student[]{s1, s2, s3};
+ Arrays.sort(s, new OrderAsc());
+ System.out.println("====1====");
+ for (Student c : s) {
+ System.out.println("name: " + c.name + ", id: " + c.id + ", age: " + c.age);
+ }
+ }
+}
diff --git a/Algorithm/src/main/java/heap/C02_heap.java b/Algorithm/src/main/java/heap/C02_heap.java
new file mode 100644
index 0000000..b1e81a6
--- /dev/null
+++ b/Algorithm/src/main/java/heap/C02_heap.java
@@ -0,0 +1,7 @@
+package heap;
+
+
+public class C02_heap {
+
+
+}
diff --git a/Algorithm/src/main/java/heap/HeapGreater.java b/Algorithm/src/main/java/heap/HeapGreater.java
new file mode 100644
index 0000000..9ef8bdd
--- /dev/null
+++ b/Algorithm/src/main/java/heap/HeapGreater.java
@@ -0,0 +1,119 @@
+package heap;
+
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.HashMap;
+
+/**
+ * 手写加强堆
+ *
+ * @param
+ */
+public class HeapGreater {
+
+ private ArrayList heap;
+ // 反向索引表
+ private HashMap indexMap;
+ private int heapSize;
+ private Comparator super T> comp;
+
+ public HeapGreater(Comparator comp) {
+ heap = new ArrayList<>();
+ indexMap = new HashMap<>();
+ heapSize = 0;
+ this.comp = comp;
+ }
+
+ public boolean isEmpty() {
+ return heapSize == 0;
+ }
+
+ public int size() {
+ return heapSize;
+ }
+
+ public boolean contains(T obj) {
+ return indexMap.containsKey(obj);
+ }
+
+ public T peek() {
+ return heap.get(0);
+ }
+
+ public void push(T obj) {
+ heap.add(obj);
+ indexMap.put(obj, heapSize);
+ heapInsert(heapSize++);
+ }
+
+ public T pop() {
+ T ans = heap.get(0);
+ swap(0, heapSize - 1);
+ indexMap.remove(ans);
+ heap.remove(--heapSize);
+ heapify(0);
+ return ans;
+ }
+
+ /**
+ * 修改对象属性后堆重排
+ *
+ * @param obj
+ */
+ public void resign(T obj) {
+ heapInsert(indexMap.get(obj));
+ heapify(indexMap.get(obj));
+ }
+
+ /**
+ * 删除堆任意节点(加强堆特有方法,交换至最后位置删除,原有位置重排)
+ *
+ * @param obj
+ */
+ public void remove(T obj) {
+ T replace = heap.get(heapSize - 1);
+ int index = indexMap.get(obj);
+ indexMap.remove(obj);
+ heap.remove(--heapSize);
+ if (obj != replace) {
+ heap.set(index, replace);
+ indexMap.put(replace, index);
+ resign(replace);
+ }
+ }
+
+ private void heapify(int index) {
+ int left = index << 1 + 1;
+ while (left < heapSize) {
+ int best = left + 1 < heapSize && comp.compare(heap.get(left + 1), heap.get(left)) < 0 ? (left + 1) : left;
+ best = comp.compare(heap.get(best), heap.get(index)) < 0 ? best : index;
+ if (best == index) {
+ break;
+ }
+ swap(best, index);
+ index = best;
+ left = index << 1 + 1;
+ }
+ }
+
+
+ private void heapInsert(int index) {
+ while (comp.compare(heap.get(index), heap.get((index - 1) / 2)) < 0) {
+ swap(index, (index - 1) / 2);
+ index = (index - 1) / 2;
+ }
+ }
+
+ private void swap(int i, int j) {
+ T o1 = heap.get(i);
+ T o2 = heap.get(j);
+ heap.set(i, o2);
+ heap.set(j, o1);
+ indexMap.put(o1, j);
+ indexMap.put(o2, i);
+ }
+
+ public static void main(String[] args) {
+ System.out.println(-1 / 2);
+ }
+}