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

LeetCode763. 划分字母区间

    • 题目链接
    • 代码

题目链接

https://leetcode.cn/problems/partition-labels/description/
LeetCode(力扣)763. 划分字母区间Python_第1张图片

代码

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

你可能感兴趣的:(leetcode,python,算法)