LeetCode Hot100(字串)

560. 和为 K 的子数组

因为存在负数,该题只能直接暴力了,当然,也可以使用前缀和,但是时间复杂度一样

复制代码
class Solution {
    public int subarraySum(int[] nums, int k) {
        int res = 0;
        for (int i = 0; i < nums.length; i++) {
            int count = 0, curSum = 0;
            for (int j = i; j < nums.length; j++) {
                curSum += nums[j];
                if (curSum == k) {
                    ++count;
                }
            }
            res += count;
        }
        return res;
    }
}

239. 滑动窗口最大值

优先队列维护即可,因为自定义优先队列返回的是自定义最大最小值,我们每一次放入值之后,开始特判当前位置过期的就可以了

复制代码
class Solution {
     public  int[] maxSlidingWindow(int[] nums, int k) {
        PriorityQueue<Node> queue = new PriorityQueue<>();
        int[] ans = new int[nums.length - k + 1];
        int index = 0;

        for (int i = 0; i < nums.length; i++) {
            queue.offer(new Node(nums[i], i));
            while (queue.peek().getIndex() < i - k + 1) {
                queue.poll();
            }

            if (i >= k - 1) {
                ans[index++] = queue.peek().getVal();
            }
        }

        return ans;
    }

    static class Node implements Comparable<Node> {
        int val;
        int index;

        public Node(int val, int index) {
            this.val = val;
            this.index = index;
        }

        public int getVal() {
            return val;
        }

        public int getIndex() {
            return index;
        }

        @Override
        public int compareTo(Node o) {
            // 降序排列(最大堆)
            return Integer.compare(o.val, this.val);
        }
    }
}

76. 最小覆盖子串

跟之前的滑动窗口非常像,我们只需要统计当前区间是否满足完全包含t就可以了,需要注意的一点是,最好通过左端点+len放最小值最后提取字符串时间较短

复制代码
class Solution {
     public String minWindow(String s, String t) {
        char []arr=s.toCharArray();
        char []brr=t.toCharArray();

        int sum=brr.length;
        int []crr=new int[200];
        HashSet<Character>mark=new HashSet<>();
        for(int i=0;i<sum;i++){
            int f=brr[i];
            mark.add(brr[i]);
            crr[f]++;
        }
        sum=mark.size();
        int sum2=0;
        int l2=0;
        for(int i=0;i<arr.length;i++){
            if(mark.contains(arr[i])){
                l2=i;
                break;
            }
        }
        int l=l2;//起始左边
        int len=1000000;//长度
        int []drr=new int[200];
        for(int i=0;i<arr.length;i++){
            int f=arr[i];
            if(!mark.contains(arr[i])){
                continue;
            }
            drr[f]++;
            if(drr[f]==crr[f]){
                sum2++;
            }

            while(sum==sum2){
                if(sum2==sum){
                    int len2=i-l2+1;
                    if(len2<=len){
                        len=len2;
                        l=l2;
                    }
                }
                int f2=arr[l2];
                if(!mark.contains(arr[l2])){
                    l2++;
                    continue;
                }
                drr[f2]--;
                l2++;
                if(drr[f2]<crr[f2]){
                    sum2--;
                    break;
                }
            }
        }


        if(len==1000000){
            return "";
        }
        else{
            String ans=s.substring(l, len+l);
//            System.out.println(ans+"----"+l+"---"+len);
            return ans;
        }

    }
}
相关推荐
Wang's Blog27 分钟前
PostgreSQL笔记49:向量检索核心算法、索引调优与过滤策略深度解析
笔记·算法·postgresql
疯狂打码的少年35 分钟前
【数据结构】交换类排序:冒泡与快速排序
数据结构·笔记·算法·排序算法
Nil2081 小时前
leetcode 108有序数组转换为二叉搜索树
数据结构·算法·leetcode
高频因子挖掘机1 小时前
QuantDash 成交量单位统一实战:从“手”到“股”的跨市场量化数据清洗全流程
后端·算法·github
hn小菜鸡1 小时前
LeetCode 763、划分字母区间
数据结构·算法·leetcode
Escalating_xu1 小时前
【C++ STL简介】从六大组件到容器、迭代器与算法协作
java·c++·算法
纪念 2292 小时前
算法二叉树(一)
算法
liulilittle2 小时前
llmx 学习手册 06 —— CPU 指令集优化(AVX-512 三层演进)
c++·学习·算法·ai·llm
土司大王3 小时前
LeetCode hot100——相交链表
算法·leetcode·链表
土司大王3 小时前
LeetCode hot100——回文链表
算法·leetcode·链表