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]))
                if (i == end) {
                    result.add(end - start + 1)
                    //update the start to current end
                    start = end + 1
                }
            }
            return result
        }
    }
  • 相关阅读:
    OpenACC 数据管理语句
    OpenACC 简单的直方图
    OpenACC 书上的范例代码(Jacobi 迭代),part 2
    OpenACC 书上的范例代码(Jacobi 迭代),part 1
    OpenACC parallel
    OpenCL 归约 1
    OpenCL 双调排序 GPU 版
    OpenCL 双调排序 CPU 版
    OpenCL 图像卷积 3 使用 CPU
    中括号记法
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/13466970.html
Copyright © 2011-2022 走看看