代码随想录——划分字母区间(Leetcode763)

题目链接

贪心

java 复制代码
class Solution {
    public List<Integer> partitionLabels(String s) {
        int[] count = new int[27];
        Arrays.fill(count,0);
        // 统计元素最后一次出现的位置
        for(int i = 0; i < s.length(); i++){
            count[s.charAt(i) - 'a'] = i;
        }
        List<Integer> res = new ArrayList<Integer>();
        int left = 0;
        int right = 0;
        for(int i = 0; i < s.length(); i++){
            // 找到字符出现的最远边界
            right = Math.max(right, count[s.charAt(i) - 'a']);
            if(i == right){
                // 将符合条件的字符串长度插入res
                res.add(right - left + 1);
                left = i + 1;
            }
        }
        return res;
    }
}
相关推荐
一叶飘零_sweeeet19 小时前
深入拆解 Java CAS:从底层原理到 ABA 问题实战
java·cas·并发编程
StackNoOverflow20 小时前
Spring Security权限控制框架详解
java·数据库·sql
yaaakaaang20 小时前
九、装饰器模式
java·装饰器模式
大熊背20 小时前
如何利用Lv值实现三级降帧
算法·自动曝光·lv·isppipeline
d_dreamer20 小时前
SeaTunnel推荐Maven版本
java·maven
清心歌20 小时前
记一次系统环境变量更改后在IDEA中无法读取新值的排查过程
java·后端·intellij-idea·idea
大尚来也20 小时前
驾驭并发:.NET多线程编程的挑战与破局之道
java·前端·算法
dong__csdn20 小时前
jdk添加信任证书
java·开发语言
向阳而生,一路生花20 小时前
深入浅出 JDK7 HashMap 源码分析
算法·哈希算法
hhcccchh20 小时前
1.1 HTML 语义化标签(header、nav、main、section、footer 等)
java·前端·html