题目:
思路:
把[0,k-1]位置和[k,arr.length-1]位置的字符串逆序,再把整体逆序即为最后结果
代码:
public static void reverse(char[] chas, int start, int end) {
char tmp = 0;
while (start < end) {
tmp = chas[start];
chas[start] = chas[end];
chas[end] = tmp;
start++;
end--;
}
}
public static void rotate1(char[] chas, int size) {
if (chas == null || size <= 0 || size >= chas.length) {
return;
}
reverse(chas, 0, size - 1);
reverse(chas, size, chas.length - 1);
reverse(chas, 0, chas.length - 1);
}