当前位置: 首页 > news >正文

网站建设教程出售用苏州久远网络现在的网络营销方式

网站建设教程出售用苏州久远网络,现在的网络营销方式,杭州营销型网站怎么做,唐山网站建设公司哪家好JAVA代码编写 232. 用栈实现队列 请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作#xff08;push、pop、peek、empty#xff09;#xff1a; 实现 MyQueue 类#xff1a; void push(int x) 将元素 x 推到队列的末尾int pop() 从队列的开头移除…JAVA代码编写 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提示 1 x 9最多调用 100 次 push、pop、peek 和 empty假设所有操作都是有效的 例如一个空的队列不会调用 pop 或者 peek 操作 进阶 你能否实现每个操作均摊时间复杂度为 O(1) 的队列换句话说执行 n 个操作的总时间复杂度为 O(n) 即使其中一个操作可能花费较长时间。 教程https://programmercarl.com/0232.%E7%94%A8%E6%A0%88%E5%AE%9E%E7%8E%B0%E9%98%9F%E5%88%97.html#%E7%AE%97%E6%B3%95%E5%85%AC%E5%BC%80%E8%AF%BE 解 思路队列由两个栈表示进栈stackIn、出栈stackOut 队列进队push等价于直接进栈 队列出队pop将stackIn中出栈到stackOut中然后再出栈 dumpstackIn()函数将stackIn中出栈到stackOut中 返回队列底元素最先入队的元素相当岁返回stackOut的栈顶元素 队列是否为空为空的话就是要stackIn和stackOut都为空 import java.util.Stack;class MyQueue {StackInteger stackIn;StackInteger stackOut;/** Initialize your data structure here. */public MyQueue() {stackIn new Stack(); // 负责进栈stackOut new Stack(); // 负责出栈}/** Push element x to the back of queue.入队 */public void push(int x) {stackIn.push(x);}/** Removes the element from in front of queue and returns that element. 删除该队列元素删第一个进队列的*/public int pop() {dumpstackIn();return stackOut.pop();}/** Get the front element. 返回队列底元素最先入队的元素*/public int peek() {dumpstackIn();return stackOut.peek();}/** Returns whether the queue is empty. */public boolean empty() {return stackIn.isEmpty() stackOut.isEmpty();}// 如果stackOut为空那么将stackIn中的元素全部放到stackOut中private void dumpstackIn(){if (!stackOut.isEmpty()) return;while (!stackIn.isEmpty()){stackOut.push(stackIn.pop());}}public static void main(String[] args) {MyQueue myQueue new MyQueue();myQueue.push(1);myQueue.push(2);myQueue.push(3);myQueue.push(4);myQueue.pop();myQueue.push(5);myQueue.push(6);System.out.println(myQueue.peek());} } 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提示 1 x 9最多调用100 次 push、pop、top 和 empty每次调用 pop 和 top 都保证栈不为空 **进阶**你能否仅用一个队列来实现栈。 教程https://programmercarl.com/0225.%E7%94%A8%E9%98%9F%E5%88%97%E5%AE%9E%E7%8E%B0%E6%A0%88.html 解 思路栈由两个队列表示queue1、queue2 进栈push将元素进队到queue2当queue1不是空的时候将queue1出队的元素入队到queue2再交换queue1和queue2 import java.util.LinkedList; import java.util.Queue;class MyStack {QueueInteger queue1; // 和栈中保持一样元素的队列QueueInteger queue2; // 辅助队列/** Initialize your data structure here. */public MyStack() {queue1 new LinkedList();queue2 new LinkedList();}/** Push element x onto stack. 入栈*/public void push(int x) {queue2.offer(x); // 先放在辅助队列中offer元素入队while (!queue1.isEmpty()){queue2.offer(queue1.poll());//poll:移除并返回队列头部的元素//逆序存入queue2}QueueInteger queueTemp;queueTemp queue1;queue1 queue2;queue2 queueTemp; // 最后交换queue1和queue2将元素都放到queue1中}/** Removes the element on top of the stack and returns that element. */public int pop() {return queue1.poll(); // 因为queue1中的元素和栈中的保持一致所以这个和下面两个的操作只看queue1即可}/** Get the top element. */public int top() {return queue1.peek();}/** Returns whether the stack is empty. */public boolean empty() {return queue1.isEmpty();}public static void main(String[] args) {MyStack myStack new MyStack();myStack.push(1);myStack.push(2);myStack.push(3);myStack.pop();myStack.push(4);} }
http://www.zqtcl.cn/news/556457/

相关文章:

  • 浮动播放器wordpress刷神马seo排名首页排名
  • 建设网站呼叫中心有什么好处站长统计免费下载
  • 做网站电脑配置要求个高吗电子商务网站的建设包含哪些流程图
  • 青岛做网站大公司wordpress文章页加一言
  • 用哪个网站做相册视频文件夹谷歌ads
  • 注册top域名做公司网站男女做暖网站是什么
  • 郴州本地网站建设高端网站设计多少钱
  • 此网站域名即将过期广告制作属于什么行业
  • 牛牛襄阳网站建设wap网站asp源码
  • 信用网站建设招标书建网站需要什么手续
  • 重庆建网站方法网站开发和维护
  • 做网站需要什么人活动策划流程及细节
  • wordpress企业网站seo上海市
  • 北京建外贸网站公司网络域名是什么
  • 聚美优品网站建设方案上市公司的信息网站
  • 济南做网站比较好的公司知道吗为什么做美食视频网站
  • 药店网站源码宣传方式
  • word如何做网站链接淘宝客建站需要多少钱
  • 凡科网免费建站步骤及视频logo设计网页
  • 天梯网站建设软件开发公司职位
  • 建站公司外贸东方购物网上商城
  • 白银做网站企业免费网站模板
  • 网络公司给我们做的网站_但是我们不知道域名是否属于我们湖北正规网站建设质量保障
  • 本地网站asp iis团队展示网站
  • 企业网站管理系统cmswordpress知识管理系统
  • 创建一个网站需要怎么做销售平台公司
  • 网站域名实名认证吗做斗图的网站
  • 公司在兰州要做网站怎样选择做网站数据库表各字段详情
  • 营销型网站建设的要素搭建本地网站
  • 深圳网站建设V芯ee8888ewordpress瀑布流主 #65533;