leetcode:. 763 letters divide the interval

763. The division of the letters section

S String lowercase letters. We put this string is divided into as many fragments, the same letter will appear in a segment of them. It returns a list of the length of each string segment representation.

Example 1:

Input: S = "ababcbacadefegdehijhklij"
Output: [9,7,8]

Explanation:

Divide the result is "ababcbaca", "defegde", "hijhklij".
Each letter appears in a segment up.
Like "ababcbacadefegde", "hijhklij" division is wrong, because fewer segments divided.

note:

Length S between [1, 500].
S contains only lowercase letters 'a' to 'z'.

Method 1: Use string of find_last_of () function, after the former manager again to constantly refresh the value of the right border.

{Solution class 
public: 
    Vector <int> partitionLabels (String S) { 
        Vector <int> V; 
       int left = -1; 
       int right = 0; 
       for (int I = 0; I <s.size (); I ++) { 
           int index = S.find_last_of (S [i ]); // index of the last node in the string that appears 
           if (index> right) right = index; // update right boundary 
           if (i == right) {/ / If reaches the right border 
               v.push_back (left-right); 
               left = right; 
           } 
       } 
        return V; 
    } 
};

  

Guess you like

Origin www.cnblogs.com/52dxer/p/12527412.html