Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.
Example 1:
Input: "Let's take LeetCode contest"
Output: "s'teL ekat edoCteeL tsetnoc"
Note: In the string, each word is separated by single space and there will not be any extra space in the string.
地址: https://leetcode.com/problems/reverse-words-in-a-string-iii/#/description
不使用额外空间,但 c 的字符串不应该是常量不可修改嘛?
为何我看别人写的都是直接对字条串的单个字符进行修改呢
void reverse(int b, int e, char *s){
while(b < e) {
s[b] = s[b] ^ s[e];
s[e] = s[b] ^ s[e];
s[b] = s[b] ^ s[e];
b++;
e--;
}
}
char* reverseWords(char* s) {
int i, s_len = strlen(s), index = 0;
for(i = 0; i <= s_len; i++) {
if((s[i] == ' ') || (s[i] == '\0')){
reverse(index, i - 1, s);
index = i + 1;
}
}
return s;
}
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.