225. Implement Stack using Queues

k8s部署 普罗米修斯(Prometheus)监控 Prometheus是一个开源的服务监控系统和时序数据库其提供了通用的数据模型和快捷数据采集、存储和查询接口它的核心组件Prometheus服务器定期从静态配置的监控目标或者基于服务发现自动配置的目标中进行拉 取数据新拉取到啊的 数据大于配置的内存缓存区时,数据就会持久化到存储设备当中。 阅读详情

Implement the following operations of a stack using queues.

  • push(x) -- Push element x onto stack.
  • pop() -- Removes the element on top of the stack.
  • top() -- Get the top element.
  • empty() -- Return whether the stack is empty.
Notes:
  • You must use only standard operations of a queue -- which means only push to back, peek/pop from front, size, and is empty operations are valid.
  • Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.
  • You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).                                         

实话说,这道题出的有点无聊,我用deque做的,下面还有一种stack的思路。

1.deque
class Stack {
public:
    // Push element x onto stack.
    void push(int x) {
        que.push_back(x);
    }

    // Removes the element on top of the stack.
    void pop() {
        if(!que.empty())
            que.pop_back();
    }

    // Get the top element.
    int top() {
        return que.back();
    }

    // Return whether the stack is empty.
    bool empty() {
        return que.empty();
    }
private:
    deque<int> que;
};


2.queue
使用队列时需要注意,queue没有pop_back()的方法,因为它是first in first out, 而stack 是 first in last out,所以要通过把前面的元素挪到新加元素的后面,利用queue的pop方法,每次弹出队列队头元素即可。
代码如下:
class Stack {
public:
	queue<int> que;
	void push(int x) {
        que.push(x);
        for(int i=0; i<que.size()-1; ++i){
            que.push(que.front());
            que.pop();
        }
	}

	void pop() {
		que.pop();
	}

	int top() {
		return que.front();
	}

	bool empty() {
		return que.empty();
	}
};



Ubuntu 解决WPS缺少宋体,微软雅黑等中文字体 这篇博客解决Ubuntu 18.04 无法使用中文字体的问题 问题描述 阅读详情

相关推荐

6 赫斯曼网管软件industrial hivision用户管理

文章目录前言一、打开Industrial HiVision服务器二、在用户管理界面添加策略三、使用可选策略四、接受策略总结 前言 安装 Industrial HiVision 后,所有人都能广泛使用程序。安全起见,请进行相 关配置,对访问权限进行限制。 本文主要介绍用户管理基于某种角色模型。在访问角色中定义角色名称,通过分配访问权限给这些角色分配角色功能。例如,给管理人员自然要分配可用的权限,而观察员尽管可以登录,但没有能够进行更改的权限。Industrial HiVision 提供以下访问角色: 

t98746194的博客 1389

为什么多线程读写 shared_ptr 要加锁?

为什么多线程读写 shared_ptr 要加锁? 陈硕(giantchen_AT_gmail_DOT_com) 2012-01-28 最新版下载:http://chenshuo.googlecode.com/files/CppEngineering.pdf 我在《Linux 多线程服务端编程:使用 muduo C++ 网络库》第 1.9 节“再论 shared_ptr 的线程安全”中写道: (s...

923723914 1196

对工时评估的思考:当AI颠覆了coding效率

在AI时代,“人日”作为软件工时度量衡已然失效。开发的价值核心正从“编码实现”转向“深度思考”与“精准沟通”——即如何将人类的模糊需求精确地传达给AI。当最有价值的“思想工作”变得最难量化时,传统的项目管理范式随之动摇。我们必须超越对过程的算计,开始探索衡量思想、沟通与业务价值的新标尺。

架构师刘7的博客 1024

leetcode 225: Implement Stack using Queues

leetcode 225: Implement Stack using Queues python java c++

西施豆腐渣 5108

kettle写doris操作

文件在目录:doris-master\extension\kettle\assemblies\plugin\target\doris-stream-loader-plugins-9.4.0.0-343.zip。官方提供了doris stream load插件,下载源码编译后放到kettle的插件目录即可使用。地址:https://github.com/apache/doris.git。注意:需要doris插件或者kettle转换生成模板的私聊博主。使用PDmaner同步业务库生成建表语句。

weixin_44449054的博客 3047

STM32CubeMx配置串口使用printf函数打印数据(HAL库开发)

HAL库开发串口,打印printf

m0_58832575的博客 6291

LeetCode 225: Implement Stack using Queues

Implement the following operations of a stack using queues. push(x) -- Push element x onto stack.pop() -- Removes the element on top of the stack.top() -- Get the top element.empty() -- Return whet

哈哈的个人专栏 6893

EtherCAT丢包问题深度解析:从原理到实战排查指南

在工业自动化与运动控制领域,实时通讯网络的稳定性是系统可靠运行的基石。EtherCAT(以太网控制自动化技术)作为一种高性能工业以太网协议,其核心在于独特的“飞读飞写”机制和纳秒级的分布时钟同步,以实现多设备间的精准协同。然而,物理链路干扰、配置不当或硬件性能瓶颈都可能导致数据帧丢失,破坏时钟同步,进而引发伺服抖动、定位精度下降乃至生产线停机。本文聚焦于EtherCAT丢包这一典型故障,结合SSC(Slave Stack Code Tool)从站协议栈开发与实时系统调优等工程实践,系统阐述其成因、影响及一套

weixin_32050773的博客 358

LeetCode225:Implement Stack using Queues

Implement the following operations of a stack using queues.push(x) – Push element x onto stack. pop() – Removes the element on top of the stack. top() – Get the top element. empty() – Return whether

1576

DP83869HM的LWIP性能测试

E:/Xilinx/Vitis/2021.1/data/embeddedsw/ThirdParty/sw_services/lwip211_v2_0/src/contrib/ports/xilinx/netif/xemacpsif_physpeed.c文件中的。然后phy_setup_emacps中调用get_IEEE_phy_speed函数,根据PHY的ID判断出Marvell或TI或Realtek的PHY,调用对应的网速自动协商函数get_TI_phy_speed,该函数中的部分配置需要注释。

FPGA艺术家的博客 359

手把手教你学Simulink--基于高频与高速信号处理的滤波器场景实例:毫米波雷达中匹配滤波器的多目标分辨仿真

在自动驾驶、智能交通和工业感知等领域,毫米波雷达(mmWave Radar)因其高分辨率全天候工作和抗电磁干扰能力,成为关键的感知传感器。其核心信号处理算法之一是匹配滤波器(Matched Filter),用于在强噪声背景下最大化信噪比(SNR),实现对微弱目标的检测。在调频连续波(FMCW, Frequency-Modulated Continuous Wave)雷达中,发射信号为线性调频(Chirp)信号。当信号遇到多个目标时,回波是多个延迟版本的叠加。

MHD0815的博客 144

[LeetCode 225] Implement Stack using Queues

Implement the following operations of a stack using queues. push(x) -- Push element x onto stack.pop() -- Removes the element on top of the stack.top() -- Get the top element.empty() -- Return whet

coder 进阶的专栏 823

手把手入门YOLO到改进优化教程专栏介绍与说明——列表目录查阅(持续更新)

手把手入门YOLO到改进优化教程专栏介绍与说明——列表目录查阅(持续更新)

嵌入式阿齐Archie 2万+

225 Implement Stack using Queues

题目链接:https://leetcode.com/problems/implement-stack-using-queues/题目:Implement the following operations of a stack using queues.push(x) -- Push element x onto stack. pop() -- Removes the element on top o

再见了,过去的名字,新的奋斗开始啦! 335

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

【leetcode】225. Implement Stack using Queues

题目: 思路: 其他都很简单,就是pop函数:连续pop,push直接到最后一个元素为止,最后一个元素只pop不push。 代码实现: class MyStack { private: queue<int> my_queue; public: /** Initialize your data structure here. */ MyStack() { } /** Push element x onto stack. */

zxc120389574的博客 291

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

【LeetCode OJ 225】Implement Stack using Queues

题目链接:https://leetcode.com/problems/implement-stack-using-queues/ 题目:Implement the following operations of a stack using queues. push(x) -- Push element x onto stack.pop() -- Removes the element on

1302

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

225. Implement Stack using Queues [easy] (Python)

题目链接 https://leetcode.com/problems/implement-stack-using-queues/ 题目原文 Implement the following operations of a stack using queues. 1. push(x) – Push element x onto stack. 2. pop() – Removes

Coder_Orz的博客 3624
上一篇: 12. Integer to Roman
下一篇: 226. Invert Binary Tree
FreeeLinux
博客等级 码龄11年 421粉丝 356原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址