[Microsoft面试]翻转句子中单词的顺序
颠倒一个句子中的词的顺序,比如将“我叫克丽丝”转换为“克丽丝叫我”,实现速度最快,移动最少。 Reverse the whole string, then reverse each word. Using the reverseFixlen() above.void reverseWordsInSentence(char * sen) {int len = strlen(sen);reverseFixlen(sen, len);char * p = str;while (*p!=’\0’) { while (*p == ‘ ‘ && *p!=’\0’) p++; str = p; while (p!= ‘ ‘ && *p!=’\0’) p++; reverseFixlen(str, p-str);}}
页:
[1]