V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
tiRolin
V2EX  ›  问与答

关于力扣上的三个题解的代码问题,求 dalao 帮我解答

  •  
  •   tiRolin · 2022-02-21 19:31:58 +08:00 · 812 次点击
    这是一个创建于 766 天前的主题,其中的信息可能已经有所发展或是发生改变。

    76.最小覆盖子串 题目链接: https://leetcode-cn.com/problems/minimum-window-substring/ 题解代码: class Solution { public String minWindow(String s, String t) { if (s == null || s.length() == 0 || t == null || t.length() == 0){ return ""; } int[] need = new int[128]; //记录需要的字符的个数 for (int i = 0; i < t.length(); i++) { need[t.charAt(i)]++; } //l 是当前左边界,r 是当前右边界,size 记录窗口大小,count 是需求的字符个数,start 是最小覆盖串开始的 index int l = 0, r = 0, size = Integer.MAX_VALUE, count = t.length(), start = 0; //遍历所有字符 while (r < s.length()) { char c = s.charAt(r); if (need[c] > 0) {//需要字符 c count--; } need[c]--;//把右边的字符加入窗口 if (count == 0) {//窗口中已经包含所有字符 while (l < r && need[s.charAt(l)] < 0) { need[s.charAt(l)]++;//释放右边移动出窗口的字符 l++;//指针右移 } if (r - l + 1 < size) {//不能右移时候挑战最小窗口大小,更新最小窗口开始的 start size = r - l + 1; start = l;//记录下最小值时候的开始位置,最后返回覆盖串时候会用到 } //l 向右移动后窗口肯定不能满足了 重新开始循环 need[s.charAt(l)]++; l++; count++; } r++; } return size == Integer.MAX_VALUE ? "" : s.substring(start, start + size); } } 我不明白的是 need[t.charAt(i)]++;这个代码的作用,我试着将其改成 need[i]=t.charAt(i),但是是不对的,说明两个代码并不是等价的,debug 看了好几遍也没搞懂这行代码的作用,我学习的过程中没有学过数组的这种用法,希望 dalao 解答一二

    567.字符串的排列 题目链接: https://leetcode-cn.com/problems/permutation-in-string/ 题解代码: class Solution { public boolean checkInclusion(String s1, String s2) { int n = s1.length(), m = s2.length(); if (n > m) { return false; } int[] cnt1 = new int[26]; int[] cnt2 = new int[26]; for (int i = 0; i < n; ++i) { ++cnt1[s1.charAt(i) - 'a']; ++cnt2[s2.charAt(i) - 'a']; } if (Arrays.equals(cnt1, cnt2)) { return true; } for (int i = n; i < m; ++i) { ++cnt2[s2.charAt(i) - 'a']; --cnt2[s2.charAt(i - n) - 'a']; if (Arrays.equals(cnt1, cnt2)) { return true; } } return false; } } 同样的,我也是搞不懂 ++cnt1[s1.charAt(i) - 'a']; ++cnt2[s2.charAt(i) - 'a']; 这两行代码的作用

    2034.股票价格的波动 题目链接: https://leetcode-cn.com/problems/stock-price-fluctuation/ 题解代码: class StockPrice { int maxTimestamp; HashMap<Integer, Integer> timePriceMap; PriorityQueue<int[]> pqMax; PriorityQueue<int[]> pqMin;

    public StockPrice() {
        maxTimestamp = 0;
        timePriceMap = new HashMap<Integer, Integer>();
        pqMax = new PriorityQueue<int[]>((a, b) -> b[0] - a[0]);
        pqMin = new PriorityQueue<int[]>((a, b) -> a[0] - b[0]);
    }
    
    public void update(int timestamp, int price) {
        maxTimestamp = Math.max(maxTimestamp, timestamp);
        timePriceMap.put(timestamp, price);
        pqMax.offer(new int[]{price, timestamp});
        pqMin.offer(new int[]{price, timestamp});
    }
    
    public int current() {
        return timePriceMap.get(maxTimestamp);
    }
    
    public int maximum() {
        while (true) {
            int[] priceTime = pqMax.peek();
            int price = priceTime[0], timestamp = priceTime[1];
            if (timePriceMap.get(timestamp) == price) {
                return price;
            }
            pqMax.poll();
        }
    }
    
    public int minimum() {
        while (true) {
            int[] priceTime = pqMin.peek();
            int price = priceTime[0], timestamp = priceTime[1];
            if (timePriceMap.get(timestamp) == price) {
                return price;
            }
            pqMin.poll();
        }
    }
    

    } 这里我是搞不懂 pqMax = new PriorityQueue<int[]>((a, b) -> b[0] - a[0]); pqMin = new PriorityQueue<int[]>((a, b) -> a[0] - b[0]); 这两行代码的原理,创建队列以及使用泛型我明白,但是括号内的定义方式我搞不懂,不知道有什么作用,不知道该怎么使用这种定义方式 暂时就这三个问题了,麻烦各位大佬了,如果各位大佬有相应的知识点链接的话,可以直接贴出来,我会看的,这样也省去了各位 dalao 对我慢慢解释的时间了,最后谢谢各位的帮助

    2 条回复    2022-02-21 20:14:12 +08:00
    hello2090
        1
    hello2090  
       2022-02-21 19:38:58 +08:00 via iPhone
    你这个排版。。最后一个括号里面的是优先级队列的排序方式啊,a0-b0 就是第一个元素最小的数组在 top, b0-a0 就是第一个元素最大的数组在 top 你看下优先级队列的构造函数不就知道了。
    GeruzoniAnsasu
        2
    GeruzoniAnsasu  
       2022-02-21 20:14:12 +08:00   ❤️ 1
    我就奇了怪了,leetcode 是没有评论区吗?

    而且都是初学最最基础的理解能力的问题,根本没必要把整个题目都丢上来,还不排版……


    a++你会吧
    a[0]++你会吗
    a[b]++你会吗
    ++a[b]呢
    ++a[b.find(c)]呢

    匿名函数听说过吗
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3531 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 04:50 · PVG 12:50 · LAX 21:50 · JFK 00:50
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.