diff options
Diffstat (limited to 'other/reverse_string3.cpp')
-rw-r--r-- | other/reverse_string3.cpp | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/other/reverse_string3.cpp b/other/reverse_string3.cpp new file mode 100644 index 0000000..0cc2cf5 --- /dev/null +++ b/other/reverse_string3.cpp @@ -0,0 +1,21 @@ +#include "leetcode.h" + +class Solution { +public: + string reverseWords(string s) { + int i = 0; + for (int j = 0; j < s.size(); ++j) { + if (s[j] == ' ') { + reverse(s.begin() + i, s.begin() + j); + i = j + 1; + } + } + reverse(s.begin() + i, s.end()); + return s; + } +}; + +int main() { + Solution obj; + cout << obj.reverseWords("Let's take LeetCode contest"); +} |