代码随想录——划分字母区间(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;
    }
}
相关推荐
BioRunYiXue1 分钟前
从现象到机制:蛋白降解调控研究的系统策略与实验设计
java·linux·运维·服务器·网络·人工智能·eclipse
希望永不加班3 分钟前
如何在 SpringBoot 里自定义 Spring MVC 配置
java·spring boot·后端·spring·mvc
alphaTao3 分钟前
LeetCode 每日一题 2026/3/16-2026/3/22
linux·windows·leetcode
空空潍3 分钟前
LeetCode力扣 hot100一刷完结
算法·leetcode
weixin199701080164 分钟前
“迷你京东”全栈架构设计与实现
java·大数据·python·数据库架构
leaves falling6 分钟前
搜索插入位置(第一个≥target的位置)
算法
历程里程碑7 分钟前
41 .UDP -3 群聊功能实现:线程池助力多客户端通信
linux·开发语言·网络·数据结构·c++·网络协议·udp
lcreek7 分钟前
LeetCode 1162.地图分析
算法·leetcode·bfs
寒月小酒10 分钟前
3.20 OJ
算法
东离与糖宝12 分钟前
3月20日紧急修复|Spring AI双漏洞CVE-2026-22730/22729实战防护方案
java