LeetCode(力扣)763. 划分字母区间Python

LeetCode763. 划分字母区间

题目链接

https://leetcode.cn/problems/partition-labels/description/
在这里插入图片描述

代码

class Solution:
    def partitionLabels(self, s: str) -> List[int]:
        last_occ = {
    
    }
        for i, ch in enumerate(s):
            last_occ[ch] = i
        
        result = []
        start = 0
        end = 0
        for i, ch in enumerate(s):
            end = max(end, last_occ[ch])
            if end == i:
                result.append(end - start + 1)
                start = end + 1
        return result

猜你喜欢

转载自blog.csdn.net/qq_44953660/article/details/132913051