题目:用栈实现队列
题目:232.用栈实现队列
文档链接:代码随想录
视频链接:栈的基本操作! | LeetCode:232.用栈实现队列
请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(push、pop、peek、empty):
实现 MyQueue 类:
- void push(int x) 将元素 x 推到队列的末尾
- int pop() 从队列的开头移除并返回元素
- int peek() 返回队列开头的元素
- boolean empty() 如果队列为空,返回 true ;否则,返回 false
示例 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
思路
用栈实现队列,队列是先近先出,而栈是先进后出,那使用两个栈一个先存,然后取栈桢依次再放入第二个栈,相当于反反得正
用时 20min
代码
class MyQueue {
private Stack<Integer> stack;
private Stack<Integer> stack1;
public MyQueue() {
this.stack = new Stack();
this.stack1 = new Stack();
}
public void push(int x) {
stack.push(x);
}
public int pop() {
if(!stack1.isEmpty()) return stack1.pop();
while(!stack.isEmpty()) {
stack1.push(stack.pop());
}
return stack1.pop();
}
public int peek() {
if(!stack1.isEmpty()) return stack1.peek();
while(!stack.isEmpty()) {
stack1.push(stack.pop());
}
return stack1.peek();
}
public boolean empty() {
return stack1.isEmpty() && stack.isEmpty();
}
}
看完代码随想录之后的想法&今日收获
思路一致~
题目 用队列实现栈
题目 225. 用队列实现栈
文档链接:代码随想录
视频链接:队列的基本操作! | LeetCode:225. 用队列实现栈
请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通栈的全部四种操作(push、top、pop 和 empty)。
实现 MyStack 类:
- void push(int x) 将元素 x 压入栈顶。
- int pop() 移除并返回栈顶元素。
- int top() 返回栈顶元素。
- boolean empty() 如果栈是空的,返回 true ;否则,返回 false 。
示例1:
输入:
["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: 看题目主要是实现栈的pop、peek操作,那么其实就是获取最后添加的元素,队列是先进先出,那么移除队列直到只剩下最后一个元素即可。但是两个方法的时间复杂度都是O(n),在lc上执行耗时巨高
思路2: 用队列实现栈,队列永远是先进先出,不能做到像栈那样两个逆序的骚操作,栈的特点的是先进后出,那就使用一个辅助队列用来存放入的元素,然后再将存储队列中的元素加入到辅助队列中,这样就相当于实现了栈的先进后出、后进先出(因为数据结构还是队列,从队列头获取元素,后加入的元素在队列头)
用时 20min
代码
//思路1
class MyStack {
private Queue<Integer> queue;
private Queue<Integer> queue1;
public MyStack() {
queue = new LinkedList<>();
queue1 = new LinkedList<>();
}
public void push(int x) {
queue.add(x);
}
public int pop() {
queue1 = new LinkedList<>();
int res = 0;
if(queue.size() == 1) {
return queue.poll();
}
while(queue.size() != 1) {
res = queue.poll();
queue1.add(res);
}
res = queue.poll();
queue = queue1;
return res;
}
public int top() {
int res = queue.peek();
queue1 = new LinkedList<>();
while(queue.size() != 0) {
res = queue.poll();
queue1.add(res);
}
queue = queue1;
return res;
}
public boolean empty() {
return queue.isEmpty() && queue1.isEmpty();
}
}
//思路2
class MyStack {
private Queue<Integer> queue1; //存储元素,当作栈
private Queue<Integer> queue2; //临时放入元素,作为实现栈的辅助容器
public MyStack() {
queue1 = new LinkedList<>();
queue2 = new LinkedList<>();
}
public void push(int x) {
//先存入queue2中
queue2.add(x);
//将queue1中的元素加入到2中,实现栈的后进先出
while(!queue1.isEmpty()) {
queue2.add(queue1.poll());
}
Queue<Integer> tempQueue = queue2;
queue2 = queue1;
queue1 = tempQueue;
}
public int pop() {
return queue1.poll();
}
public int top() {
return queue1.peek();
}
public boolean empty() {
return queue1.isEmpty();
}
}
看完代码随想录之后的想法&今日收获
恨自己想不到优秀思路,哎
题目 有效的数字
题目 20. 有效的括号
文档链接:代码随想录
视频链接:栈的拿手好戏!| LeetCode:20. 有效的括号
给定一个只包括 ‘(’,‘)’,‘{’,‘}’,‘[’,‘]’ 的字符串 s ,判断字符串是否有效。
有效字符串需满足:
- 左括号必须用相同类型的右括号闭合
- 左括号必须以正确的顺序闭合
- 每个右括号都有一个对应的相同类型的左括号
示例 1:
输入:s = "()"
输出:true
示例 2:
输入:s = "()[]{}"
输出:true
思路
用栈来实现,分情况讨论:如果遇到
( { [入栈,反之判断栈顶元素是否是对应的括号,最后判断栈是否为空
用时 10min
代码
class Solution {
public boolean isValid(String s) {
Stack<Character> stack = new Stack<>();
for(Character ch : s.toCharArray()) {
if(!stack.isEmpty()) {
if(ch == '(' || ch == '[' || ch == '{') {
stack.add(ch);
continue;
}
if(ch == ')') {
if(stack.peek() != '(') return false;
stack.pop();
}
if(ch == ']') {
if(stack.peek() != '[') return false;
stack.pop();
}
if(ch == '}') {
if(stack.peek() != '{') return false;
stack.pop();
}
} else {
if(ch == ')' || ch == ']' || ch == '}') return false;
stack.add(ch);
}
}
return stack.isEmpty();
}
}
看完代码随想录之后的想法&今日收获
思路一致~ 属于简单题
题目 删除字符串中的所有相邻重复项
题目 1047. 删除字符串中的所有相邻重复项
文档链接:代码随想录
视频链接:栈的好戏还要继续!| LeetCode:1047. 删除字符串中的所有相邻重复项
给出由小写字母组成的字符串 s,重复项删除操作会选择两个相邻且相同的字母,并删除它们。
在 s 上反复执行重复项删除操作,直到无法继续删除。
在完成所有重复项删除操作后返回最终的字符串。答案保证唯一。
示例1:
输入:"abbaca"
输出:"ca"
解释:
例如,在 "abbaca" 中,我们可以删除 "bb" 由于两字母相邻且相同,这是此时唯一可以执行删除操作的重复项。之后我们得到字符串 "aaca",其中又只有 "aa" 可以执行重复项删除操作,所以最后的字符串为 "ca"。
思路
遍历字符串判断当前栈顶元素是否一致,如果一致则出栈,最后返回结果
用时 15min
代码
class Solution {
public String removeDuplicates(String s) {
Deque<Character> stack = new ArrayDeque<>();
for(Character ch : s.toCharArray()) {
if(!stack.isEmpty()) {
if(stack.peek() == ch) {
stack.pop();
} else {
stack.push(ch);
}
} else {
stack.push(ch);
}
}
StringBuilder sb = new StringBuilder();
while(!stack.isEmpty()) {
//System.out.println(stack.pop());
sb.append(stack.removeLast());
}
return sb.toString();
}
}
看完代码随想录之后的想法&今日收获
easy题目~

200

被折叠的 条评论
为什么被折叠?



