Blage's Coding Blage's Coding
Home
算法
  • 手写Spring
  • SSM
  • SpringBoot
  • JavaWeb
  • JAVA基础
  • 容器
  • Netty

    • IO模型
    • Netty初级
    • Netty原理
  • JVM
  • JUC
  • Redis基础
  • 源码分析
  • 实战应用
  • 单机缓存
  • MySQL

    • 基础部分
    • 实战与处理方案
    • 面试
  • ORM框架

    • Mybatis
    • Mybatis_Plus
  • SpringCloudAlibaba
  • MQ消息队列
  • Nginx
  • Elasticsearch
  • Gateway
  • Xxl-job
  • Feign
  • Eureka
  • 面试
  • 工具
  • 项目
  • 关于
🌏本站
🧸GitHub (opens new window)
Home
算法
  • 手写Spring
  • SSM
  • SpringBoot
  • JavaWeb
  • JAVA基础
  • 容器
  • Netty

    • IO模型
    • Netty初级
    • Netty原理
  • JVM
  • JUC
  • Redis基础
  • 源码分析
  • 实战应用
  • 单机缓存
  • MySQL

    • 基础部分
    • 实战与处理方案
    • 面试
  • ORM框架

    • Mybatis
    • Mybatis_Plus
  • SpringCloudAlibaba
  • MQ消息队列
  • Nginx
  • Elasticsearch
  • Gateway
  • Xxl-job
  • Feign
  • Eureka
  • 面试
  • 工具
  • 项目
  • 关于
🌏本站
🧸GitHub (opens new window)
  • 数组

  • 链表

  • 字符串

    • 3.无重复字符的最长字串
    • 2451. 差值数组不同的字符串
    • 301. 删除无效的括号
    • 49. 字母异位词分组
    • 459. 重复的子字符串
    • 844. 比较含退格的字符串
    • 828. 统计子串中的唯一字符
      • 907. 子数组的最小值之和
        • 1.贡献法+单调栈
    • 剑指offer05
    • 剑指offer38
    • 剑指 Offer 50. 第一个只出现一次的字符
    • 双指针法

  • 二叉树

  • 动态规划

  • 深搜回溯

  • 数学贪心

  • 堆栈队列

  • 前缀和

  • 算法设计

  • 位运算

  • WA

  • 算法
  • 字符串
phan
2023-11-26
目录

828. 统计子串中的唯一字符

# 828. 统计子串中的唯一字符 (opens new window)

# 1.贡献法+哈希

思路:考虑单个字符对答案的贡献值,而不去找出所有的子串。

这里计算单个字符的贡献值采用正难则反的方式,字符c的贡献值=所有字串数目 - 不包含字符c的字串数目。

class Solution {
    public int uniqueLetterString(String s) {
        int res = 0;
        Map<Character, List<Integer>> map = new HashMap<>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            List<Integer> list = map.get(c);
            if (!map.containsKey(c)) {
                list = new ArrayList<>();
                map.put(c, list);
            }
            list.add(i);
        }
        for (Character c : map.keySet()) {
            List<Integer> list = map.get(c);
            for (int i = 0; i < list.size(); i++) {
                int pre = i == 0 ? -1 : list.get(i - 1);
                int suff = i == list.size() - 1 ? s.length() : list.get(i + 1);

                int sum = (suff - pre) * (suff - pre - 1) / 2;
                int left = (list.get(i) - 1 - pre - 1 + 2) * (list.get(i) - pre - 1) / 2;
                int right = (suff - 1 - (list.get(i) + 1) + 2) * (suff - 1 - (list.get(i) + 1) + 1) / 2;
                int devote = sum - left - right;
                res += devote;
            }
        }
        return res;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

# 907. 子数组的最小值之和 (opens new window)

# 1.贡献法+单调栈

通过单调栈找到第i个元素作为最小值的最大子数组左右边界。

class Solution {
    public int sumSubarrayMins(int[] arr) {
        long res = 0;
        int mod = 1000000007;
        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i < arr.length; i++) {
            while (!stack.isEmpty() && arr[stack.peek()] > arr[i]) {
                Integer pop = stack.pop();
                int last = stack.size() == 0 ? 0 : stack.peek()+1;
                res =(res+1l*arr[pop] * (pop - last + 1) * (i - pop )%mod)%mod;
            }
            stack.push(i);
        }

        while (!stack.isEmpty()) {
            Integer pop = stack.pop();
            int last = stack.size() == 0 ? 0 : stack.peek() + 1;
            res =(res+ 1l*arr[pop] * (pop - last + 1) * (arr.length - pop)%mod)%mod;
        }
        return (int)res;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
编辑 (opens new window)
#Leetcode#字符串
上次更新: 2023/12/15, 15:49:57
844. 比较含退格的字符串
剑指offer05

← 844. 比较含退格的字符串 剑指offer05→

Theme by Vdoing | Copyright © 2023-2024 blageCoder
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式