字符串循环移动-高效优雅算法 本算法详细说明:http://blog.csdn.net/maoyuanming0806/article/details/78932840代码:/**反转。把字符数组从from位置到to位置的数反转*/static void revertStr(char[] str, int fr
本算法详细说明:http://blog.csdn.net/maoyuanming0806/article/details/78932840
代码:
/**反转。把字符数组从from位置到to位置的数反转*/
static void revertStr(char[] str, int from, int to){
if(to - from < 1){
return;
}
for(int i = from; i < (to + from + 1)/2;i++){
swapChar(str,i,to -(i - from));
}
}
static void swapChar(char[] s, int i, int j) {
char temp = s[i];
s[i] = s[j];
s[j] = temp;
}
/**字符数组str中moveStrHead位置到moveStrEnd位置的字符向右循环移动moveLength位*/
static void circleLeftMove(char[] str, int moveStrHead, int moveStrEnd, int moveLength){
revertStr(str, moveStrHead, moveStrEnd);
revertStr(str, moveStrEnd+1, moveStrEnd + moveLength);
revertStr(str, moveStrHead, moveStrEnd + moveLength);
}
//===========测试:
public static void main(String[] args) {
String test = "123456789";
char[] charArray = test.toCharArray();
circleLeftMove(charArray, 0, 2, 2);
System.out.println(charArray);
}
输出:451236789
