mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 11:34:46 +08:00
更新主函数精简版本
This commit is contained in:
@ -141,7 +141,8 @@ void reverse(string& s, int start, int end) {
|
|||||||
|
|
||||||
<img src='https://code-thinking.cdn.bcebos.com/pics/151_%E7%BF%BB%E8%BD%AC%E5%AD%97%E7%AC%A6%E4%B8%B2%E9%87%8C%E7%9A%84%E5%8D%95%E8%AF%8D.png' width=600> </img></div>
|
<img src='https://code-thinking.cdn.bcebos.com/pics/151_%E7%BF%BB%E8%BD%AC%E5%AD%97%E7%AC%A6%E4%B8%B2%E9%87%8C%E7%9A%84%E5%8D%95%E8%AF%8D.png' width=600> </img></div>
|
||||||
|
|
||||||
```
|
```C++
|
||||||
|
// 版本一
|
||||||
class Solution {
|
class Solution {
|
||||||
public:
|
public:
|
||||||
// 反转字符串s中左闭又闭的区间[start, end]
|
// 反转字符串s中左闭又闭的区间[start, end]
|
||||||
@ -219,6 +220,24 @@ public:
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
当然这里的主函数reverseWords写的有一些冗余的,可以精简一些,精简之后的主函数为:
|
||||||
|
|
||||||
|
```C++
|
||||||
|
// 注意这里仅仅是主函数,其他函数和版本一一致
|
||||||
|
string reverseWords(string s) {
|
||||||
|
removeExtraSpaces(s);
|
||||||
|
reverse(s, 0, s.size() - 1);
|
||||||
|
for(int i = 0; i < s.size(); i++) {
|
||||||
|
int j = i;
|
||||||
|
// 查找单词间的空格,翻转单词
|
||||||
|
while(j < s.size() && s[j] != ' ') j++;
|
||||||
|
reverse(s, i, j - 1);
|
||||||
|
i = j;
|
||||||
|
}
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user