一个人的朝圣 — LeetCode打卡第9天
知识总结
今天深入了解了Java栈和队列的两种操作, 并且做了两到基础题. 两个数据结构在Java中都有对应的使用方法和构造方法, 需要掌握, 这里我们也巩固一下.
Stack 栈的操作
构建栈
Stack<Integer> stack = new Stack<>():
五大关键方法
void push(E e); E pop(); E peek(); boolean isEmpty(); int size();
push(E e) //入栈
pop() //栈顶元素出栈, 如果栈顶没有元素则抛出异常
isEmpty() //判定栈是否为空 注意这里和Collection的区别
peek() //获取栈顶元素
size() // 返回大小
search(num) //栈顶到该元素首次出现的位置的距离
import java.util.Stack;
public class Solution{
public static void main(String[] args){
Stack<Integer> sk=new Stack<Integer>();
System.out.println(sk.isEmpty());//判断栈是否为空,true
for(int i=0;i<10;i++){
int e=sk.push(i+20);//将元素加入栈顶,并返回栈顶元素
System.out.println(i+": "+e);
}
System.out.println(sk.empty());//判断栈是否为空,false
System.out.println(sk.pop());//返回栈顶元素,并删除
System.out.println(sk.peek());//返回当前栈顶元素
System.out.println("first:"+sk.search(20));//查找栈中元素的位置
System.out.println("last:"+sk.search(29));
}
}
Queue 队列的操作
构建队列
Queue<Integer> queue = new LinkedList<>();
五大关键方法
void offer(); E poll(); E peek(); boolean isEmpty(); int size();
// remove(), element() 会返回异常, 所以用的比较少
Queue<String> queue = new LinkedList<String>();
//增添
offer(object) // 在队列尾部添加一个元素,并返回是否成功
//移除
poll() // 删除队列中第一个元素,并返回该元素的值,如果元素为null,将返回null(其实调用的是pollFirst())
remove() // 删除队列中第一个元素,并返回该元素的值,如果元素为null,将抛出异常(其实底层调用的是removeFirst())
//查找首个元素
peek() // 获取第一个元素,如果返回null
element() //获取第一个元素,如果没有将抛出异常
isEmpty()//队列为空返回true,否则返回false
size()// 返回队列长度
import java.util.LinkedList;
import java.util.Queue;
public class Solution{
public static void main(String[] args){
Queue<Integer> queue=new LinkedList<Integer>();
System.out.println(queue.isEmpty());//判断是否为空,true
//System.out.println(queue.empty());//错误,没有这个方法
for(int i=0;i<10;i++){
queue.offer(i+20);//向队尾添加元素
System.out.println(i+": "+(i+20));
}
System.out.println("peekFirst:"+queue.peek());
System.out.println("removeFirst:"+queue.poll());//获得队首元素并删除
System.out.println("peekFirst:"+queue.peek());//获得队首元素
}
}
Leetcode 232. 用栈实现队列
题目说明
请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(push、pop、peek、empty):
实现 MyQueue 类:
代码说明
class MyQueue {
Stack<Integer> stackA;
Stack<Integer> stackB;
public MyQueue() {
this.stackA = new Stack<>();
this.stackB = new Stack<>();
}
public void push(int x) {
while(!stackB.isEmpty()){
stackA.push(stackB.pop());
}
stackA.push(x);
}
public int pop() {
while(!stackA.isEmpty()){
stackB.push(stackA.pop());
}
return stackB.pop();
}
public int peek() {
while(!stackA.isEmpty()){
stackB.push(stackA.pop());
}
return stackB.peek();
}
public boolean empty() {
return stackB.isEmpty() && stackA.isEmpty();
}
}
Leetcode 225. 用队列实现栈
题目说明
请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通栈的全部四种操作(push、top、pop 和 empty)。
代码说明
使用两个队列
class MyStack {
Queue<Integer> queue1;
Queue<Integer> queue2;
public MyStack() {
this.queue1 = new LinkedList<>();
this.queue2 = new LinkedList<>();
}
public void push(int x) {
if(queue1.isEmpty()){
queue1.offer(x);
while(!queue2.isEmpty()){
queue1.offer(queue2.poll());
}
}else{
queue2.offer(x);
while(!queue1.isEmpty()){
queue2.offer(queue1.poll());
}
}
}
public int pop() {
if(queue1.isEmpty()){
return queue2.poll();
}
return queue1.poll();
}
public int top() {
if(queue1.isEmpty()){
return queue2.peek();
}
return queue1.peek();
}
public boolean empty() {
return queue1.isEmpty() && queue2.isEmpty();
}
}
也可以只使用一个队列
class MyStack {
Queue<Integer> queue1;
public MyStack() {
this.queue1 = new LinkedList<>();
}
public void push(int x) {
int n = queue1.size();
queue1.offer(x);
for(int i = 0; i < n; i++){
queue1.offer(queue1.poll());
}
}
public int pop() {
return queue1.poll();
}
public int top() {
return queue1.peek();
}
public boolean empty() {
return queue1.isEmpty();
}
}