diff --git a/problems/0151.翻转字符串里的单词.md b/problems/0151.翻转字符串里的单词.md index 677a8f64..e7abd1d8 100644 --- a/problems/0151.翻转字符串里的单词.md +++ b/problems/0151.翻转字符串里的单词.md @@ -224,34 +224,36 @@ public: ```CPP //版本二: +//原理同版本1,更简洁实现。 class Solution { public: - void reverseWord(string& s,int start,int end){ //这个函数,Carl哥的要更清晰。 - for(int i=start;i<(end-start)/2+start;++i){ - swap(s[i],s[end-1-i+start]); + void reverse(string& s, int start, int end){ //翻转,区间写法:闭区间 [] + for (int i = start, j = end; i < j; i++, j--) { + swap(s[i], s[j]); } } - void trim(string& s){//去除所有空格并在相邻单词之间添加空格 - int slow = 0; - for(int i=0;i