栈:用pop移除、push、peek只查看不移除
队列:队列用poll、offer、top
题目一:用栈实现队列
1. 思路:用两个栈,分别用于入栈和出栈,实现各种方法
2. 代码
import java.util.Stack;
public class MyQueue {
static Stack<Integer> stackin = new Stack<>();
static Stack<Integer> stackout = new Stack<>();
public MyQueue() {
Stack stackin;
Stack stackout;
}
public void push(int x) {
stackin.push(x);
}
public static int pop() {
if (stackout.isEmpty()) {
while (!stackin.isEmpty()) {
stackout.push(stackin.pop());
}
}
return stackout.pop();
}
public int peek() {
if (stackout.isEmpty()) {
while (!stackin.isEmpty()) {
stackout.push(stackin.pop());
}
}
return stackout.peek();
}
public boolean empty() {
return stackin.isEmpty() && stackout.isEmpty();
}
}
(2)测试用例
public static void main(String[] args) {
MyQueue myQueue = new MyQueue();
myQueue.push(1); // queue is: [1]
myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue)
myQueue.peek(); // return 1
myQueue.pop(); // return 1, queue is [2]
myQueue.empty(); // return false
}
题目二:用队列实现栈
1. 可以用一个队列反复进行插入,队列里有size个元素,弹出size-1个元素,重新加入队列,然后弹出最后一个元素
2. (1)实现
class MyStack {
Deque<Integer> queue=new LinkedList<Integer>();
public MyStack() {
Deque<Integer> queue;
}
public void push(int x) {
queue.offer(x);//先插入x
int size=queue.size();
for(int i=0;i<size-1;i++){//将除了x的其它元素重新插入
queue.offer(queue.poll());
}
}
public int pop() {
return queue.poll();
}
public int top() {
return queue.peek();
}
public boolean empty() {
return queue.isEmpty();
}
}
(2)测试用例
public static void main(String[] args) {
MyStack myStack = new MyStack();
myStack.push(1);
myStack.push(2);
System.out.println(myStack.top()); // 返回 2
System.out.println(myStack.pop());; // 返回 2
System.out.println(myStack.empty()); // 返回 False
}
题目三:有效的括号
1. 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效,有效字符串需满足:
- 左括号必须用相同类型的右括号闭合。
- 左括号必须以正确的顺序闭合。
- 注意空字符串可被认为是有效字符
2.思路
(1)括号不匹配的情况:括号多余,括号类型没匹配上,没有对应的括号
(2)可以先做剪枝:如果size不是偶数的话就false
(3)在判断错误的时候:先判断是否为空再弹出栈顶元素进行比较
public boolean isValid(String s) {
Stack<Character> stack=new Stack<>();
for(int i=0;i<s.length();i++){
if(s.charAt(i)=='[')stack.push(']');
else if(s.charAt(i)=='{')stack.push('}');
else if(s.charAt(i)=='(')stack.push(')');
else if(stack.isEmpty() || s.charAt(i)!=stack.pop()) return false;
}
return stack.isEmpty();
}
题目四:删除字符串中的所有相邻重复项
1. 题目:给出由小写字母组成的字符串 S,重复项删除操作会选择两个相邻且相同的字母,并删除它们。例如:输入:"abbaca",输出:"ca"
2. 思路:和上一题一样,如果一样则弹出,不一样则入栈,最后返回
public static String removeDuplicates(String s) {
Stack<Character> stack = new Stack<>();
for(int i=0;i<s.length();i++){
if(stack.isEmpty() || stack.peek()!=s.charAt(i))stack.push(s.charAt(i));
else stack.pop();
}
String ans="";
while(!stack.isEmpty()){
ans=stack.pop()+ans;
}
return ans;
}
题目五:逆波兰表达式求值
1. 题目:. - 力扣(LeetCode)
2. 思路:如果遇到计算符号,就弹出最上面的两个数字,进行操作,否则就入栈
public static int evalRPN(String[] tokens) {
Stack<Integer> stack = new Stack<>();
for(String i:tokens){
if(i.equals("+")|| i.equals("-") || i.equals("*") || i.equals("/")){
int num1= stack.pop();
int num2= stack.pop();
if(i.equals("+"))stack.push(num1 + num2);
else if(i.equals("-"))stack.push(-num1 + num2);
else if(i.equals("*"))stack.push(num1 * num2);
else if(i.equals("/"))stack.push(num2 / num1);
}else {
stack.push(Integer.parseInt(i));
}
}
return stack.pop();
}
题目六:滑动窗口最大值
1. 题目:给你一个整数数组 nums
,有一个大小为 k
的滑动窗口从数组的最左侧移动到数组的最右侧。你只可以看到在滑动窗口内的 k
个数字。滑动窗口每次只向右移动一位。返回 滑动窗口中的最大值 。
2. 单调队列:维护队列中单调递增和单调递减
3. 思路
(1)维护单调队列:每次新加入的元素和前面的进行比较,如果前面的元素小,就弹出,直到没有元素比新加的元素大
题目七:前k个高频元素