Leetcode 763. Partition Labels

思路:spa

方法一:時間複雜度O(n)。很巧妙。指針

 1 class Solution {
 2     public List<Integer> partitionLabels(String S) {
 3         List<Integer> res = new ArrayList<>();//返回的結果
 4         int[] map = new int[26];
 5         for(int i = 0; i < S.length(); i++) {//記錄每一個字母在S中最後一次出現的位置
 6             map[S.charAt(i) - 'a'] = i;
 7         }
 8         int start = -1, end = 0;//start是下一個字段的首字母的前一個位置,第一個字段的首字母的位置必定是0
 9         for(int i = 0; i < S.length(); i++) {
10             end = Math.max(end, map[S.charAt(i) - 'a']);//不斷更新末尾指針的位置
11             if(end == i) {//當i指針的位置和末尾指針的位置相同時意味着[start,end]之間的字母的最後出現位置都在[start,end],尾指針沒法再更新
12                 res.add(end - start);//當前字段的長度
13                 start = end;//start是下一個字段的首字母的前一個位置
14             }
15         }
16         return res;
17     }
18 }

方法二:也是不斷更新末尾指針,可是寫得沒有方法一好,複雜度也比方法一高。code

 1 class Solution {
 2     public List<Integer> partitionLabels(String S) {
 3         List<Integer> re = new ArrayList<Integer>();
 4         HashSet<Character> set = new HashSet<Character>();
 5         for(int i = 0; i < S.length(); i++) {
 6             int start = i, end = i;
 7             boolean flags = true;
 8             set.add(S.charAt(start));
 9             while(flags) {
10                 int j = S.length() - 1;
11                 for(; j > end; j--) {
12                     if(set.contains(S.charAt(j))) {
13                         for(int k = end; k < j; k++) {
14                             set.add(S.charAt(k));
15                         }
16                         break;
17                     }
18                 }
19                 if(j > end) {
20                     end = j;
21                 }else {
22                     flags = false;
23                 }
24             }
25             re.add(end - start + 1);
26             i = end;
27         }
28         return re;
29     }
30 }
相關文章
相關標籤/搜索