代码随想录刷题记录day10

代码随想录刷题记录day10

Java的栈与队列基础

栈:先进后出

常见API:

//声明一个栈:
Stack < Integer > stack=new Stack<>();

//入栈
stack.push(1);

//出栈
stack.pop();

//获取栈顶元素
stack.peek();

//判断是否为空
stack.empty();

队列:一端进另一端出

常见API:

//声明
Deque< Integer> queue =new LinkedList<>();

//入队
queue.offer(1);

//出队 同时会返回值 
int i =queue.poll();


//获取队首元素
int j= queue.peek();

//获取队尾元素
int k=queue.getLast();

//删除队尾元素
int l=queue.removeLast();//调用的是LinkedList 的方法

leetcode:232. 用栈实现队列

请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(push、pop、peek、empty):

实现 MyQueue 类:

void push(int x) 将元素 x 推到队列的末尾
int pop() 从队列的开头移除并返回元素
int peek() 返回队列开头的元素
boolean empty() 如果队列为空,返回 true ;否则,返回 false
说明:

你 只能 使用标准的栈操作 —— 也就是只有 push to top, peek/pop from top, size, 和 is empty 操作是合法的。
你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。

示例 1:

输入:
[“MyQueue”, “push”, “push”, “peek”, “pop”, “empty”]
[[], [1], [2], [], [], []]
输出:
[null, null, null, 1, 1, false]

解释:
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

思想:

声明两个栈,stack1,stack2。把stack1的元素出栈,放到stack2中,stack2再出栈,出栈顺序就会改变。但是得注意(pop操作):

  • 先判断stack2中的元素是否为空,不为空就直接出栈就好了
  • stack2为空,将stack1中的所有的元素都拉取到stack2中,再进行出栈操作
代码
class MyQueue {
    Stack<Integer> stack1=new Stack<>(); //放进入的元素

    Stack<Integer> stack2=new Stack<>();//放出去的元素

    // 队列 一端进 另一端出
    public MyQueue() {

    }
    
    public void push(int x) {
        //入队操作
        this.stack1.push(x);
    }
    
    public int pop() {
        //出的是队列的头

        if(!stack2.empty()){
            //s2中还有先进的元素
            int temp=stack2.peek();
            stack2.pop();
            return temp;
        }

        while(!stack1.empty()){
            stack2.push(stack1.peek());
            stack1.pop();
        }
        int temp=stack2.peek();
        stack2.pop();
        return temp;
        
    }
    
    public int peek() {
        if(!stack2.empty()){
            //s2中还有先进的元素
            int temp=stack2.peek();
            
            return temp;
        }

         while(!stack1.empty()){
            stack2.push(stack1.peek());
            stack1.pop();
        }

        int temp=stack2.peek();
        return temp;
    }
    
    public boolean empty() {
        if(stack1.empty()&&stack2.empty()){
            return true;
        }
        else{
            return false;
        }
    }
}

leedcode:225. 用队列实现栈

请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通栈的全部四种操作(push、top、pop 和 empty)。

实现 MyStack 类:

void push(int x) 将元素 x 压入栈顶。
int pop() 移除并返回栈顶元素。
int top() 返回栈顶元素。
boolean empty() 如果栈是空的,返回 true ;否则,返回 false 。

注意:

你只能使用队列的基本操作 —— 也就是 push to back、peek/pop from front、size 和 is empty 这些操作。
你所使用的语言也许不支持队列。 你可以使用 list (列表)或者 deque(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。

示例:

输入:
[“MyStack”, “push”, “push”, “top”, “pop”, “empty”]
[[], [1], [2], [], [], []]
输出:
[null, null, null, 2, 2, false]

解释:
MyStack myStack = new MyStack();
myStack.push(1);
myStack.push(2);
myStack.top(); // 返回 2
myStack.pop(); // 返回 2
myStack.empty(); // 返回 False

思想:

一开始陷入了上一题的惯性逻辑中,寻思着,这该咋整!

后来:两个队列que1 和que2,其中一个队列que2做备份,具体步骤如下,主要是pop 和top的操作

1.声明一个size记录栈的长度

2.pop操作,将size-1个元素进行备份,推入到新的队列que2,把第size个元素出队并记录res

3.将que2再推入到que1队列

4.size–,返回res

一个队列实现pop操作

将size-1个元素出队以后再入队,将第size个元素出队就好了,真妙!

代码
class MyStack {
    //
    Queue<Integer> que1=new LinkedList<>();

    Queue<Integer> que2=new LinkedList<>();

    private int size;


    public MyStack() {

    }
    
    public void push(int x) {
        que1.offer(x);
        size++;
    }
    
    public int pop() {
        //que2 做备份

        for(int i=0;i<size-1;i++){
            que2.offer(que1.poll());
        }
        int res=que1.poll();

        for(int i=0;i<size-1;i++){
            que1.offer(que2.poll());
        }
        size--;
        return res;

    }
    
    public int top() {
        //que2 做备份

        for(int i=0;i<size-1;i++){
            que2.offer(que1.poll());
        }
        int res=que1.poll();


        for(int i=0;i<size-1;i++){
            que1.offer(que2.poll());
        }
        que1.offer(res);
        return res;
    }
    
    public boolean empty() {
        if(size==0)return true;
        else return false;
    }
}
class MyStack {
  
// 用一个队列来实现。

    Queue<Integer> que1=new LinkedList<>();

    // 出队列以后 再重新入队列
    private int size;


    public MyStack() {

    }
    
    public void push(int x) {
        que1.offer(x);
        size++;
    }
    
    public int pop() {
        //que2 做备份

        for(int i=0;i<size-1;i++){
            que1.offer(que1.poll());
        }
        int res=que1.poll();

        size--;
        return res;

    }
    
    public int top() {

        for(int i=0;i<size-1;i++){
            que1.offer(que1.poll());
        }
        int res=que1.poll();
        que1.offer(res);

        return res;
    }
    
    public boolean empty() {
        if(size==0)return true;
        else return false;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值