栈(Stack):后进先出(LIFO)
方法 | 解释 |
---|---|
push(e) | 压栈 |
pop() | 出栈 |
peek() | 取栈顶元素(不删除) |
boolean empty() | 判断栈是否为空 |
队列(Queue):先进先出(FIFO)
方法 | 解释 |
---|---|
add(e) / ofter(e) | 入队列,add()抛出异常,offer()返回特殊值 |
remove() / pool() | 出队列,remove()抛出异常,offer()返回特殊值 |
element() / peek() | 取队首元素(不删除)element()抛出异常,peek()返回特殊值 |
isEmpty() | 判断是否为空 |
练习:
力扣(LeetCode) 20. 有效的括号
给定一个只包括 ‘(’,’)’,’{’,’}’,’[’,’]’ 的字符串 s ,判断字符串是否有效。
有效字符串需满足:
左括号必须用相同类型的右括号闭合。
左括号必须以正确的顺序闭合。
class Solution {
public static boolean isValid(String s) {
Stack<Character> stack = new Stack<Character>();
char[] chars = s.toCharArray();
for (int i = 0; i < s.length();i++) {
switch (chars[i]) {
case '(':
case '{':
case '[':
stack.push(chars[i]); //放入栈
break;
default:
if (stack.isEmpty()) {
return false;
}
//取出栈顶元素
char left = stack.pop();
if (!compareBracket(left, chars[i])) {
return false;
}
}
}
if (!stack.isEmpty() ) {
return false;
} else {
return true;
}
}
private static boolean compareBracket(char left, char right) {
if (left == '(' && right == ')') {
return true;
}
if (left == '{' && right == '}') {
return true;
}
if (left == '[' && right == ']') {
return true;
}
return false;
}
}
力扣(LeetCode)232. 用栈实现队列
import java.util.Stack;
class MyQueue {
private Stack<Integer> stack1;
private Stack<Integer> stack2;
/** Initialize your data structure here. */
public MyQueue() {
stack1 = new Stack<Integer>(); //取
stack2 = new Stack<Integer>(); //放
}
/** Push element x to the back of queue. */
public void push(int x) {
stack2.push(x); //往stack2中放元素
}
/** Removes the element from in front of queue and returns that element. */
public int pop() {
if (stack1.isEmpty()) {
while (!stack2.isEmpty()) {
stack1.push(stack2.pop()); //将stack2中的元素取出来放到stack1中
}
}
return stack1.pop();
}
/** Get the front element. */
public int peek() {
if (stack1.isEmpty()) {
while (!stack2.isEmpty()) {
stack1.push(stack2.pop()); //将stack2中的元素取出来放到stack1中
}
}
return stack1.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();
*/
力扣(LeetCode)225.用队列实现栈
import java.util.Stack;
class MyQueue {
private Stack<Integer> stack1;
private Stack<Integer> stack2;
/** Initialize your data structure here. */
public MyQueue() {
stack1 = new Stack<Integer>(); //取
stack2 = new Stack<Integer>(); //放
}
/** Push element x to the back of queue. */
public void push(int x) {
stack2.push(x); //往stack2中放元素
}
/** Removes the element from in front of queue and returns that element. */
public int pop() {
if (stack1.isEmpty()) {
while (!stack2.isEmpty()) {
stack1.push(stack2.pop()); //将stack2中的元素取出来放到stack1中
}
}
return stack1.pop();
}
/** Get the front element. */
public int peek() {
if (stack1.isEmpty()) {
while (!stack2.isEmpty()) {
stack1.push(stack2.pop()); //将stack2中的元素取出来放到stack1中
}
}
return stack1.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();
*/