225. Implement Stack using Queues + 232. Implement Queue using Stacks
▶ 栈和队列的相互表示。发现内置的队列和栈结构都十分高效,相互表示后性能损失都很小。
▶ 第 225 题,用队列实现栈
● 自己的代码,3 ms,单队列实现,入栈 O(1),读取栈顶元素 O(n),出栈 O(n) 。
class MyStack
{
private:
queue<int> fakeStack;
public:
MyStack()
{
fakeStack = queue<int>();
}
void push(int x)
{
fakeStack.push(x);
}
int pop()
{
if (fakeStack.empty())
return -;
int i, output;
if (fakeStack.size() == )
{
output = fakeStack.front();
fakeStack.pop();
return output;
}
for (i = ;; i++)
{
output = fakeStack.front(), fakeStack.pop();
if (i == fakeStack.size())// 注意这里 fakeStack.size() 已经发生了改变,不用再 -1 了
return output;
else
fakeStack.push(output);
}
}
int top()
{
if (fakeStack.empty())
return -;
if (fakeStack.size() == )
return fakeStack.front();
int i, output;
for (i = ;; i++)
{
output = fakeStack.front(), fakeStack.pop();
fakeStack.push(output);
if (i == fakeStack.size() - )
return output;
}
}
bool empty()
{
return fakeStack.empty();
}
};
● 大佬的代码,3 ms,队列 queue 具有成员函数 back(),可以用于读取队尾元素。单队列实现,入栈 O(1),读取栈顶元素 O(1),出栈 O(n) 。
class MyStack
{
int curr_size;
queue<int> q1, q2;
public:
MyStack()
{
curr_size = ;
}
void push(int x)
{
q1.push(x);
curr_size++;
}
int pop()
{
if (q1.empty())
return -;
while (q1.size() != )
{
q2.push(q1.front());
q1.pop();
}
int temp = q1.front();
q1.pop();
curr_size--;
queue<int> q = q1;
q1 = q2;
q2 = q;
return temp;
}
int top()
{
return q1.back();
}
bool empty()
{
return q1.empty();
}
};
● 作弊,就使用内置栈结构,
class MyStack
{
private:
stack<int> st;
public:
MyStack()
{
st = stack<int>();
}
void push(int x)
{
st.push(x);
}
int pop()
{
int output = st.top();
st.pop();
return output;
}
int top()
{
return st.top();
}
bool empty()
{
return st.empty();
}
};
▶ 第 232 题,用栈实现队列
● 自己的解法,2 ms,双栈实现,分别命名为 fakeQueueIn 和 fakeQueueOut,使用一个标记 store 来记录数据存放于哪个栈里,需要入队的时候把数据倒进 fakeQueueIn 中,需要读取队头或者出队的时候把数据倒入 fakeQueueOut 中,其他情况下不再调整数据的存放位置。
class MyQueue
{
private:
stack<int> fakeQueueIn,fakeQueueOut;
bool store; // true:数据存储在 fakeQueueIn 中
void moveBetweenStack(stack<int>& in, stack<int>& out)
{
int temp;
for (; !in.empty(); out.push(in.top()), in.pop());
}
public:
MyQueue()
{
fakeQueueIn = stack<int>();
fakeQueueOut = stack<int>();
store = true;
}
void push(int x)
{
if (!store)
{
moveBetweenStack(fakeQueueOut, fakeQueueIn);
store = true;
}
fakeQueueIn.push(x);
}
int pop()
{
int output;
if (store)
{
moveBetweenStack(fakeQueueIn, fakeQueueOut);
store = false;
}
output = fakeQueueOut.top();
fakeQueueOut.pop();
return output;
}
int peek()
{
if (store)
{
moveBetweenStack(fakeQueueIn, fakeQueueOut);
store = false;
}
return fakeQueueOut.top();
}
bool empty()
{
return fakeQueueIn.empty() && fakeQueueOut.empty();
}
};
● 最快的解法,2 ms,压根没用 stack,用的 list
class MyQueue
{
public:
list<int> data;
list<int> buffer;
int dataSize;
MyQueue() : data{}, buffer{}, dataSize{ } {}
void push(int x) {
if (dataSize == )
data.push_back(x);
else
buffer.push_back(x);
++dataSize;
}
int pop()
{
--dataSize;
auto res = data.back();
data.pop_back();
if (data.empty())
{
while (!buffer.empty())
{
data.push_back(buffer.back());
buffer.pop_back();
}
}
return res;
}
int peek()
{
return data.back();
}
bool empty()
{
return dataSize == ;
}
};
● 作弊,使用内置的队列结构,3 ms
class MyQueue
{
private:
queue<int> qq;
public:
MyQueue()
{
qq = queue<int>();
}
void push(int x)
{
qq.push(x);
}
int pop()
{
int output = qq.front();
qq.pop();
return output;
}
int peek()
{
return qq.front();
}
bool empty()
{
return qq.empty();
}
};
225. Implement Stack using Queues + 232. Implement Queue using Stacks的更多相关文章
- leetcode:Implement Stack using Queues 与 Implement Queue using Stacks
一.Implement Stack using Queues Implement the following operations of a stack using queues. push(x) - ...
- 【LeetCode】232 & 225 - Implement Queue using Stacks & Implement Stack using Queues
232 - Implement Queue using Stacks Implement the following operations of a queue using stacks. push( ...
- 232. Implement Queue using Stacks,225. Implement Stack using Queues
232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...
- leetcode 155. Min Stack 、232. Implement Queue using Stacks 、225. Implement Stack using Queues
155. Min Stack class MinStack { public: /** initialize your data structure here. */ MinStack() { } v ...
- Implement Queue by Two Stacks & Implement Stack using Queues
Implement Queue by Two Stacks Implement the following operations of a queue using stacks. push(x) -- ...
- LeetCode 225 Implement Stack using Queues(用队列来实现栈)(*)
翻译 用队列来实现栈的例如以下操作. push(x) -- 将元素x加入进栈 pop() -- 从栈顶移除元素 top() -- 返回栈顶元素 empty() -- 返回栈是否为空 注意: 你必须使用 ...
- [LeetCode] 225. Implement Stack using Queues 用队列来实现栈
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- Java for LeetCode 225 Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- (easy)LeetCode 225.Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
随机推荐
- 雷林鹏分享:Ruby 面向对象
Ruby 面向对象 Ruby 是纯面向对象的语言,Ruby 中的一切都是以对象的形式出现.Ruby 中的每个值都是一个对象,即使是最原始的东西:字符串.数字,甚至连 true 和 false 都是对象 ...
- error processing package oracle-java8-installer问题解决
ubuntu通过ppa源安装jdk时遇到如下问题: download failedOracle JDK 8 is NOT installed.dpkg: error processing packag ...
- gruntjs开发实例
Grunt是基于Node.js的项目构建工具.它可以自动运行你所设定的任务,如编译less,sass,压缩js,合拼文件等等. (一)安装nodejs环境,Grunt 0.4.x要求Node.js的版 ...
- 本地Jdev Run PG报严重: Socket accept failed错误
严重: Socket accept failed java.net.SocketException: select failed at java.net.PlainSocketImpl.socketA ...
- 处理ListView数据为空的情况
如何处理需要填充的数据为空的情况? ListView及其他继承自AdapterView的类都有一个简便的处理这种情况的方法:setEmptyView(View). 当ListView的Adapter为 ...
- XML——概述
body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...
- zabbix利用python脚本发送钉钉报警
#!/usr/bin/python # -*- coding: utf-8 -*- import requests import json import sys import os headers = ...
- java日常知识点积累
java类型中的普通非static方法 示例代码: package com.lvzhi; /** * Created by lvzhi on 2017/9/3 */ public class MyTh ...
- 【liunx】telnet命令
telnet命令用于登录远程主机,对远程主机进行管理.telnet因为采用明文传送报文,安全性不好,很多Linux服务器都不开放telnet服务,而改用更安全的ssh方式了.但仍然有很多别的系统可能采 ...
- 【java多线程】ConCurrent并发包 - Lock详解
synchronized的缺陷 我们知道,可以利用synchronized关键字来实现共享资源的互斥访问. Java 5在java.util.concurrent.locks包下提供了另一种来实现 ...