提交 026c6637 编写于 作者: GreyZeng's avatar GreyZeng

update code

上级 56f7ca38
package git.snippet.queueandstack;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
// 用双链表实现栈和队列
// @see LeetCode_0641_DesignCircularDeque
// notes: https://www.cnblogs.com/greyzeng/p/16631644.html
public class Code_DoubleEndsListToStackAndQueue {
public static boolean isEqual(Integer o1, Integer o2) {
if (o1 == null && o2 != null) {
return false;
}
if (o1 != null && o2 == null) {
return false;
}
if (o1 == null) {
return true;
}
return o1.equals(o2);
}
public static void main(String[] args) {
int oneTestDataNum = 100;
int value = 10000;
int testTimes = 100000;
for (int i = 0; i < testTimes; i++) {
MyStack<Integer> myStack = new MyStack<>();
MyQueue<Integer> myQueue = new MyQueue<>();
Stack<Integer> stack = new Stack<>();
Queue<Integer> queue = new LinkedList<>();
for (int j = 0; j < oneTestDataNum; j++) {
int nums = (int) (Math.random() * value);
if (stack.isEmpty()) {
myStack.push(nums);
stack.push(nums);
} else {
if (Math.random() < 0.5) {
myStack.push(nums);
stack.push(nums);
} else {
if (!isEqual(myStack.pop(), stack.pop())) {
System.out.println("oops!");
}
}
}
int numq = (int) (Math.random() * value);
if (queue.isEmpty()) {
myQueue.push(numq);
queue.offer(numq);
} else {
if (Math.random() < 0.5) {
myQueue.push(numq);
queue.offer(numq);
} else {
if (!isEqual(myQueue.poll(), queue.poll())) {
System.out.println("oops!");
}
}
}
}
}
System.out.println("finish!");
}
public static class Node<T> {
public T value;
......
......@@ -11,37 +11,31 @@ public class LeetCode_0155_MinStack {
System.out.println(Long.toBinaryString(offset));
}
static class MinStack {
class MinStack {
Stack<Integer> stack;
Stack<Integer> minStack;
Stack<Integer> valStack;
public MinStack() {
stack = new Stack<>();
minStack = new Stack<>();
valStack = new Stack<>();
}
public void push(int val) {
valStack.push(val);
if (!minStack.isEmpty()) {
if (minStack.peek() < val) {
minStack.push(minStack.peek());
} else {
minStack.push(val);
}
} else {
stack.push(val);
if (minStack.isEmpty() || minStack.peek() > val) {
minStack.push(val);
} else {
minStack.push(minStack.peek());
}
}
public void pop() {
if (!valStack.isEmpty()) {
valStack.pop();
minStack.pop();
}
minStack.pop();
stack.pop();
}
public int top() {
return valStack.peek();
return stack.peek();
}
public int getMin() {
......
......@@ -22,4 +22,17 @@ public class Generator {
System.arraycopy(arr1, 0, arr2, 0, arr1.length);
return arr2;
}
public static boolean isEqual(Integer o1, Integer o2) {
if (o1 == null && o2 != null) {
return false;
}
if (o1 != null && o2 == null) {
return false;
}
if (o1 == null) {
return true;
}
return o1.equals(o2);
}
}
package git.snippet.queueandstack;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
import static git.snippet.common.Generator.isEqual;
@DisplayName("用双链表实现栈和队列")
public class Code_DoubleEndsListToStackAndQueueTest {
@Test
@DisplayName("用双链表实现栈和队列")
public void testInAndOut() {
int oneTestDataNum = 100;
int value = 10000;
int testTimes = 100000;
for (int i = 0; i < testTimes; i++) {
Code_DoubleEndsListToStackAndQueue.MyStack<Integer> myStack = new Code_DoubleEndsListToStackAndQueue.MyStack<>();
Code_DoubleEndsListToStackAndQueue.MyQueue<Integer> myQueue = new Code_DoubleEndsListToStackAndQueue.MyQueue<>();
Stack<Integer> stack = new Stack<>();
Queue<Integer> queue = new LinkedList<>();
for (int j = 0; j < oneTestDataNum; j++) {
int nums = (int) (Math.random() * value);
if (stack.isEmpty()) {
myStack.push(nums);
stack.push(nums);
} else {
if (Math.random() < 0.5) {
myStack.push(nums);
stack.push(nums);
} else {
if (!isEqual(myStack.pop(), stack.pop())) {
Assertions.fail();
}
}
}
int numq = (int) (Math.random() * value);
if (queue.isEmpty()) {
myQueue.push(numq);
queue.offer(numq);
} else {
if (Math.random() < 0.5) {
myQueue.push(numq);
queue.offer(numq);
} else {
if (!isEqual(myQueue.poll(), queue.poll())) {
Assertions.fail();
}
}
}
}
}
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册