zoukankan      html  css  js  c++  java
  • 763. Partition Labels

    package LeetCode_763
    
    /**
     * 763. Partition Labels
     * https://leetcode.com/problems/partition-labels/description/
     *
     * A string S of lowercase English 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:
    S will have length in range [1, 500].
    S will consist of lowercase English letters ('a' to 'z') only.
     * */
    class Solution {
        /*
        * solution: brute force, Time complexity:O(n^2), Space complexity:O(1);
        * traverse S from left to right to find the last position of current char in String,
        * then compare to find partition;
        * */
        fun partitionLabels(S: String): List<Int> {
            val result = ArrayList<Int>()
            var start = 0
            var end = 0
            for (i in S.indices) {
                end = Math.max(end, S.lastIndexOf(S[i]))
                //found out the split point
                if (i == end) {
                    result.add(end - start + 1)
                    //update the start to current end
                    start = end + 1
                }
            }
            return result
        }
    }
  • 相关阅读:
    SpringMVC基础知识
    git pull 和git fetch的区别
    动态规划的原理?
    为什么要使用volatile修饰呢?
    内部类,匿名内部类?
    什么是性能优化?
    如何定位CPU瓶颈?
    什么是程序的耦合?
    什么是性能优化?
    Class类的常用方法?
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/13721919.html
Copyright © 2011-2022 走看看