package offer;
/**
* offer interview 4
*/
public class Test04 {
public static int replaceBlank(char[] string,int usedLength){
if (null == string || string.length < usedLength){
return -1;
}
int whiteCount = 0;
for (int i = 0 ; i < usedLength ; i++){
if (string[i] == ' '){
whiteCount++;
}
}
int targetLength = whiteCount * 2 + usedLength;
int temp = targetLength;
if (targetLength > string.length){
return -1;
}
if (0 == whiteCount){
return usedLength;
}
usedLength--;
targetLength--;
while(usedLength >= 0 && usedLength < targetLength){
if (string[usedLength] == ' '){
string[targetLength--] = '0';
string[targetLength--] = '2';
string[targetLength--] = '%';
}else{
string[targetLength--] = string[usedLength];
}
usedLength--;
}
return temp;
}
public static void main(String[] args){
char[] string = new char[50];
string[0] = ' ';
string[1] = 'e';
string[2] = ' ';
string[3] = ' ';
string[4] = 'r';
string[5] = 'e';
string[6] = ' ';
string[7] = ' ';
string[8] = 'a';
string[9] = ' ';
string[10] = 'p';
string[11] = ' ';
int length = replaceBlank(string,12);
System.out.println(new String (string,0,length));
}
}
剑指offer-问题4
猜你喜欢
转载自blog.csdn.net/ma451152002/article/details/83141728
今日推荐
周排行