Skip to content

Latest commit

 

History

History
23 lines (23 loc) · 562 Bytes

763.划分字母区间.md

File metadata and controls

23 lines (23 loc) · 562 Bytes
func partitionLabels(s string) []int {
    var res []int
    // 先遍历记录每个字母的最远边界
    table := make([]int, 26)
    for i, c := range s {
        table[c-'a'] = i
    }
    // 遍历s,对于每个字母查看其最远边界
    var start, end int
    for i, c := range s {
        if table[c-'a'] > end {
            end = table[c-'a']
        }
        // 达到最远边界重合,为切割点
        if end == i {
            res = append(res, end - start + 1)
            start = end + 1
        }
    }
    return res
}