diff --git a/problems/0151.翻转字符串里的单词.md b/problems/0151.翻转字符串里的单词.md index dc12ae23..eb78cc9d 100644 --- a/problems/0151.翻转字符串里的单词.md +++ b/problems/0151.翻转字符串里的单词.md @@ -516,6 +516,48 @@ class Solution: return s[:ps] + s[ps:][::-1] # Must do the last step, because the last word is omit though the pointers are on the correct positions, ``` +```python +class Solution: # 使用双指针法移除空格 + def reverseWords(self, s: str) -> str: + + def removeextraspace(s): + start = 0; end = len(s)-1 + while s[start]==' ': + start+=1 + while s[end]==' ': + end-=1 + news = list(s[start:end+1]) + slow = fast = 0 + while fast0 and news[fast]==news[fast-1]==' ': + fast+=1 + news[slow]=news[fast] + slow+=1; fast+=1 + #return "".join(news[:slow]) + return news[:slow] + + def reversestr(s): + left,right = 0,len(s)-1 + news = list(s) + while left