题目
原题页面:https://leetcode.com/problems/reverse-words-in-a-string/
题目类型:String
难度评价:Medium
类似题目:(M) Reverse Words in a String II
Given an input string, reverse the string word by word.
For example,
Given s = "the sky is blue",
return "blue is sky the".Update (2015-02-12):
For C programmers: Try to solve it in-place in O(1) space.Clarification:
- What constitutes a word?
A sequence of non-space characters constitutes a word.- Could the input string contain leading or trailing spaces?
Yes. However, your reversed string should not contain leading or trailing spaces.- How about multiple spaces between two words?
Reduce them to a single space in the reversed string.
分析
反转一句话中的单词。注意题中还要求去除首尾可能存在的空白字符,词与词之间也要去除多余空白字符,而只留一个空白。
代码
1 | class Solution: |
