[Lintcode] 旋转字符串

 对于字符串 "abcdefg".

offset=0 => "abcdefg"
offset=1 => "gabcdef"
offset=2 => "fgabcde"
offset=3 => "efgabcd"

 1 class Solution {
 2     public:
 3         /**
 4          * @param str: An array of char
 5          * @param offset: An integer
 6          * @return: nothing
 7          */
 8         void rotateString(string &str, int offset) {
 9             // write your code here
10             if (str.length() == 0)
11                 return ;
12                 
13             offset = offset % str.length(); 14 int len = str.length(); 15 16 string sub1 = str.substr((len-offset), offset); 17 18 string sub2 = str.substr(0, (len-offset)); 19 20  sub1.append(sub2); 21  str.swap(sub1); 22  } 23 };
 

猜你喜欢

转载自www.cnblogs.com/icoder/p/9465797.html