A string S
of lowercase 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.java
Example 1:app
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:less
S
will have length in range [1, 500]
.S
will consist of lowercase letters ('a'
to 'z'
) only.題目地址this
這道題我竟然一遍過,之前作題或多或少有些小錯誤。這道題我一上手就發現應該儘可能阻止對數據的屢次遍歷,因此若是能遍歷一遍獲得一些有用的信息就行了。我用的解法是,創建一個map存儲每一個字母的首尾位置(固然實際操做中我用的是兩個map分別存儲字母第一次出現的位置和最後一次出現的位置)。而後若是這一段之間出現了一個字母,它的尾部位置比框住它的這個字母更大,就更新尾部位置,直到尾部位置沒法再擴大爲止。code
class Solution { public List<Integer> partitionLabels(String S) { List<Integer> result = new ArrayList<>(); char[] c = S.toCharArray(); Map<Character, Integer> mapStart = new HashMap<>(); Map<Character, Integer> mapEnd = new HashMap<>(); for(int i=0;i<c.length;i++){ if(mapStart.get(c[i])==null){ mapStart.put(c[i], i); mapEnd.put(c[i], i); }else{ mapEnd.put(c[i], i); } } int beginIndex=0; while(beginIndex<c.length){ int endIndex=mapEnd.get(c[beginIndex]); for(int i=beginIndex;i<endIndex;i++){ if(mapEnd.get(c[i])>endIndex){ endIndex = mapEnd.get(c[i]); } } result.add(endIndex-beginIndex+1); beginIndex = endIndex+1; } return result; } }