We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 417021d + ab07259 commit 32bc4ceCopy full SHA for 32bc4ce
2018.12.2-leetcode557/神秘的火柴人.md
@@ -0,0 +1,44 @@
1
+...
2
+class Solution {
3
+public:
4
+ string reverseWords(string s) {
5
+ string ret;
6
+
7
+ int cur = 0;
8
+ while(cur < s.length())
9
+ {
10
+ int begin = cur;
11
+ int end = s.find(" ", begin);
12
+ if(end == s.npos)
13
+ end = s.length();
14
15
+ string tmp = s.substr(begin, end-begin);
16
+ reversOne(tmp);
17
+ if(end != s.length())
18
+ ret = ret + tmp + " " ;
19
+ else
20
+ ret = ret + tmp;
21
22
+ cur = end+1;
23
+ }
24
25
+ return ret;
26
27
28
29
+ void reversOne(string &s)
30
31
+ int n = s.length();
32
33
+ int i = 0;
34
+ int j = n-1;
35
+ while(i < j)
36
37
+ swap(s[i], s[j]);
38
+ i++;
39
+ j--;
40
41
42
43
+};
44
0 commit comments