From 25b5d1834c39907eaddcc5b21ba06cbc4806f829 Mon Sep 17 00:00:00 2001 From: Leo <582717189@qq.com> Date: Sat, 21 Nov 2020 00:04:35 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B0=E7=BB=84=E5=AE=9E=E7=8E=B0=E9=98=9F?= =?UTF-8?q?=E5=88=97,Stack.getMin()=20by=20=20O(1)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/leo/class01/KM.java | 2 - src/leo/class02/GetMinStack.java | 260 +++++++++++++++++++++++++++++++ src/leo/class02/RingArray.java | 112 +++++++++++++ 3 files changed, 372 insertions(+), 2 deletions(-) create mode 100644 src/leo/class02/GetMinStack.java create mode 100644 src/leo/class02/RingArray.java diff --git a/src/leo/class01/KM.java b/src/leo/class01/KM.java index 2bdd7b1..363765f 100644 --- a/src/leo/class01/KM.java +++ b/src/leo/class01/KM.java @@ -1,7 +1,5 @@ package leo.class01; -import class01.Code08_KM; - import java.util.HashMap; import java.util.HashSet; diff --git a/src/leo/class02/GetMinStack.java b/src/leo/class02/GetMinStack.java new file mode 100644 index 0000000..4b4d9e5 --- /dev/null +++ b/src/leo/class02/GetMinStack.java @@ -0,0 +1,260 @@ +package leo.class02; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Stack; + +/** + * @author Leo + * @ClassName GetMinStack + * @DATE 2020/11/20 4:54 下午 + * @Description + */ +public class GetMinStack { + + /** + * 只有最小值入栈,最小值只入一次栈 + */ + public static class MyStack1{ + + private Stack data; + private Stack min; + public MyStack1() { + this.data = new Stack<>(); + this.min = new Stack<>(); + } + public void push(int value) { + if (this.min.isEmpty()||value <= this.getMin()) { + this.min.push(value); + } + data.push(value); + } + public int pop() { + if (data.isEmpty()) { + throw new RuntimeException("stack isEmpty"); + } + Integer value = data.pop(); + if (this.getMin() == value) { + min.pop(); + } + return value; + } + public int getMin() { + if (min.isEmpty()) { + throw new RuntimeException("stack isEmpty"); + } + return min.peek(); + } + + + + + } + + /** + * 只有最小值入栈,最小值重复入栈 + */ + public static class MyStack2 { + private Stack data; + private Stack min; + + public MyStack2() { + data = new Stack<>(); + min = new Stack<>(); + } + public void push(int value) { + if (data.isEmpty()) { + min.push(value); + } else if (this.getMin() > value) { + min.push(value); + }else { + min.push(this.getMin()); + } + data.push(value); + } + + public int pop() { + if (data.isEmpty()) { + throw new RuntimeException("Stack is Empty"); + } + this.min.pop(); + return data.pop(); + } + + + public int getMin() { + if (min.isEmpty()) { + throw new RuntimeException("stack is Empty"); + } + return min.peek(); + } + + + + } + + /** + * 用list实现 只存最小值的索引 + */ + public static class MyStackOfList{ + private List data; + private List min; + + public MyStackOfList() { + this.data = new ArrayList<>(); + this.min = new ArrayList<>(); + } + + public void push(int value) { + this.data.add(value); + if (this.min.size() == 0) { + this.min.add(0); + }else{ + int minValue = getMin(); + if (minValue > value) { + min.add(data.size() - 1); + } + } + } + + + public int pop() { + if (data.size() == 0) { + throw new RuntimeException("stack is empty!"); + } + int dataIndex = this.data.size() - 1; + int minIndex = this.min.get(min.size() - 1); + int value = data.get(dataIndex); + if (dataIndex == minIndex) { + min.remove(minIndex); + } + data.remove(dataIndex); + return value; + } + + public int getMin() { + if (min.size() == 0) { + throw new RuntimeException("stack is empty"); + } + return data.get(min.get(min.size() - 1)); + } + + } + + /** + * test + */ + public static class TestMyStack { + private List data ; + + public TestMyStack() { + data = new ArrayList<>(); + } + + public void push(int value) { + data.add(value); + } + + public int pop() { + if (data.size() == 0) { + throw new RuntimeException("stack is empty"); + } + int value = data.get(data.size() - 1); + data.remove(data.size() - 1); + return value; + } + + public int gitMin() { + if (data.size() == 0) { + throw new RuntimeException("stack is empty"); + } + Integer[] dataArray = data.toArray(new Integer[data.size()]); + Arrays.sort(dataArray); + return dataArray[0]; + } + } + + + public static void main(String[] args) { + + int testTime = 10000; + int range = 200; + int forTime = 100; + System.out.println("Start!"); + + for (int i = 0; i < testTime; i++) { + MyStackOfList myStack = new MyStackOfList(); + TestMyStack testMyStack = new TestMyStack(); + for (int j = 0; j < forTime; j++) { + if (Math.random() < 0.5) { + int value = randomIndex(range); + myStack.push(value); + testMyStack.push(value); + } else { + try { + int myStack1Value = myStack.pop(); + int testMyStackValue = testMyStack.pop(); + if (myStack1Value != testMyStackValue) { + System.out.println("myStack1Value: "+myStack1Value); + System.out.println("testMyStackMin: "+testMyStackValue); + return; + } + } catch (Exception e) { + int value = randomIndex(range); + myStack.push(value); + testMyStack.push(value); + } + + } + } + for (int j = 0; j < forTime; j++) { + if (Math.random() < 0.5) { + try { + int myStack1Min = myStack.getMin(); + int testMyStackMin = testMyStack.gitMin(); + if (myStack1Min != testMyStackMin) { + System.out.println("myStack1Min: "+myStack1Min); + System.out.println("testMyStackMin: "+testMyStackMin); + break; + } + } catch (Exception e) { + int value = randomIndex(range); + myStack.push(value); + testMyStack.push(value); + } + + } else { + + try { + int myStack1Value = myStack.pop(); + int testMyStackValue = testMyStack.pop(); + if (myStack1Value != testMyStackValue) { + System.out.println("myStack1Value: "+myStack1Value); + System.out.println("testMyStackValue: "+testMyStackValue); + break; + } + } catch (Exception e) { + int value = randomIndex(range); + myStack.push(value); + testMyStack.push(value); + } + } + } + } + System.out.println("End!"); + } + + + public static int randomIndex(int range) { + return (int) ((int) (range * Math.random() + 1) - (range * Math.random() + 1)); + } + + + + + + + + +} diff --git a/src/leo/class02/RingArray.java b/src/leo/class02/RingArray.java new file mode 100644 index 0000000..6334a9a --- /dev/null +++ b/src/leo/class02/RingArray.java @@ -0,0 +1,112 @@ +package leo.class02; + + +import java.util.Queue; +import java.util.LinkedList; + +/** + * @author Leo + * @ClassName RingArray + * @DATE 2020/11/20 2:53 下午 + * @Description 使用数字实现队列 + */ +public class RingArray { + + /** + * 使用数字实现队列 + */ + public static class MyQueue { + int[] arr; + int head; + int tail; + int size; + final int limit; + + + public MyQueue(int limit) { + arr = new int[limit]; + head=0; + tail=0; + size=0; + this.limit = limit; + } + + public void push(int value) { + if (size == limit) { + new RuntimeException("队列满了!"); + } + arr[head] = value; + size++; + head = head < limit - 1 ? head + 1 : 0; + } + + public int poll() { + if (size == 0) { + new RuntimeException("队列空了!"); + } + int value = arr[tail]; + size--; + tail = tail < limit - 1 ? tail + 1 : 0; + return value; + } + + 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); + Queue queue = new LinkedList<>(); + for (int j = 0; j < length; j++) { + int value = randomInt(range); + if (myQueue.isEmpty()){ + myQueue.push(value); + queue.offer(value); + }else{ + if (Math.random() < 0.5) { + myQueue.push(value); + queue.offer(value); + }else { + int myQueueValue = myQueue.poll(); + Integer queueValue = queue.poll(); + if (!isEquals(myQueueValue, queueValue)) { + System.out.println(myQueueValue); + System.out.println(queueValue); + System.out.println("fuck!!"); + break; + } + } + } + } + + } + System.out.println("OK!👌"); + + } + + public static int randomInt(int range) { + return (int) (range * Math.random()) + 1; + } + + public static boolean isEquals(Integer a, Integer b) { + if (a == null && b != null) { + return false; + } + if (a != null && b == null) { + return false; + } + if (a == null || b == null) { + return true; + } + return a.equals(b); + } + +}