Leetcode One Question of the Day 2020.10.22 Question 763: Divide the letter interval

763. Divide the letter interval

Title description

The string S consists of lowercase letters. We have to divide this string into as many segments as possible, and the same letter appears in at most one segment. Returns a list representing the length of each string fragment.
Source: LeetCode
Link: https://leetcode-cn.com/problems/partition-labels/ The
copyright is owned by LeetCode . For commercial reprints, please contact the official authorization. For non-commercial reprints, please indicate the source.

Example:

Insert picture description here

Ideas:

Since the same letter can only appear in one segment at most, the first subscript of each letter and its last subscript must be in the same segment. Therefore, it is necessary to traverse the entire string to get the subscript of the last occurrence of each letter.
1. Set the initial values ​​of the two pointers start and end to 0, and maintain these two pointers during each traversal.
2. For the subscript end1 at the last occurrence of each letter, the length of each segment cannot exceed the end1, that is, the end point of the segment end = max(max,end1). When traversing to the end, a segment ends, and the segment length (end-start + 1) is added to the list to be output.
3. The start of the next segment is (end + 1) of the previous segment.
4. Repeat the above operation
PS: For detailed code explanation, please refer to the code comments
Insert picture description here
**

Knowledge point: enumerate() function

**
Function: Combine a traversable data object (such as a list, tuple or string, etc.) into an index sequence, and list data and data subscripts at the same time, generally used in for loops
Insert picture description here

Guess you like

Origin blog.csdn.net/m0_51210480/article/details/109242614