File tree Expand file tree Collapse file tree 1 file changed +43
-0
lines changed
docs/algorithm/double-pointer Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change @@ -219,6 +219,49 @@ class Solution {
219219}
220220```
221221
222+ ## 763. 划分字母区间
223+
224+ [ 原题链接] ( https://leetcode-cn.com/problems/partition-labels/ )
225+
226+ ### 思路
227+
228+ - 哈希
229+ - 双指针
230+
231+ ``` python
232+ class Solution :
233+ def partitionLabels (self , S : str ) -> List[int ]:
234+ # 字母最后出现的位置
235+ letter_ends = dict ()
236+ for i in range (len (S)):
237+ s = S[i]
238+ letter_ends[s] = i
239+
240+ ans = list ()
241+ start = 0
242+
243+ while start < len (S):
244+ begin = start
245+ # 字母最后出现的位置
246+ end = letter_ends[S[start]]
247+ while start < end:
248+ letter = S[start]
249+ letter_end = letter_ends[letter]
250+ # 如果字母最后出现位置大于 end,对 end 进行更新
251+ if letter_end > end:
252+ end = letter_end
253+ start += 1
254+ ans.append(end - begin + 1 )
255+ start = end + 1
256+
257+ return ans
258+ ```
259+
260+ ### 复杂度
261+
262+ - 时间复杂度:` O(n) `
263+ - 空间复杂度:` O(26) `
264+
222265<!-- tabs:end -->
223266
224267## 面试题 10.01. 合并排序的数组
You can’t perform that action at this time.
0 commit comments