From cdbdf836b865586df6705bc17c77b8da0e148c3f Mon Sep 17 00:00:00 2001 From: Leo <582717189@qq.com> Date: Sat, 21 Nov 2020 21:20:30 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E4=B8=89=E8=8A=82=E7=BB=83=E4=B9=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/leo/class02/DeleteGivenValue.java | 56 +++++++++++++++++- src/leo/class02/GetMax.java | 55 ++++++++++++++++++ src/leo/class02/GetMinStack.java | 82 ++++++++++++++++++++++++++- src/leo/class02/LinkedList.java | 43 +++++++++++++- src/leo/class02/RingArray.java | 49 +++++++++++++++- 5 files changed, 277 insertions(+), 8 deletions(-) create mode 100644 src/leo/class02/GetMax.java diff --git a/src/leo/class02/DeleteGivenValue.java b/src/leo/class02/DeleteGivenValue.java index fb6e69b..7a96af8 100644 --- a/src/leo/class02/DeleteGivenValue.java +++ b/src/leo/class02/DeleteGivenValue.java @@ -219,6 +219,60 @@ public class DeleteGivenValue { return head; } + public static DoubleNode removeDoubleNodeOfValue4(DoubleNode head, int value) { + while (head != null) { + if (head.value != value) { + break; + } + head = head.next; + } + if (head != null) { + head.pre = null; + } + DoubleNode pre = head; + DoubleNode cur = head; + while (cur != null) { + if (cur.value == value) { + pre.next = cur.next; + }else{ + if (cur.pre != pre) { + cur.pre = pre; + } + pre = cur; + } + cur = cur.next; + } + return head; + } + + + public static DoubleNode removeDoubleNodeOfValue5(DoubleNode head, int value) { + while (head != null) { + if (head.value != value) { + break; + } + head = head.next; + } + if (head != null && head.pre != null) { + head.pre = null; + } + + DoubleNode pre = head; + DoubleNode cur = head; + while (cur != null) { + if (cur.value == value) { + pre.next = cur.next; + }else{ + if (cur.pre != pre) { + cur.pre = pre; + } + pre = cur; + } + cur = cur.next; + } + return head; + } + /** * 功能描述 : 验证单链表删除结果 * @author Leo @@ -332,7 +386,7 @@ public class DeleteGivenValue { break; } DoubleNode doubleNodeHead = randomDoubleNode(sizeMax, range); - DoubleNode doubleNode = removeDoubleNodeOfValue3(doubleNodeHead, value); + DoubleNode doubleNode = removeDoubleNodeOfValue5(doubleNodeHead, value); if (!verifyRemoveDoubleNodeOfValue(doubleNode, value)) { System.out.println("doubleNode fuck"); break; diff --git a/src/leo/class02/GetMax.java b/src/leo/class02/GetMax.java new file mode 100644 index 0000000..946ed5b --- /dev/null +++ b/src/leo/class02/GetMax.java @@ -0,0 +1,55 @@ +package leo.class02; + +import leo.util.ArrayUtil; + +import java.util.Arrays; + +/** + * @author Leo + * @ClassName GetMax + * @DATE 2020/11/21 7:52 下午 + * @Description + */ +public class GetMax { + + public static int getMax(int[] arr) { + int max = process(arr, 0, arr.length - 1); + return max; + } + + private static int process(int[] arr, int L, int R) { + if (L == R) { + return arr[L]; + } + int mid = L + ((R - L) >> 1); + int leftMax = process(arr, 0, mid); + int rightMax = process(arr, mid + 1, R); + return Math.max(leftMax, rightMax); + } + + + private static int testGetMax(int[] arr) { + Arrays.sort(arr); + return arr[arr.length - 1]; + } + + public static void main(String[] args){ + int maxSize = 10; + int range = 100; + int testTime = 100; + System.out.println("Start!"); + + for (int i = 0; i < testTime; i++) { + int[] arr = ArrayUtil.randomAdjacentNotEqualArray(maxSize, range); + int max = getMax(arr); + int testMax = testGetMax(arr); + if (max != testMax) { + System.out.println("fuck"); + break; + } + } + System.out.println("End!"); + } + + +} diff --git a/src/leo/class02/GetMinStack.java b/src/leo/class02/GetMinStack.java index 4b4d9e5..d05ce8b 100644 --- a/src/leo/class02/GetMinStack.java +++ b/src/leo/class02/GetMinStack.java @@ -14,7 +14,7 @@ import java.util.Stack; public class GetMinStack { /** - * 只有最小值入栈,最小值只入一次栈 + * 每个数进数据栈,最小栈只有栈顶要更新才加 省一点点空间 */ public static class MyStack1{ @@ -50,6 +50,45 @@ public class GetMinStack { + } + + + public static class MyStack3{ + + private Stack data; + private Stack min; + + public MyStack3() { + data = new Stack<>(); + min = new Stack<>(); + } + + public void push(int value) { + if (min.isEmpty() || this.getMin() >= value) { + min.push(value); + } + data.push(value); + } + + public Integer pop() { + if (data.isEmpty()) { + throw new RuntimeException("Stack is Empty"); + } + int value = data.pop(); + if (value == this.getMin()) { + min.pop(); + } + return value; + } + + + public Integer getMin() { + if (min.isEmpty()) { + throw new RuntimeException("stack is empty"); + } + return min.peek(); + } + } /** @@ -94,6 +133,45 @@ public class GetMinStack { } + + public static class MyStack4{ + private Stack data; + private Stack min; + + public MyStack4() { + this.data = new Stack<>(); + this.min = new Stack<>(); + } + + public void push(int value) { + try { + if (data.isEmpty() || this.getMin() > value) { + this.min.push(value); + } else { + min.push(this.getMin()); + } + } catch (Exception e) { + this.min.push(value); + } + this.data.push(value); + } + + public Integer pop() throws Exception { + if (data.isEmpty()) { + throw new Exception("Stack is Empty!"); + } + this.min.pop(); + return this.data.pop(); + } + + public int getMin() throws Exception { + if (this.min.isEmpty()) { + throw new Exception("stack is empty"); + } + return min.peek(); + } + } + /** * 用list实现 只存最小值的索引 */ @@ -184,7 +262,7 @@ public class GetMinStack { System.out.println("Start!"); for (int i = 0; i < testTime; i++) { - MyStackOfList myStack = new MyStackOfList(); + MyStack4 myStack = new MyStack4(); TestMyStack testMyStack = new TestMyStack(); for (int j = 0; j < forTime; j++) { if (Math.random() < 0.5) { diff --git a/src/leo/class02/LinkedList.java b/src/leo/class02/LinkedList.java index 8365470..14e9452 100644 --- a/src/leo/class02/LinkedList.java +++ b/src/leo/class02/LinkedList.java @@ -130,6 +130,31 @@ public class LinkedList { return pre; } + + public static Node reverseNode7(Node head) { + Node pre = head; + Node next = null; + while (head != null) { + next = head.next; + head.next = pre; + pre = head; + head = next; + } + return pre; + } + + public static Node reverseNode8(Node head) { + Node pre = head; + Node next = null; + while (head != null) { + next = head.next; + head.next = pre; + pre = head; + head = next; + } + return pre; + } + public static DoubleNode reverseDoubleNode(DoubleNode head) { DoubleNode pre = null; DoubleNode next; @@ -183,6 +208,20 @@ public class LinkedList { } + + public static DoubleNode reverseDoubleNode4(DoubleNode head) { + DoubleNode pre = head; + DoubleNode next = null; + while (head != null) { + next = head.next; + head.next = pre; + head.pre = next; + pre = head; + head = next; + } + return pre; + } + /** * 功能描述 : 随机生成单链表 * @author Leo @@ -313,14 +352,14 @@ public class LinkedList { for (int i = 0; i < testTime; i++) { Node head = randomNode(maxSize, range); List nodeList = nodeToList(head); - Node node = reverseNode6(head); + Node node = reverseNode8(head); if (!verifyReverseListAndNode(nodeList, node)) { System.out.println("nodeFuck!!"); break; } DoubleNode doubleNodeHead = randomDoubleNode(maxSize, range); List doubleNodeList = DoubleNodeToList(doubleNodeHead); - DoubleNode doubleNode = reverseDoublerNode3(doubleNodeHead); + DoubleNode doubleNode = reverseDoubleNode4(doubleNodeHead); if (!verifyReverseListAndDoubleNode(doubleNodeList, doubleNode)) { System.out.println("doubleNodeFuck!!"); break; diff --git a/src/leo/class02/RingArray.java b/src/leo/class02/RingArray.java index 6334a9a..486a274 100644 --- a/src/leo/class02/RingArray.java +++ b/src/leo/class02/RingArray.java @@ -8,12 +8,12 @@ import java.util.LinkedList; * @author Leo * @ClassName RingArray * @DATE 2020/11/20 2:53 下午 - * @Description 使用数字实现队列 + * @Description 使用数组实现队列 */ public class RingArray { /** - * 使用数字实现队列 + * 使用数组实现队列 */ public static class MyQueue { int[] arr; @@ -57,13 +57,56 @@ public class RingArray { } + public static class MyQueue1{ + private int[] arr; + private int size; + private int push; + private int poll; + private final int limit; + + public MyQueue1(int limit) { + arr = new int[limit]; + this.size = 0; + this.push = 0; + this.poll = 0; + this.limit = limit; + } + + public void push(int value) { + if (size == limit) { + throw new RuntimeException("队列满了"); + } + arr[push] = value; + size++; + push = nextIndex(push); + } + + public int poll() { + if (size == 0) { + throw new RuntimeException("队列空了"); + } + int value = arr[poll]; + size--; + poll = nextIndex(poll); + return value; + } + + public int nextIndex(int i) { + return i < limit - 1 ? i + 1 : 0; + } + + public boolean isEmpty() { + return size == 0; + } + } + public static void main(String[] args) { int testTime = 10000; int range = 100; int sizeMax = 80; for (int i = 0; i < testTime; i++) { int length = randomInt(sizeMax); - MyQueue myQueue = new MyQueue(length); + MyQueue1 myQueue = new MyQueue1(length); Queue queue = new LinkedList<>(); for (int j = 0; j < length; j++) { int value = randomInt(range);