557. Reverse Words in a String III
这题是反转字符串中的单词。关键是找到单词与单词之间的空格。在题目的Note里:有写单词与单词之间的空格只有一个。所以我的代码每次在找到空格后,只跳过一个。(这题通俗应该写一个循环,把空格都跳过去)。
Note: In the string, each word is separated by single space and there will not be
any extra space in the string.
Code:
class Solution {
public:
string reverseWords(string s) {
auto current = s.begin();
auto _find = s.begin();
while (current != s.end()) {
_find = find(current,s.end(), ' ');
reverse(current, _find);
current = _find != s.end()?_find+1:s.end();
}
return s;
}
};