给定一个字符串和一个偏移量,根据偏移量旋转字符串(从左向右旋转)
您在真实的面试中是否遇到过这个题?
Yes
样例
对于字符串 "abcdefg".
offset=0 => "abcdefg"
offset=1 => "gabcdef"
offset=2 => "fgabcde"
offset=3 => "efgabcd"
class Solution {
public:
/**
* @param str: a string
* @param offset: an integer
* @return: nothing
*/
void rotateString(string &str, int offset) {
//wirte your code here
if(str.size()==0){
return ;
}
if (offset == 0 || offset % str.length()==0) {
return ;
}
if (offset > str.length()) {
offset = offset%str.length();
}
int newbegin = str.length() - offset;
int newend = str.length();
string res(str.begin()+newbegin, str.end());
string res1(str.begin(), str.begin() + newbegin);
//res.insert(str.end(), str.begin(), str.begin() + newbegin);
str = res+res1;
}
};