Skip to content

Commit 5884250

Browse files
authored
Merge pull request gzc426#127 from cuiHL/master
LeetCode151
2 parents 27ad47e + 10f3450 commit 5884250

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

2018.11.28-leetcode151/cuiHlu.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//first reverse the string, second reverse the word
2+
void reverseWords(string &s) {
3+
if (s.empty())
4+
return;
5+
reverse(s.begin(), s.end());
6+
int length = s.size();
7+
int storeIndex = 0;
8+
for (int i = 0; i < length; ++i)
9+
{
10+
if (s[i] != ' ')
11+
{
12+
if (storeIndex != 0)
13+
{
14+
s[storeIndex++] = ' ';
15+
}
16+
int j = i;
17+
while (j < length && s[j] != ' ' )
18+
{
19+
s[storeIndex++] = s[j++];
20+
}
21+
reverse(s.begin() + storeIndex - j + i, s.begin() + storeIndex);
22+
i = j;
23+
}
24+
}
25+
s.erase(s.begin() + storeIndex, s.end());
26+
}

0 commit comments

Comments
 (0)