【陪伴式刷题】Day 32|贪心算法|763.划分字母区间(Partition Labels)

刷题顺序按照代码随想录建议

题目描述

英文版描述

You are given a string s. We want to partition the string into as many parts as possible so that each letter appears in at most one part.

Note that the partition is done so that after concatenating all the parts in order, the resultant string should be s.

Return a list of integers representing the size of these parts.

Example 1:

Input: s = "ababcbacadefegdehijhklij" Output: [9,7,8] Explanation: The partition is "ababcbaca", "defegde", "hijhklij". This is a partition so that each letter appears in at most one part. A partition like "ababcbacadefegde", "hijhklij" is incorrect, because it splits s into less parts.

Example 2:

Input: s = "eccbbbbdec" Output: [10]

Constraints:

  • 1 <= s.length <= 500
  • s consists of lowercase English letters.

英文版地址

leetcode.com/problems/pa...

中文版描述

给你一个字符串 s 。我们要把这个字符串划分为尽可能多的片段,同一字母最多出现在一个片段中。

注意,划分结果需要满足:将所有划分结果按顺序连接,得到的字符串仍然是 s 。

返回一个表示每个字符串片段的长度的列表。

示例 1:

输入: s = "ababcbacadefegdehijhklij" 输出: [9,7,8] 解释: 划分结果为 "ababcbaca"、"defegde"、"hijhklij" 。 每个字母最多出现在一个片段中。 像 "ababcbacadefegde", "hijhklij" 这样的划分是错误的,因为划分的片段数较少。

示例 2:

输入: s = "eccbbbbdec" 输出: [10]

提示:

  • 1 <= s.length <= 500
  • s 仅由小写英文字母组成

中文版地址

leetcode.cn/problems/pa...

解题方法

俺这版

java 复制代码
class Solution {
    List<Integer> result = new ArrayList<>();
    public List<Integer> partitionLabels(String s) {
        if (s == null || s.length() == 1) {
            return result;
        }
        Map<Character, Integer> map = new HashMap<>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (map.get(c) != null) {
                map.put(c, i);
            } else {
                map.put(c, i);
            }
        }
        int end = 0;
        int last = -1;
        for (int i = 0; i < s.length(); i++) {
            end = Math.max(end, map.get(s.charAt(i)));
            if (i == end) {
                result.add(i - last);
                last = i;
            }
        }
        return result;
    }
}

复杂度分析

  • 时间复杂度:O(n),其中 n 是字符串长度
  • 空间复杂度:O(1),字母数量确定,最多26个字母,因此额外只需要常数空间

官方版

两种方法逻辑基本一致,主要是官方将Map优化为了数组,效果果然好了很多(不过基于之前有篇文章作了深入的分析与测试,Leetcode官方的这个时间复杂度和空间复杂度大家就姑且看看,图一乐,别太当真)

ini 复制代码
class Solution {
    public List<Integer> partitionLabels(String S) {
        List<Integer> list = new LinkedList<>();
        int[] edge = new int[26];
        char[] chars = S.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            edge[chars[i] - 'a'] = i;
        }
        int idx = 0;
        int last = -1;
        for (int i = 0; i < chars.length; i++) {
            idx = Math.max(idx,edge[chars[i] - 'a']);
            if (i == idx) {
                list.add(i - last);
                last = i;
            }
        }
        return list;
    }
}

复杂度分析

  • 时间复杂度:O(n),其中 n 是字符串长度
  • 空间复杂度:O(1),额外常数空间
相关推荐
yngsqq39 分钟前
netdxf—— CAD c#二次开发之(netDxf 处理 DXF 文件)
java·前端·c#
A了LONE1 小时前
h5的底部导航栏模板
java·前端·javascript
经典19921 小时前
spring boot 详解以及原理
java·spring boot·后端
星光54221 小时前
飞算JavaAI:给Java开发装上“智能引擎”的超级助手
java·开发语言
学习3人组2 小时前
JVM GC长暂停问题排查
java
R_AirMan2 小时前
深入浅出Redis:一文掌握Redis底层数据结构与实现原理
java·数据结构·数据库·redis
人生在勤,不索何获-白大侠2 小时前
day17——Java集合进阶(Collections、Map)
java·开发语言
程序员小羊!2 小时前
Java教程:JavaWeb ---MySQL高级
java·开发语言·mysql
白仑色2 小时前
Spring Boot 多环境配置详解
java·spring boot·后端·微服务架构·配置管理
超级小忍2 小时前
在 Spring Boot 中优化长轮询(Long Polling)连接频繁建立销毁问题
java·spring boot·后端