LeetCode763. Partition Labels(划分字母区间)JAVA实现

本文介绍了一种将字符串S按字母出现次数进行最优分区的算法,确保每个字母只在一个分区中出现,返回各分区长度的列表。通过示例展示了如何利用辅助数组优化查找过程,提高效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A string S of lowercase letters is given. We want to partition this string into as many parts as possible so that each letter appears in at most one part, and 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.

 

Note:

  1. S will have length in range [1, 500].
  2. S will consist of lowercase letters ('a' to 'z') only.

字符串 S 由小写字母组成。我们要把这个字符串划分为尽可能多的片段,同一个字母只会出现在其中的一个片段。返回一个表示每个字符串片段的长度的列表。

示例 1:

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

注意:

  1. S的长度在[1, 500]之间。
  2. S只包含小写字母'a''z'

解题思路:以示例一为例子, S = "ababcbacadefegdehijhklij"

遍历S字符串,首先对于S[0]:a,使用S.lastIndexOf(a)找到a在S中出现的最后一次的下标8,记为maxSubIndex,再次遍历i-maxSubIndex的位置,查找其中其它字母的最后一次出现的下标大于maxSubIndex,如果有的话更新maxSubIndex,然后继续遍历到最新的maxSubIndex,找到最大的maxSubIndex后,maxSubIndex-i+1就是该段划分区间的长度。

为了避免每一次都要查找想要字符的最后一次出现的下标,所以先设置一个辅助数组int charTable[] = new int[26]。先遍历一次S,记录下每个字母的lastIndex。那么在循环中只需要查这个表就可以了。

实现代码:

class Solution {
    public List<Integer> partitionLabels(String S) {
         if(S == null || S.length() == 0){
            return null;
        }
        List<Integer> res = new ArrayList<Integer>();
        int len = S.length();
        int maxSubIndex = 0;
        int i = 0;
        int []charTable = new int[26];
        for(i=0;i<len;i++){
            charTable[(int)(S.charAt(i)-'a')] = i;
        }
        i = 0;
        while(i<len){
            maxSubIndex = charTable[(int)(S.charAt(i)-'a')];
            for(int j=i+1;j<=maxSubIndex&&j<len;j++){
                if(charTable[S.charAt(j)-'a']>maxSubIndex){
                    maxSubIndex = charTable[S.charAt(j)-'a'];
                }
            } 
            res.add(maxSubIndex-i+1);
            i = maxSubIndex+1;
        }
        return res;
    }
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值