爱上算法:每日算法(24-2月4号)

🌟坚持每日刷算法,😃将其变为习惯🤛让我们一起坚持吧💪

文章目录

232. 用栈实现队列

思路

首先应该先明确队列是先进先出,

而栈是先进后出,而如果想用栈实现队列,就可以尝试用两个栈

进栈和出栈

  • 进栈模拟入队列
  • 出栈模拟先出队列

画图如下

Code

Java

java 复制代码
class MyQueue {
				
				Stack<Integer> stIn;
				Stack<Integer> stOut;
    public MyQueue() {
      stIn = new Stack<>();
			stOut = new Stack<>();
    }
    
    public void push(int x) {
						stIn.push(x);
    }
    
    public int pop() {
       
						if(stOut.isEmpty()){
						    while(!stIn.isEmpty()){
						        stOut.push(stIn.pop());
						    } 
						}
						return stOut.pop();
    }
    
    public int peek() {
							int res = this.pop();
							stOut.push(res);
							return res;
							
    }
    
    public boolean empty() {
							return stOut.isEmpty()&&stIn.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();
 */

C++

c 复制代码
class MyQueue {
public:
				stack<int>	 stIn;
				stack<int> stOut;
				
    MyQueue() {

    }
    
    void push(int x) {
						stIn.push(x);
    }
    
    int pop() {
							if(stOut.empty()){
							    while(!stIn.empty()){
							        stOut.push(stIn.top());
							        stIn.pop();
							    }
							}
							int result = stOut.top();
							stOut.pop();
							return result;
    }
    
    int peek() {
						int res = this->pop();
						stOut.push(res);
						return res;
    }
    
     bool empty() {
						return stIn.empty()&&stOut.empty();
    }
};

/**
 * 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();
 * bool param_4 = obj.empty();
 */

复杂度

时间复杂度: push和empty为O(1), pop和peek为O(n)

空间复杂度: O(n)

225. 用队列实现栈

思路

队列是先进先出原则,

而栈是先进后出原则

因此,可以使用两个队列来实现栈

可以使用一个队列来实现栈

满足先进后出的方法就是; 入队列之后,就将这个数放到队首

Code

C++

c 复制代码
class MyStack {
public:	
				queue<int> que;
    MyStack() {

    }
    
    void push(int x) {
						que.push(x);
    }
    
    int pop() {
						int size = que.size();
						size--;
						while(size--){
						    que.push(que.front());
						    que.pop();
						}
						int res = que.front();
						que.pop();
						return res;
    }
    
    int top() {
					return que.back();
    }
    
    bool empty() {
						return que.empty();
    }
};

/**
 * 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();
 * bool param_4 = obj->empty();
 */

Java

java 复制代码
class MyStack {
    Queue<Integer> que = new LinkedList<>();
    public MyStack() {

    }
    
    public void push(int x) {
            que.add(x);
    }
    
    public int pop() {
        rePosition();
        return que.poll();
    }
    
    public int top() {
        rePosition();
        int res = que.poll();
        que.add(res);
        return res;
    }
    
    public boolean empty() {
        return que.isEmpty();
    }
    public void rePosition(){
        int size = que.size();
        size--; // 不包括刚刚添加的数
        while(size-- > 0){
            que.add(que.poll());
        }
    }
}

/**
 * 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();
 */

复杂度

  • 时间复杂度: pop为O(n),其他为O(1)
  • 空间复杂度: O(n)
相关推荐
liuluyang5301 小时前
C语言C11支持的结构体嵌套的用法
c语言·开发语言·算法·编译·c11
勤劳的进取家2 小时前
贪心算法之最小生成树问题
数据结构·python·算法·贪心算法·排序算法·动态规划
牛奶咖啡.8542 小时前
第十四届蓝桥杯大赛软件赛省赛C/C++ 大学 A 组真题
c语言·数据结构·c++·算法·蓝桥杯
亓才孓2 小时前
[leetcode]stack的基本操作的回顾
算法
小美爱刷题2 小时前
力扣DAY46-50 | 热100 | 二叉树:展开为链表、pre+inorder构建、路径总和、最近公共祖先、最大路径和
算法·leetcode·链表
Fanxt_Ja3 小时前
【数据结构】红黑树超详解 ---一篇通关红黑树原理(含源码解析+动态构建红黑树)
java·数据结构·算法·红黑树
永恒迷星.by3 小时前
全球变暖(蓝桥杯 2018 年第九届省赛)
算法
旧时光林4 小时前
蓝桥杯 分解质因数(唯一分解定理)
数据结构·c++·算法·蓝桥杯·模拟·枚举
烁3474 小时前
每日一题(小白)模拟娱乐篇27
java·数据结构·算法·娱乐
Hello bugyan5 小时前
并查集initial,find,union+应用
数据结构·算法