秋招算法刷题10(栈和队列)

0509

232.用栈实现队列

class MyQueue {
        Deque<Integer> inStack;
        Deque<Integer> outStack;

        public MyQueue() {
            inStack = new ArrayDeque<Integer>();
            outStack = new ArrayDeque<Integer>();
        }

        public void push(int x) {
            inStack.push(x);
        }

        public int pop() {
            if (outStack.isEmpty()) {
                in2Out();
            }
            return outStack.pop();
        }

        public int peek() {
            if (outStack.isEmpty()) {
                in2Out();
            }
            return outStack.peek();
        }

        public boolean empty() {
            return inStack.isEmpty() && outStack.isEmpty();
        }

        private void in2Out() {
            while (!inStack.isEmpty()) {
                outStack.push(inStack.pop());
            }
        }
    }

225. 用队列实现栈


20.有效的括号

class Solution {
    private static final Map<Character,Character> map=new HashMap<Character,Character>(){{
        put('{','}');
        put('(',')');
        put('[',']');
        put('?','?');
    }};
    public boolean isValid(String s) {
        if(s.length()>0&&!map.containsKey(s.charAt(0))) return false;
        LinkedList<Character> stack=new LinkedList<Character>() {{add('?');}};
        for(Character c:s.toCharArray()){
            if(map.containsKey(c)) stack.addLast(c);
            else if(map.get(stack.removeLast()) !=c) return false;

        }
        return stack.size()==1;
    }
}

0510

1047 删除字符串中的所有相邻重复项

在这里插入图片描述

public String removeDuplicates(String s) {
        char[] s1=s.toCharArray();
        int top=-1;
        for(int i=0;i<s.length();i++){
            if(top==-1||s1[top]!=s1[i]){
                s1[++top]=s1[i];
            }else{
                top--;
            }
        }
        return String.valueOf(s1,0,top+1);
    }

150.逆波兰表达式求值

在这里插入图片描述

public int evalRPN(String[] tokens) {
        Stack<Integer> numStack=new Stack<>();
        Integer op1,op2;
        for(String s:tokens){
            switch(s){
                case "+":
                    op2=numStack.pop();
                    op1=numStack.pop();
                    numStack.push(op1+op2);
                    break;
                case "-":
                    op2=numStack.pop();
                    op1=numStack.pop();
                    numStack.push(op1-op2);
                    break;
                case "*":
                    op2=numStack.pop();
                    op1=numStack.pop();
                    numStack.push(op1*op2);
                    break;
                case "/":
                    op2=numStack.pop();
                    op1=numStack.pop();
                    numStack.push(op1/op2);
                    break;
                default:
                    numStack.push(Integer.valueOf(s));
                    break;
            }
        }
        return numStack.pop();
    }

347.前k个高频元素

在这里插入图片描述

public int[] topKFrequent(int[] nums, int k) {
        // 优先级队列,为了避免复杂 api 操作,pq 存储数组
        // lambda 表达式设置优先级队列从大到小存储 o1 - o2 为从小到大,o2 - o1 反之
        PriorityQueue<int[]> pq = new PriorityQueue<>((o1, o2) -> o1[1] - o2[1]);
        int[] res = new int[k]; // 答案数组为 k 个元素
        Map<Integer, Integer> map = new HashMap<>(); // 记录元素出现次数
        for (int num : nums) map.put(num, map.getOrDefault(num, 0) + 1);
        for (var x : map.entrySet()) { // entrySet 获取 k-v Set 集合
            // 将 kv 转化成数组
            int[] tmp = new int[2];
            tmp[0] = x.getKey();
            tmp[1] = x.getValue();
            pq.offer(tmp);
            // 下面的代码是根据小根堆实现的,我只保留优先队列的最后的k个,只要超出了k我就将最小的弹出,剩余的k个就是答案
            if(pq.size() > k) {
                pq.poll();
            }
        }
        for (int i = 0; i < k; i++) {
            res[i] = pq.poll()[0]; // 获取优先队列里的元素
        }
        return res;
    }
  • 7
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值