232.用栈实现队列
请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(
push、pop、peek、empty):实现
MyQueue类:
void push(int x)将元素 x 推到队列的末尾int pop()从队列的开头移除并返回元素int peek()返回队列开头的元素boolean empty()如果队列为空,返回true;否则,返回false
class MyQueue {
private Stack<Integer> inStack;//输入栈
private Stack<Integer> outStack;//输出栈
public MyQueue() {
inStack = new Stack<>();
outStack = new Stack<>();
}
public void push(int x) {//压入输入栈
inStack.push(x);
}
public int pop() {//从输出栈弹出
dumpToOutStack();
return outStack.pop();
}
public int peek() {//读取输出栈顶元素
dumpToOutStack();
return outStack.peek();
}
public boolean empty() {//两栈都为空则队列为空
return (inStack.empty() && outStack.empty());
}
public void dumpToOutStack() {//将输出栈中元素存入输出栈中
if(!outStack.empty())
return;
while(!inStack.empty()) {
outStack.push(inStack.pop());
}
}
}
/**
* 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();
* boolean param_4 = obj.empty();
*/
- 队列:先入先出
- 栈:先入后出
- 通过栈实现队列只需要将元素全部压入栈中后逆置栈即可完成先入先出,最先压入栈中元素逆置后位于栈顶
225.用队列实现栈
请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通栈的全部四种操作(
push、top、pop和empty)。实现
MyStack类:
void push(int x)将元素 x 压入栈顶。int pop()移除并返回栈顶元素。int top()返回栈顶元素。boolean empty()如果栈是空的,返回true;否则,返回false。
class MyStack {
private Queue<Integer> queue;
public MyStack() {
queue = new LinkedList<>();
}
public void push(int x) {
queue.add(x);
int size = queue.size();
while(size > 1) {//将新加入元素移动到队列头
queue.add(queue.poll());
size--;
}
}
public int pop() {
return queue.poll();
}
public int top() {
return queue.peek();
}
public boolean empty() {
return queue.isEmpty();
}
}
/**
* 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();
* boolean param_4 = obj.empty();
*/
20.有效的括号
给定一个只包括
'(',')','{','}','[',']'的字符串s,判断字符串是否有效。有效字符串需满足:
- 左括号必须用相同类型的右括号闭合。
- 左括号必须以正确的顺序闭合。
- 每个右括号都有一个对应的相同类型的左括号。
class Solution {
public boolean isValid(String s) {
Stack<Character> stack = new Stack<>();
for(int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
if(ch == '(')
stack.push(')');
else if(ch == '[')
stack.push(']');
else if(ch == '{')
stack.push('}');
else if(!stack.empty() && ch == stack.peek())
//检查右括号是否与栈顶元素相等
stack.pop();
else
return false;
}
return stack.empty();
}
}
1047.删除字符串中的所有相邻重复项
给出由小写字母组成的字符串
s,重复项删除操作会选择两个相邻且相同的字母,并删除它们。在
s上反复执行重复项删除操作,直到无法继续删除。在完成所有重复项删除操作后返回最终的字符串。答案保证唯一。
//使用栈
class Solution {
public String removeDuplicates(String s) {
StringBuilder result = new StringBuilder();//将结果作为栈
int top = -1;//维护一个栈顶指针,初始值为-1
for(int i = 0; i < s.length(); i++) {
if(result.length() > 0 && result.charAt(top) == s.charAt(i)) {
//与栈顶元素相等,弹出栈顶元素,检查下一元素
result.deleteCharAt(top);
top--;
continue;
}
result.append(s.charAt(i));
top++;
}
return result.toString();
}
}
- 可以使用双指针解决
class Solution {
public String removeDuplicates(String s) {
char[] chars = s.toCharArray();
int fast = 0;//遍历元素
int slow = 0;//修改元素
while(fast < chars.length) {
chars[slow] = chars[fast];
if(slow > 0 && chars[slow] == chars[slow - 1]) {
//相邻两元素相同,回退慢指针
slow--;
}else
slow++;
fast++;
}
return new String(chars, 0, slow);
}
}
3036

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



