Reverse Words in a String
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.
解题思路:
1、若用C#实现,非常简单,用字符串的split函数,将非空的子串倒着连接起来即可。
2、C++似乎没有split函数,需要我们自己实现一下,返回vector
3、可以只扫描一次字符串。下面是代码。有几个特殊情况需要考虑(1)可能会有多个空格符(2)可能没有空格符
class Solution {
public:
void reverseWords(string &s) {
string result="";
int len = s.length();
string word="";
for(int i=0; i
转载请注明: » [LeetCode] Reverse Words in a String
<!--
-->
原文作者:;如有侵权,请联系管理员删除。