30、用栈实现队列
题目简介:
请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(push
、pop
、peek
、empty
):
实现 MyQueue
类:
void push(int x)
将元素 x 推到队列的末尾int pop()
从队列的开头移除并返回元素int peek()
返回队列开头的元素boolean empty()
如果队列为空,返回true
;否则,返回false
初见思路:
我首先想到的就是两个栈来实现,准备一个空栈2
,准备一个正常栈1
;我的目标就是保证栈1
里的数就是正常的先入先出。怎么保证呢,每次要压入新的数到栈1的时候,我就先将栈1
的数压入到栈2
去,然后将数压入到栈2
的最上面,再把栈2
的数押入到栈1
,这样子新压入的数就自然而然到了栈1
的最底部,这样就实现了后入后出了。
但我写的时候就觉得,肯定还有很多种逻辑来实现这个过程,但我觉得我这个还挺简单的。
class MyQueue {
public:
MyQueue() {
}
void push(int x) {
while(!stack1.empty()){
int temp = stack1.top();
stack1.pop();
stack2.push(temp);
}
stack2.push(x);
while(!stack2.empty()){
int temp = stack2.top();
stack2.pop();
stack1.push(temp);
}
}
int pop() {
int temp =stack1.top();
stack1.pop();
return temp;
}
int peek() {
return stack1.top();
}
bool empty() {
return stack1.empty();
}
private:
stack<int> stack1;
stack<int> stack2;
};
/**
* 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();
*/
31、用队列实现栈
题目简介:
请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通栈的全部四种操作(push
、top
、pop
和 empty
)。
实现 MyStack
类:
void push(int x)
将元素 x 压入栈顶。int pop()
移除并返回栈顶元素。int top()
返回栈顶元素。boolean empty()
如果栈是空的,返回true
;否则,返回false
。
初见思路:
还是先准备两个队列,入队的时候就是正常的入队,但是出队的时候,我就将前面的数全都先入队到另一个空队列,只留下最后的一个,再把这个数给pop出去就好了,这样就实现了后入先出。
class MyStack {
public:
MyStack() {
}
void push(int x) {
if(queue1.empty()){
queue2.push(x);
}else{
queue1.push(x);
}
}
int pop() {
if(queue1.empty()){
while(queue2.size() != 1){
int temp = queue2.front();
queue2.pop();
queue1.push(temp);
}
int temp = queue2.front();
queue2.pop();
return temp;
}else{
while(queue1.size() != 1){
int temp = queue1.front();
queue1.pop();
queue2.push(temp);
}
int temp = queue1.front();
queue1.pop();
return temp;
}
}
int top() {
if(queue1.empty()){
while(queue2.size() != 1){
int temp = queue2.front();
queue2.pop();
queue1.push(temp);
}
int temp = queue2.front();
queue2.pop();
queue1.push(temp);
return temp;
}else{
while(queue1.size() != 1){
int temp = queue1.front();
queue1.pop();
queue2.push(temp);
}
int temp = queue1.front();
queue1.pop();
queue2.push(temp);
return temp;
}
}
bool empty() {
return queue1.empty() && queue2.empty();
}
private:
queue<int> queue1;
queue<int> queue2;
};
/**
* 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();
*/
32、括号匹配
题目简介:
给定一个只包括 '('
,')'
,'{'
,'}'
,'['
,']'
的字符串 s
,判断字符串是否有效。
有效字符串需满足:
- 左括号必须用相同类型的右括号闭合。
- 左括号必须以正确的顺序闭合。
- 每个右括号都有一个对应的相同类型的左括号。
初见思路:
我准备三个栈呗,一个栈检查一种括号,一个左括号,我就压入一个左括号,遇到一个右括号我就出一个左括号,然后我检查三个栈是不是都是空的就好了。
我去试试。
class Solution {
public:
bool isValid(string s) {
stack<char> stack1;//检查{}
stack<char> stack2;//检查()
stack<char> stack3;//检查[]
int n = s.size();
for(auto a : s){
if(a=='{'){
stack1.push('{');
}
else if(a=='('){
stack1.push('(');
}
else if(a=='['){
stack1.push('[');
}
else if(a=='}'){
if(!stack1.empty()){
char temp = stack1.top();
stack1.pop();
if(temp == '{'){
continue;
}
else{
return false;
}
}
else{
return false;
}
}
else if(a==')'){
if(!stack1.empty()){
char temp = stack1.top();
stack1.pop();
if(temp == '('){
continue;
}
else{
return false;
}
}
else{
return false;
}
}
else if(a==']'){
if(!stack1.empty()){
char temp = stack1.top();
stack1.pop();
if(temp == '['){
continue;
}
else{
return false;
}
}
else{
return false;
}
}
}
return stack1.empty() && stack2.empty() && stack3.empty();
}
};
一般还不行啊,还得考虑顺序啊,一般还不行。嗯,还要按顺序pop
出去,检查是什么。
33、删除字符串中所有相邻重复项
题目简介:
给出由小写字母组成的字符串 S,重复项删除操作会选择两个相邻且相同的字母,并删除它们。
在 S 上反复执行重复项删除操作,直到无法继续删除。
在完成所有重复项删除操作后返回最终的字符串。答案保证唯一
初见思路:
我没啥思路,可恶,我钻到牛角尖去了,想着怎么去修改原字符串种的元素,结果完全不知道该怎么记录,也不知道怎么删除。
算法思路:
原来栈还有这种使用方法啊,实在是妙。
class Solution {
public:
string removeDuplicates(string s) {
stack<char> stack1;
for(auto a : s){
if(!stack1.empty()){
char temp = stack1.top();
if(temp == a){
stack1.pop();
}else{
stack1.push(a);
}
}else{
stack1.push(a);
}
}
string res;
while(!stack1.empty()){
char temp = stack1.top();
stack1.pop();
res.push_back(temp);
}
int left =0;
int right = res.size()-1;
while(left < right){
swap(res[left],res[right]);
left++;
right--;
}
return res;
}
};