1. 用栈实现队列
232. Implement Queue using Stacks (Easy)
class MyQueue {
Stack<Integer> stack1=new Stack<>();
Stack<Integer> stack2=new Stack<>();
/** Initialize your data structure here. */
public MyQueue() {
}
/** Push element x to the back of queue. */
public void push(int x) {
stack1.push(x);
}
/** Removes the element from in front of queue and returns that element. */
public int pop() {
if(stack2.isEmpty()){
while(!stack1.isEmpty()){
stack2.push(stack1.pop());
}
}
return stack2.pop();
}
/** Get the front element. */
public int peek() {
if(stack2.isEmpty()){
while(!stack1.isEmpty()){
stack2.push(stack1.pop());
}
}
return stack2.peek();
}
/** Returns whether the queue is empty. */
public boolean empty() {
return stack1.isEmpty()&&stack2.isEmpty();
}
}
/**
* Your MyQueue object will be instantiated and called as such:
* MyQueue obj = new MyQueue();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.peek();
* boolean param_4 = obj.empty();
*/
2. 用队列实现栈
225. Implement Stack using Queues (Easy)
class MyStack {
Queue<Integer> queue;
/** Initialize your data structure here. */
public MyStack() {
queue=new LinkedList<>();
}
/** Push element x onto stack. */
public void push(int x) {
int n=queue.size();
queue.add(x);
while(n-->0){
queue.add(queue.poll());
}
}
/** Removes the element on top of the stack and returns that element. */
public int pop() {
return queue.poll();
}
/** Get the top element. */
public int top() {
return queue.peek();
}
/** Returns whether the stack is empty. */
public boolean empty() {
return queue.isEmpty();
}
}
/**
* Your MyStack object will be instantiated and called as such:
* MyStack obj = new MyStack();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.top();
* boolean param_4 = obj.empty();
*/
3. 最小值栈
155. Min Stack (Easy)
class MinStack {
private Stack<Integer> stack;
private Stack<Integer> minStack;
/** initialize your data structure here. */
public MinStack() {
stack=new Stack<>();
minStack=new Stack<>();
}
public void push(int x) {
stack.push(x);
if(minStack.isEmpty()||minStack.peek()>=x){
minStack.add(x);
}
}
public void pop() {
int val=stack.pop();
if(val==minStack.peek())minStack.pop();
}
public int top() {
return stack.peek();
}
public int getMin() {
return minStack.peek();
}
}
/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(x);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/
4. 用栈实现括号匹配
20. Valid Parentheses (Easy)
class Solution {
public boolean isValid(String s) {
Stack<Character> stack=new Stack<>();
for(char c: s.toCharArray()){
if(c=='(')stack.push(')');
else if(c=='[')stack.push(']');
else if(c=='{')stack.push('}');
else if(stack.isEmpty()||stack.pop()!=c)return false;
}
return stack.isEmpty();
}
}
5. 数组中元素与下一个比它大的元素之间的距离
这个题目很巧妙,主要的用法在于是对栈存的元素是索引,并且是个单调栈。
739. Daily Temperatures (Medium)
class Solution {
public int[] dailyTemperatures(int[] T) {
Stack<Integer> stack=new Stack<>();
int N=T.length;
int[] result=new int[N];
for(int i=0;i<N;i++){
while(!stack.isEmpty()&&T[i]>T[stack.peek()]){
int temp=stack.pop();
result[temp]=i-temp;
}
stack.push(i);
}
return result;
}
}
6. 循环数组中比当前元素大的下一个元素'
也就是多了几行代码,全部设置为-1,循环的化用俩个数组
- 思路
- 1.将数组中所有元素全部置为-1
- 2.遍历两次,相当于循环遍历
- 3.第一遍遍历,入栈索引i
- 4.只要后面元素比栈顶索引对应的元素大,索引出栈,更改res[sta.pop()]的数值
- 5.最后栈里面剩余的索引对应的数组值,都为默认的-1(因为后面未找到比它大的值) */
503. Next Greater Element II (Medium)
class Solution {
public int[] nextGreaterElements(int[] nums) {
int N=nums.length;
int[] res=new int[N];
Arrays.fill(res,-1);
Stack<Integer> stack=new Stack<>();
for(int i=0;i<N*2;i++){
int num=nums[i%N];
while(!stack.isEmpty()&&num>nums[stack.peek()]){
res[stack.pop()]=num;
}
if(i<N)stack.push(i);
}
return res;
}
}