day10:第五章 栈与队列part01|232.用栈实现队列|225. 用队列实现栈|20. 有效的括号|1047. 删除字符串中的所有相邻重复项
栈:用stack
-
进栈(push), 出栈(pop)
queue:用linkedList: offer(). poll()
232.用栈实现队列
class MyQueue { //用2个stack to implement the queue. //就是用第二个栈存数据。 //为什么不用把out的数据再放回去?因为check了out不为空,就可以在out那里继续拿,in的可以一直存着,直到out为空,再把in的都转去out 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() { reverseToOut(); return stackOut.pop(); } public int peek() { reverseToOut(); return stackOut.peek(); } public boolean empty() { return stackIn.isEmpty() && stackOut.isEmpty(); } private void reverseToOut(){ 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. 用队列实现栈
Two queue
class MyStack { // two queue. b is temporary evert time x in, move all the element to b, add x to a, // and move all the elements from b to a, so the first element in the a is the top in the stack Queue<Integer> a; Queue<Integer> b; public MyStack() { a = new LinkedList<>(); b = new LinkedList<>(); } public void push(int x) { while(!a.isEmpty()){ b.offer(a.poll()); } a.offer(x); while(!b.isEmpty()){ a.offer(b.poll()); } } public int pop() { return a.poll(); } public int top() { return a.peek(); } public boolean empty() { return a.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(); */
one queue
class MyStack { // one queue: everty time when add the new element just add new and add the front to back. Queue<Integer> a; int size; public MyStack() { a = new LinkedList<>(); size = 0; } public void push(int x) { a.offer(x); size++; int reduce = size; while (reduce-- > 1){ a.offer(a.poll()); } } public int pop() { return a.poll(); } public int top() { return a.peek(); } public boolean empty() { return a.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. 有效的括号
class Solution { public boolean isValid(String s) { //如果是左边的,就把右边的放入queue,如果不是左边的就对比,对比不对或者为空,return false //其他的都是真,最后检查这个queue是不是空。 //第一想法就是用map跟queue,但是这种做法更好。 if(s.length() % 2 != 0) return false; Deque<Character> deque = new LinkedList<>(); char ch; for(int i = 0; i < s.length(); i++){ ch = s.charAt(i); if(ch == '('){ deque.push(')'); } else if(ch == '['){ deque.push(']'); } else if(ch == '{') { deque.push('}'); } else if(deque.isEmpty() || deque.peek()!= ch ){ return false; }else{ deque.pop(); } } return deque.isEmpty(); } }
1047. 删除字符串中的所有相邻重复项
class Solution { public String removeDuplicates(String s) { if (s == null) return null; //直接存入stack然后对比。然后翻转string Stack<Character> stack = new Stack<>(); for(int i = 0; i < s.length(); i++){ char ch = s.charAt(i); if(!stack.isEmpty()){ char tem = stack.peek(); if(tem == ch){ stack.pop(); }else{ stack.push(ch); } }else { stack.push(ch); } } String str = ""; while(!stack.isEmpty()){ str = stack.pop() + str; } return str; } }