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

2022-02-21 19:31:58 +08:00
 tiRolin

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 对我慢慢解释的时间了,最后谢谢各位的帮助

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

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


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

匿名函数听说过吗

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/835478

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX