Given a string and an offset, rotate string by offset. (rotate from left to right)
Example
这个题目比较简单的,但是没有考虑全面,忽略了两个点,一个是空字符串,一个是当数值超过数值长度,写下纪念
Given "abcdefg"
.
offset=0 => "abcdefg"
offset=1 => "gabcdef"
offset=2 => "fgabcde"
offset=3 => "efgabcd"
class Solution { public: /** * @param str: An array of char * @param offset: An integer * @return: nothing */ void rotateString(string &str, int offset) { if(!str.empty()){ int newoffset=offset%str.length(); string str1(str, str.length() - newoffset, str.length() ); str.erase(str.length() - newoffset, str.length()); str = str1 + str; } } };