爱上算法:每日算法(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)
相关推荐
roman_日积跬步-终至千里1 小时前
【机器学习】“回归“算法模型的三个评估指标:MAE(衡量预测准确性)、MSE(放大大误差)、R²(说明模型解释能力)
算法·机器学习·回归
小指纹2 小时前
图论-最短路Dijkstra算法
数据结构·c++·算法·深度优先·图论
赴3353 小时前
逻辑回归 银行贷款资格判断案列优化 交叉验证,调整阈值,下采样与过采样方法
算法·机器学习·逻辑回归·下采样·交叉验证·过采样·阈值
2501_924878734 小时前
无人机光伏巡检缺陷检出率↑32%:陌讯多模态融合算法实战解析
开发语言·人工智能·算法·视觉检测·无人机
沉睡的无敌雄狮4 小时前
无人机光伏巡检漏检率↓78%!陌讯多模态融合算法实战解析
人工智能·算法·计算机视觉·目标跟踪
magicwt4 小时前
《从零构建大模型》读书笔记
算法
大胖猫L4 小时前
深搜与广搜在 TypeScript 类型递归中的应用
前端·算法
2202_756749695 小时前
02 基于sklearn的机械学习-KNN算法、模型选择与调优(交叉验证、朴素贝叶斯算法、拉普拉斯平滑)、决策树(信息增益、基尼指数)、随机森林
python·算法·决策树·随机森林·机器学习·sklearn
ATaylorSu5 小时前
经典算法之美:冒泡排序的优雅实现
开发语言·笔记·学习·算法
菜鸡nan6 小时前
23th Day| 39.组合总和,40.组合总和II,131.分割回文串
算法·leetcode·职场和发展