We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 42d84f8 commit 7bbf382Copy full SHA for 7bbf382
problems/0151.翻转字符串里的单词.md
@@ -513,6 +513,29 @@ class Solution:
513
514
return "".join(result)
515
```
516
+
517
+(版本五) 遇到空格就说明前面的是一个单词,把它加入到一个数组中。
518
519
+```python
520
+class Solution:
521
+ def reverseWords(self, s: str) -> str:
522
+ words = []
523
+ word = ''
524
+ s += ' ' # 帮助处理最后一个字词
525
526
+ for char in s:
527
+ if char == ' ': # 遇到空格就说明前面的可能是一个单词
528
+ if word != '': # 确认是单词,把它加入到一个数组中
529
+ words.append(word)
530
+ word = '' # 清空当前单词
531
+ continue
532
533
+ word += char # 收集单词的字母
534
535
+ words.reverse()
536
+ return ' '.join(words)
537
+```
538
539
### Go:
540
541
版本一:
0 commit comments