232 用栈实现队列
栈后进先出 队列先进先出 两个栈才能实现队列 入栈inS是接受新插入队列的元素 出栈outS是返回队首元素的操作 pop和peek操作先去出栈outS中找,如果栈空, 则将入栈的全部元素按栈的顺序压入出栈
class MyQueue {
Deque<Integer> inStack;
Deque<Integer> outStack;
public MyQueue() {
inStack = new ArrayDeque<>();
outStack = new ArrayDeque<>();
}
public void push(int x) {
inStack.push(x);
}
public int pop() {
if (outStack.isEmpty()) {
inToOut();
}
return outStack.pop();
}
public int peek() {
if (outStack.isEmpty()) {
inToOut();
}
return outStack.peek();
}
public boolean empty() {
return inStack.isEmpty() && outStack.isEmpty();
}
private void inToOut() {
while (!inStack.isEmpty()) {
outStack.push(inStack.pop());
}
}
}
225 用队列实现栈
单个队列实现栈 push只需正常入队即可 pop操作(获取栈顶并移除): 获取队列长度,将元素依次出队入队,直到末尾,将末尾出队即为栈顶元素 top操作(获取栈顶但不移除元素): 获取队列长度,将元素依次出队入队,直到末尾,将末尾出队并保存结果,将结果入队,返回即可
class MyStack {
Queue<Integer> queue;
public MyStack() {
queue = new LinkedList<>();
}
public void push(int x) {
queue.offer(x);
}
public int pop() {
int queueSize = queue.size();
for (int i = 1; i < queueSize; i++) {
int temp = queue.poll();
queue.offer(temp);
}
return queue.poll();
}
public int top() {
int queueSize = queue.size();
for (int i = 1; i < queueSize; i++) {
int temp = queue.poll();
queue.offer(temp);
}
int result = queue.poll();
queue.offer(result);
return result;
}
public boolean empty() {
return queue.isEmpty();
}
}
20 有效的括号
public boolean isValid(String s) {
Deque<Character> stringDeque = new ArrayDeque<>();
int length = s.length();
if (length == 1) {
return false;
}
for (int i = 0; i < length; i++) {
Character temp = s.charAt(i);
if (temp == '(' || temp == '{' || temp == '[') {
stringDeque.push(temp);
continue;
} else if (temp.equals(')')) {
if (stringDeque.isEmpty()) {
return false;
}
char topChar = stringDeque.pop();
if (topChar != '(') {
return false;
}
} else if (temp == '}') {
if (stringDeque.isEmpty()) {
return false;
}
char topChar = stringDeque.pop();
if (topChar != '{') {
return false;
}
} else if (temp == ']') {
if (stringDeque.isEmpty()) {
return false;
}
char topChar = stringDeque.pop();
if (topChar != '[') {
return false;
}
}
}
if (stringDeque.isEmpty()) {
return true;
}
return false;
}
1047 删除字符串中的所有相邻重复项
public String removeDuplicates(String s) {
Deque<Character> stack = new ArrayDeque<>();
int length = s.length();
if (length == 1) {
return s;
}
//如果栈不空,则top,判断当前元素是否相同,
//相同则再pop,不同则push
for (int i = 0; i < length; i++){
if (!stack.isEmpty()){
Character tempStackChar = stack.peek();
Character tempStringChar = s.charAt(i);
if (tempStackChar.equals(tempStringChar)) {
stack.pop();
continue;
} else {
stack.push(tempStringChar);
continue;
}
}
stack.push(s.charAt(i));
}
StringBuilder sb = new StringBuilder();
while (!stack.isEmpty()) {
sb.append(stack.pop());
}
return sb.reverse().toString();
}
优化写法
public String removeDuplicates(String s) {
Deque<Character> stack = new ArrayDeque<>();
int length = s.length();
if (length == 1) {
return s;
}
//如果栈不空,则top,判断当前元素是否相同,
//相同则再pop,不同则push
for (int i = 0; i < length; i++) {
Character tempStringChar = s.charAt(i);
if (stack.isEmpty() || !stack.peek().equals(tempStringChar)) {
stack.push(tempStringChar);
} else {
stack.pop();
}
}
StringBuilder sb = new StringBuilder();
while (!stack.isEmpty()) {
sb.append(stack.pop());
}
return sb.reverse().toString();
}