力扣-10.14-763

在这里插入图片描述

class Solution {
    
    
    public List<Integer> partitionLabels(String S) {
    
    
        int[] lastIndexOfChar=new int[26];
		for(int i=0;i<S.length();i++) {
    
    
			lastIndexOfChar[S.charAt(i)-'a']=i;
		}
		List<Integer> partitions=new ArrayList<Integer>();
		int firstIndex=0;
		while(firstIndex<S.length()) {
    
    
			int lastIndex=firstIndex;
			for(int i=firstIndex;i<S.length() && i<=lastIndex;i++) {
    
    
				int index=lastIndexOfChar[S.charAt(i)-'a'];
				if(index>lastIndex) {
    
    
					lastIndex=index;
				}
			}
			partitions.add(lastIndex-firstIndex+1);
			firstIndex=lastIndex+1;
		}
		return partitions;
    }
}

难!!!

猜你喜欢

转载自blog.csdn.net/Desperate_gh/article/details/109083194