今日任务:
232.用栈实现队列
225. 用队列实现栈
20. 有效的括号
1047. 删除字符串中的所有相邻重复项
232.用栈实现队列
题目链接:. - 力扣(LeetCode)
class MyQueue {
private Stack<Integer> stack;
public MyQueue() {
stack = new Stack<Integer>();
}
public void push(int x) {
stack.push(x);
}
public int pop() {
Stack<Integer> candidateStack = new Stack<Integer>();
int res;
while (!stack.isEmpty()) {
candidateStack.push(stack.pop());
}
res = candidateStack.pop();
while (!candidateStack.isEmpty()) {
stack.push(candidateStack.pop());
}
return res;
}
public int peek() {
Stack<Integer> candidateStack = new Stack<Integer>();
int res;
while (!stack.isEmpty()) {
candidateStack.push(stack.pop());
}
res = candidateStack.peek();
while (!candidateStack.isEmpty()) {
stack.push(candidateStack.pop());
}
return res;
}
public boolean empty() {
return stack.isEmpty();
}
// @Test
// public void test() {
// LeetCode232 myQueue = new LeetCode232();
// myQueue.push(1);
// myQueue.push(2);
// System.out.println("The peek output is " + myQueue.peek());
// System.out.println("The pop output is " + myQueue.pop());
// System.out.println("The empty output is " + myQueue.empty());
// }
}
/**
* 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. 用队列实现栈
题目链接:. - 力扣(LeetCode)
class MyStack {
private Queue<Integer> queue;
public MyStack() {
queue = new LinkedList<>();
}
public void push(int x) {
queue.offer(x);
}
public int pop() {
Queue<Integer> tempQueue = new LinkedList<>();
int size = queue.size();
for (int i = 0; i < size - 1; i++) {
tempQueue.offer(queue.poll());
}
int res = queue.poll();
while (!tempQueue.isEmpty()) {
queue.offer(tempQueue.poll());
}
return res;
}
public int top() {
Queue<Integer> tempQueue = new LinkedList<>();
int size = queue.size();
for (int i = 0; i < size - 1; i++) {
tempQueue.offer(queue.poll());
}
int res = queue.peek();
tempQueue.offer(queue.poll());
while (!tempQueue.isEmpty()) {
queue.offer(tempQueue.poll());
}
return res;
}
public boolean empty() {
return queue.isEmpty();
}
// @Test
// public void test() {
// LeetCode225 stack = new LeetCode225();
// stack.push(1);
// stack.push(2);
// System.out.println("The top output is " + stack.top());
// System.out.println("The pop output is " + stack.pop());
// System.out.println("The empty output is " + stack.empty());
// }
}
/**
* 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. 有效的括号
题目链接:. - 力扣(LeetCode)
class Solution {
public boolean isValid(String s) {
Stack<Character> stack = new Stack();
String leftStr = "({[";
String rightStr = ")}]";
for (int i = 0; i < s.length(); i++) {
if (leftStr.indexOf(s.charAt(i)) != -1){
stack.push(s.charAt(i));
} else if (!stack.isEmpty()) {
if (rightStr.indexOf(s.charAt(i)) != leftStr.indexOf(stack.pop())) {
return false;
}
} else {
return false;
}
}
return stack.isEmpty() == true ? true : false;
}
}
1047. 删除字符串中的所有相邻重复项
题目链接:. - 力扣(LeetCode)
class Solution {
public String removeDuplicates(String s) {
Stack<Character> stack = new Stack<Character>();
for (int i = 0; i < s.length(); i++) {
if(!stack.isEmpty()){
char ch = stack.peek();
if (ch == s.charAt(i)) {
stack.pop();
} else {
stack.push(s.charAt(i));
}
} else {
stack.push(s.charAt(i));
}
}
StringBuilder res = new StringBuilder();
while (!stack.isEmpty()) {
res.append(stack.pop());
}
return res.reverse().toString();
}
}