摘要
一、队列 & 栈原理与解题方法
二、队列 & 栈相关算法练习题目
2.1 两个栈实现队列
用两个栈实现队列_牛客题霸_牛客网
package 栈和队列;
import java.util.Stack;
/**
* @Classname JZ9用两个栈实现队列
* @Description TODO
* @Date 2022/2/1 18:22
* @Created by xjl
*/
public class JZ9用两个栈实现队列 {
Stack<Integer> stack1 = new Stack<Integer>();
Stack<Integer> stack2 = new Stack<Integer>();
public void push(int node) {
while (!stack2.isEmpty()){
stack1.add(stack2.pop());
}
stack1.add(node);
}
public int pop() {
while (!stack1.isEmpty()){
stack2.add(stack1.pop());
}
return stack2.pop();
}
public void push1(int node) {
stack1.push(node);
}
public int pop1() {
if(stack1.empty()&&stack2.empty()){
throw new RuntimeException("Queue is empty!");
}
if(stack2.empty()){
while(!stack1.empty()){
stack2.push(stack1.pop());
}
}
return stack2.pop();
}
}
2.2 包含最小的min的栈
包含min函数的栈_牛客题霸_牛客网
package 栈和队列;
import java.util.Stack;
/**
* @Classname JZ30包含min函数的栈
* @Description TODO
* @Date 2022/2/1 18:47
* @Created by xjl
*/
public class JZ30包含min函数的栈 {
Stack<Integer> stack = new Stack<Integer>();
public void push(int node) {
if (stack.isEmpty()) {
stack.add(node);
stack.add(node);
} else {
int value=stack.peek();
if (value>=node){
stack.add(node);
stack.add(node);
}else {
stack.add(node);
stack.add(value);
}
}
}
public void pop() {
stack.pop();
stack.pop();
}
public int top() {
int n1=stack.pop();
int n2=stack.pop();
stack.add(n2);
stack.add(n1);
return n2;
}
public int min() {
int tmp=stack.peek();
return tmp;
}
Stack<Integer> stack1 = new Stack<Integer>();
Stack<Integer> stack2 = new Stack<Integer>();
public void push1(int node) {
stack1.add(node);
if (stack2.isEmpty()){
stack2.add(node);
}else {
if (node<stack2.peek()){
stack2.add(node);
}else {
stack2.add(stack2.peek());
}
}
}
public void pop1() {
stack1.pop();
stack2.pop();
}
public int top1() {
return stack1.peek();
}
public int min1() {
return stack2.peek();
}
public static void main(String[] args) {
JZ30包含min函数的栈 j=new JZ30包含min函数的栈();
j.push(1);
j.push(0);
j.push(-3);
j.min();
j.pop();
j.top();
j.min();
}
}
2.3 栈的压入和弹出序列
栈的压入、弹出序列_牛客题霸_牛客网
package 栈和队列;
import java.util.Stack;
/**
* @Classname JZ31栈的压入弹出序列
* @Description TODO
* @Date 2022/2/2 9:07
* @Created by xjl
*/
public class JZ31栈的压入弹出序列 {
public boolean IsPopOrder(int[] pushA, int[] popA) {
Stack<Integer> stack = new Stack();
int pushIndex = 0, popIndex = 0;
while(pushIndex < pushA.length){
//入栈
stack.push(pushA[pushIndex++]);
//判断出栈的后的是否一致
while(popIndex<popA.length && !stack.isEmpty() && stack.peek()==popA[popIndex]){
stack.pop();
popIndex++;
}
}
//判断是全部出栈了
return stack.isEmpty();
}
}
2.4 翻转单词序列
翻转单词序列_牛客题霸_牛客网
package 栈和队列;
import org.junit.Test;
import java.util.Stack;
/**
* @Classname JZ73翻转单词序列
* @Description TODO
* @Date 2022/2/2 9:22
* @Created by xjl
*/
public class JZ73翻转单词序列 {
public String ReverseSentence(String str) {
StringBuilder result = new StringBuilder();
String[] s = str.split(" ");
for(int i=s.length-1;i>=0;i--){
result.append(s[i]).append(" ");
}
return result.toString().trim();
}
@Test
public void test() {
String str = "nowcoder. a am I";
String s = ReverseSentence(str);
System.out.println(s);
}
}
2.5 滑动串口的最大值
滑动窗口的最大值_牛客题霸_牛客网
第一种,硬暴力