Rotate String
1 public class Solution { 2 /** 3 * @param str: an array of char 4 * @param offset: an integer 5 * @return: nothing 6 */ 7 public static char[] rotateString(char[] str, int offset) { 8 if (str == null || str.length == 0){ 9 return str; 10 } 11 int len = str.length; 12 offset %= len; 13 reverse(str, 0, len - offset - 1); 14 reverse(str, len - offset, len - 1); 15 reverse(str, 0, len - 1); 16 return str; 17 } 18 public static void reverse(char[] str, int start, int end){ 19 while (start < end){ 20 char temp = str[start]; 21 str[start] = str[end]; 22 str[end] = temp; 23 start++; 24 end--; 25 } 26 } 27 }