diff options
Diffstat (limited to 'other/reverse_string2.cpp')
-rw-r--r-- | other/reverse_string2.cpp | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/other/reverse_string2.cpp b/other/reverse_string2.cpp new file mode 100644 index 0000000..839a26f --- /dev/null +++ b/other/reverse_string2.cpp @@ -0,0 +1,34 @@ +#include "leetcode.h" + +class Solution { +public: + string reverseStr(string s, int k) { + int len = s.size(), rem = len % (2 * k); + int start = 0, end = 2 * k, actual = len - rem; + string ans = ""; + while (actual) { + string tmp = s.substr(start, end); + reverse(tmp.begin(), tmp.begin() + k); + start += 2 * k; + ans += tmp; + actual -= 2 * k; + } + if (rem > 0) { + if (rem < k) { + string tmp = s.substr(len - rem, rem); + reverse(tmp.begin(), tmp.end()); + ans += tmp; + } else if (rem <= 2 * k) { + string tmp = s.substr(len - rem, rem); + reverse(tmp.begin(), tmp.begin() + k); + ans += tmp; + } + } + return ans; + } +}; + +int main() { + Solution obj; + cout << obj.reverseStr("abcdefg", 2); +} |