zoukankan      html  css  js  c++  java
  • 【Leetcode记录】栈与队列

    想到未来如果换工作/run出去找工作也需要算法

    计划跟着代码随想录开始刷题,但是解题过程中发现代码随想录主要用C++,Java的代码没有特别的友好,

    于是记录下自己做不出的题目以及微调过的代码

    栈与队列

    代码随想录题解

    150. 逆波兰表达式求值

    class Solution {
        public int evalRPN(String[] tokens) {
             Stack<Integer> stack = new Stack();
             for(String token: tokens){
                    char c = token.charAt(0);
                    if(!isOpe(token)){
                        stack.push(Integer.valueOf(token));
                    }else if(c == '+'){
                        stack.push(stack.pop() + stack.pop());
                    }else if(c == '-'){
                        stack.push(- stack.pop() + stack.pop());
                        // 坑一: 谁减谁,谁除谁
                    }else if(c == '*'){
                        stack.push(stack.pop() * stack.pop());
                    }else if(c == '/'){
                        int num1 = stack.pop();
                        int num2 = stack.pop();
                    stack.push( num2/num1);
                    }
             }
             return stack.pop();
        }
        private boolean isOpe(String s){
            return s.length() == 1 & (s.charAt(0) < '0' || s.charAt(0) >'9');
            // 坑二:如何判断是否是一个普通的数字,比较麻烦,可能要用正则来判断
            //但是可以通过判断符号,符号仅占一位,
            //且符号可以简单地计算
        }
    }
    

    347.前 K 个高频元素

    大顶堆

    class Solution {
        public int[] topKFrequent(int[] nums, int k) {
            Map<Integer, Integer> map = new HashMap<>();
            for (int n : nums){
                map.put(n, map.getOrDefault(n, 0) + 1);
            }
            PriorityQueue<Map.Entry<Integer, Integer>> queue = new PriorityQueue<>((e1, e2) -> e2.getValue() - e1.getValue());
            //建大顶堆,塞进去全部,然后弹出来前k个即可
            //建小顶堆的思路反而是,塞进去全部,把小的弹出来,留k个大的,但显然大顶堆比较麻烦,需要逐个取出来剩下的
            queue.addAll(map.entrySet());
            int[] ans = new int[k];
            for (int i = 0; i < k && !queue.isEmpty(); ++i){
                ans[i] = queue.poll().getKey();
            }
            return ans;
        }
    }
    

    小顶堆

    class Solution {
        public int[] topKFrequent(int[] nums, int k) {
            Map<Integer, Integer> map = new HashMap<>();
            for (int n : nums){
                map.put(n, map.getOrDefault(n, 0) + 1);
            }
            PriorityQueue<Map.Entry<Integer, Integer>> queue = new PriorityQueue<>((e1,e2) -> e1.getValue() - e2.getValue());
            //建大顶堆,塞进去全部,然后弹出来前k个即可
            //建小顶堆的思路反而是,塞进去全部,把小的弹出来,留k个大的,但显然大顶堆比较麻烦,需要逐个取出来剩下的
            queue.addAll(map.entrySet());
            int[] ans = new int[k];
            while(queue.size()>k){
                queue.poll();
            }
            for(int i = 0 ; i < k ;i++){
                ans[i] = queue.poll().getKey();
            }
            return ans;
        }
    }
    
  • 相关阅读:
    php数组转换成js可用的数组的两种方式
    常用正则表达式--------------[拿把小刀,强大自己]
    AngularJs 相应回车事件
    常见的关系型数据库和非关系型数据库及其区别
    CMDB资产采集
    GB和GiB的区别
    python枚举详解
    python保留两位小数
    详解TCP三握四挥
    npm run dev 和 npm run serve
  • 原文地址:https://www.cnblogs.com/buzhouke/p/15785909.html
Copyright © 2011-2022 走看看