232. 用栈实现队列
题目链接:232. 用栈实现队列 - 力扣(LeetCode)
代码随想录讲解:
由于java的栈和队列的底层实现忘得差不多了,所以这一遍直接看题解视频,把饭喂在嘴里,不思考了。
java有自己的栈结构,stack,那么在初始化MyQueue实体对象时,要初始化所有的属性,这种自定义的class类,属性值就放实现下列函数可能用到的所有东西就好,比如这里需要两个栈(一个入栈,一个出栈)。
push( ):直接往stackIn中push就可以了。
pop( ):队列的特征是先进先出,栈的特征是先进后出,所以要实现从队列中弹出元素时,保证其他元素还存在,就要借助另外一个栈,即分为入栈(in)和出栈(out),注意要在有弹出动作时把所有的元素都从入栈放到出栈中,否则顺序就乱了。如果stackOut为空,就要把stakIn中所有的元素都放到出栈中,由于出栈和查看队列首元素时都需要进行这个操作,所以集合成一个方法(dumpStackIn),在出栈为空时,将入栈中所有的元素移动到出栈中。
class MyQueue {
Stack<Integer> stackIn;
Stack<Integer> stackOut;
public MyQueue() {
stackIn = new Stack<>();
stackOut = new Stack<>();
}
public void push(int x) {
stackIn.push(x);
}
public int pop() {
dumpStackIn();
return stackOut.pop();
}
public int peek() {
dumpStackIn();
return stackOut.peek();
}
public boolean empty() {
dumpStackIn();
if (stackOut.isEmpty()) return true;
else return false;
}
public void dumpStackIn() {
if (!stackOut.isEmpty()) return;
while (!stackIn.isEmpty()) {
stackOut.push(stackIn.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. 用队列实现栈
题目链接:225. 用队列实现栈 - 力扣(LeetCode)
用队列来实现栈,同样考虑一下栈和队列的特征,栈先进后出,队列先进先出,不能像上道题一样使用两个队列来模拟,因为无法通过操作两个队列来实现顺序的反转(好像可以,就是将最后一个元素前面的所有元素存进第二个队列,然后将元素弹出)。
只用一个队列如何实现pop:将前面的元素先弹出,再放进队列。
java中有Queue和Deque两种数据结构(二刷补充)
class MyStack {
Queue<Integer> queue;
public MyStack() {
queue = new LinkedList<>();
}
public void push(int x) {
queue.add(x);
}
public int pop() {
dropQueue();
return queue.poll();
}
public int top() {
dropQueue();
int result = queue.poll();
queue.add(result);
return result;
}
public boolean empty() {
return queue.isEmpty();
}
public void dropQueue() {
int size = queue.size();
for(int i = 0; i < size - 1; i++) {
queue.add(queue.poll());
}
}
}
/**
* 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. 有效的括号
虽然能写,但是写出来一段极度不优雅的代码
class Solution {
public boolean isValid(String s) {
Stack<Character> stack = new Stack<>();
if (s.length() == 1) return false;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
// if ((c == ')' && stack.peek() == '(') || (c == ']' && stack.peek() == '[') || (c == '}' && stack.peek() == '{')) {
// stack.pop();
// } else {
// stack.push(c);
// }
if (c == ')') {
if (!stack.isEmpty()) {
if (stack.peek() == '(') {
stack.pop();
continue;
}
}
stack.push(c);
}else if (c == ']') {
if (!stack.isEmpty()) {
if (stack.peek() == '[') {
stack.pop();
continue;
}
}
stack.push(c);
}else if (c == '}') {
if (!stack.isEmpty()) {
if (stack.peek() == '{') {
stack.pop();
continue;
}
}
stack.push(c);
} else {
stack.push(c);
}
}
if (stack.isEmpty()) return true;
return false;
}
}
1047. 删除字符串中的所有相邻重复项
题目链接:1047. 删除字符串中的所有相邻重复项 - 力扣(LeetCode)
难度不高,学习一下StringBuffer的操作
class Solution {
public String removeDuplicates(String s) {
StringBuffer sb = new StringBuffer();
int top = -1;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (top >= 0 && sb.charAt(top) == c) {
sb.deleteCharAt(top);
top--;
} else {
sb.append(c);
top++;
}
}
return sb.toString();
}
}
day10,迟到的打卡,明天继续~