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; } 