题目
给定一个字符类型的数组chas,请在单词间做逆序调整,只要做到单词顺序逆序即可,对空格的位置没有特别要求。
举例
如果把chas看作字符串为"dog loves pig",调整为"pig loves dog"。
如果把chas看作字符串为"I'm a student.",调整为"student . a I'm"。
补充题目
给定一个字符类型的数组chas和一个整数size,请把大小为size的左半区调整移到右半区,右半区整体移到左边。
举例
如果把chas看作字符串为"ABCDE",size=3,调整成"DEABC"。
要求
如果chas长度为N,两道题都要求时间复杂度为O(N)。额外空间复杂度为O(1)。
java
public void rotate(char[] chas){
if (chas == null || chas.length == 0){
return;
}
reverse(chas,0,chas.length-1);
int l = -1;
int r = -1;
for (int i = 0; i < chas.length; i++) {
if (chas[i] != ' ') {
//先计算出左边游标的位置
l = i == 0 || chas[i-1] == ' '? i : l;
//计算出右边游标的位置
r = i == chas.length-1 || chas[i+1] == ' '? i : r;
}
//如果左右游标都有值,则翻转
if (l != -1 && r != -1){
reverse(chas,l,r);
l = -1;
r = -1;
}
}
}
public static void reverse(char[] chas,int start,int end){
char temp = 0;
while(start < end){
temp = chas[start];
chas[start] = chas[end];
chas[end] = temp;
start++;
end--;
}
}
public static void reverse(char[] chas,int size){
if (chas == null || chas.length == 0 || size >= chas.length || size <= 0){
return;
}
reverse(chas,0,size-1);
reverse(chas,size,chas.length-1);
reverse(chas,0,chas.length-1);
}
public static void rotate2(char[] chas,int size) {
if (chas == null || chas.length == 0 || size >= chas.length || size <= 0) {
return;
}
int start = 0;
int end = chas.length - 1;
int lpart = size;
int rpart = chas.length - size;
int s = Math.min(lpart, rpart);
int d = lpart-rpart;
while (true) {
exchange(chas, start, end, s);
if (d == 0) {
break;
}
if (d > 0) {
start += s;
lpart = d;
} else {
end -= s;
rpart = -d;
}
s = Math.min(lpart, rpart);
d = lpart - rpart;
}
}
public static void exchange(char[] chas,int start,int end,int size){
int i = end - size + 1;
char tmp = 0;
while(size-- != 0){
tmp = chas[start];
chas[start] = chas[i];
chas[i] = tmp;
start++;
i++;
}
}