Leetcode刷题 2021.03.09

Leetcode440 字典序的第K小数字

给定整数 n 和 k,找到 1 到 n 中字典序第 k 小的数字。

唉,开始春招面试了以后真的没什么时间刷算法题了。只能先记录一下了,等后面有空的时候再回来看看吧。(字节常考题,多回来看看)

class Solution {
    public int findKthNumber(int n, int k) {
        int cur = 1;
        int prefix = 1;
        while (cur < k){
            int count = helper(n, prefix);
            if (cur + count > k){
                prefix *= 10;
                cur++;
            }else{
                prefix++;
                cur += count;
            }
        }
        return prefix;
    }

    private int helper(int n, int prefix){
        long cur = prefix;
        long next = cur + 1;
        int count = 0;
        while (cur <= n){
            count += Math.min(n + 1, next) - cur;

            cur *= 10;
            next *= 10;
        }

        return count;
    }
}

Leetcode224 基本计算器

给你一个字符串表达式 s ,请你实现一个基本计算器来计算并返回它的值。

class Solution {
    public int calculate(String s) {
        char[] arr = s.toCharArray();
        Deque<Integer> stack = new ArrayDeque<>();
        int i = 0, n = arr.length, sign = 1, res = 0;
        stack.push(1);
        while (i < n){
            if (arr[i] == ' '){
                i++;
            }else if (arr[i] == '+'){
                sign = stack.peek();
                i++;
            }else if (arr[i] == '-'){
                sign = -stack.peek();
                i++;
            }else if (arr[i] == '('){
                stack.push(sign);
                i++;
            }else if (arr[i] == ')'){
                stack.pop();
                i++;
            }else {
                long num = 0;
                while (i < n && arr[i] >= '0' && arr[i] <= '9'){
                    num = num * 10 + arr[i] - '0';
                    i++;
                }
                
                res += sign * num;
            }
        }
        return res;
    }
}

Leetcode787 K 站中转内最便宜的航班

有 n 个城市通过 m 个航班连接。每个航班都从城市 u 开始,以价格 w 抵达 v。

现在给定所有的城市和航班,以及出发城市 src 和目的地 dst,你的任务是找到从 src 到 dst 最多经过 k 站中转的最便宜的价格。 如果没有这样的路线,则输出 -1。

这题用最朴素的迪杰斯特拉,效率差的一。

class Solution {
    public int findCheapestPrice(int n, int[][] flights, int src, int dst, int K) {
        List<List<int[]>> edges = new ArrayList<>();
        for(int i = 0; i < n; i++){
            edges.add(new ArrayList<>());
        }
        for(int[] ele : flights){
            edges.get(ele[0]).add(new int[]{ele[1], ele[2]});
        }

        PriorityQueue<Node> queue = new PriorityQueue<>((x, y) -> (x.dist - y.dist));
        queue.offer(new Node(src, 0, K));
        while (!queue.isEmpty()){
            Node temp = queue.poll();
            if (temp.index == dst) return temp.dist;
            if (temp.k < 0) continue;
            for(int[] ele : edges.get(temp.index)){
                int next = ele[0];
                queue.offer(new Node(next, temp.dist + ele[1], temp.k - 1));
            }
        }

        return -1;
    }

    class Node{
        int index;
        int dist;
        int k;

        public Node(int index, int dist, int k){
            this.index = index;
            this.dist = dist;
            this.k = k;
        }
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值