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() {
if(stackOut.isEmpty()){
while (!stackIn.isEmpty()){
int temp = stackIn.pop();
stackOut.push(temp);
}
}
return stackOut.pop();
}
public int peek() {
int ans = pop();
stackOut.push(ans);
return ans;
}
public boolean empty() {
if(stackOut.isEmpty() && stackIn.isEmpty()){
return true;
}
return false;
}
}
class MyStack {
Queue<Integer> q1;
Queue<Integer> q2;
public MyStack() {
q1 = new LinkedList<>();
q2 = new LinkedList<>();
}
public void push(int x) {
q1.add(x);
}
public int pop() {
int size = q1.size();
while(size>1){
q2.add(q1.poll());
size--;
}
int ans = q1.poll();
while (!q2.isEmpty()){
q1.add(q2.poll());
}
return ans;
}
public int top() {
int ans = pop();
q1.add(ans);
return ans;
}
public boolean empty() {
if(q1.isEmpty()){
return true;
}
return false;
}
}
class Solution {
public boolean isValid(String s) {
Stack<Character> stack = new Stack<>();
for(int i=0; i<s.length();i++){
char c = s.charAt(i);
if(c == '('){
stack.push(')');
}else if(c == '{'){
stack.push('}');
}else if(c == '['){
stack.push(']');
}else if(stack.isEmpty() || stack.peek()!=c){
return false;
}else{
stack.pop();
}
}
return stack.isEmpty();
}
}
class Solution {
public String removeDuplicates(String s) {
int slow = 0;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if(slow == 0){
sb.append(c);
slow++;
}else if(c != sb.charAt(slow-1)){
sb.append(c);
slow++;
}else{
sb.deleteCharAt(slow-1);
slow--;
}
}
return sb.toString();
}
}
方法二:栈
class Solution {
public String removeDuplicates(String s) {
Deque<Character> stack = new LinkedList<>();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if(stack.isEmpty() || stack.peek()!=c){
stack.push(c);
}else {
stack.pop();
}
}
String str = "";
while (!stack.isEmpty()) {
str = stack.pop() + str;
}
return str;
}
}
class Solution {
public int evalRPN(String[] tokens) {
//如果把运算符作为中间节点,将表达式按照中序遍历的规则转换成一个二叉树,
//那么逆波兰表达式就是用后序遍历的方式把二叉树序列化的结果。
Deque<Integer> stack = new LinkedList<>();
for (String s:tokens) {
if(s.equals("+")){
stack.push(stack.pop()+stack.pop());
}else if(s.equals("-")){
stack.push(-stack.pop()+stack.pop());
}else if(s.equals("*")){
stack.push(stack.pop()*stack.pop());
}else if(s.equals("/")){
int temp = stack.pop();
stack.push(stack.pop()/temp);
}else{
stack.push(Integer.valueOf(s));
}
}
return stack.pop();
}
}
class Solution {
public int[] maxSlidingWindow(int[] nums, int k) {
//不能使用双指针法,因为如果当前窗口的最大值不在下一个窗口,
//则下一个窗口还需重新寻找最大值
//因此需要维护一个最大值队列,该队列是递减的
//若滑出元素等于队头元素,则需将队头弹出
//若滑入元素大于队尾元素,则需将队尾弹出
//将滑入元素入队
int[] ans = new int[nums.length-k+1];
Deque<Integer> queue = new ArrayDeque<>();
for (int i = 0; i < nums.length; i++) {
while (!queue.isEmpty() && nums[i]>queue.peekLast()){
queue.pollLast();
}
queue.add(nums[i]);
if(i-k+1>=0){
ans[i-k+1] = queue.peek();
if(nums[i-k+1] == queue.peek()){
queue.poll();
}
}
}
return ans;
}
}
class Solution {
public int[] topKFrequent(int[] nums, int k) {
//可以使用map来存储每个元素及其对应出现的频率
//寻找频率前k个元素,可以使用优先级队列PriorityQueue
//PriorityQueue底层使用了堆的数据结构,满足FIFO原则
//队头元素只可能是队列中优先级最高的元素
//所以这里使用小根堆方式保留出现频率前k个高的元素
PriorityQueue<int[]> pq = new PriorityQueue<>(new compareArr());
HashMap<Integer,Integer> map = new HashMap<>();
int[] ans = new int[k];
for (int i = 0; i < nums.length; i++) {
map.put(nums[i],map.getOrDefault(nums[i],0)+1);
}
for(Map.Entry<Integer,Integer> entry:map.entrySet()){
int[] temp = new int[2];
temp[0] = entry.getKey();
temp[1] = entry.getValue();
pq.offer(temp);
}
for (int i = pq.size()-1; i >= 0; i--) {
if(i>=k){
pq.poll();
}else{
ans[i] = pq.poll()[0];
}
}
return ans;
}
}
class compareArr implements Comparator<int[]>{
@Override
public int compare(int[] m,int[] n) {
return m[1]-n[1];
}
}