1.题目
Given an array of characters, compress it in-place.
The length after compression must always be smaller than or equal to the original array.
Every element of the array should be a character (not int) of length 1.
After you are done modifying the input array in-place, return the new length of the array.
Follow up:
Could you solve it using only O(1) extra space?
Example 1:
Input: ["a","a","b","b","c","c","c"] Output: Return 6, and the first 6 characters of the input array should be: ["a","2","b","2","c","3"] Explanation: "aa" is replaced by "a2". "bb" is replaced by "b2". "ccc" is replaced by "c3".
Example 2:
Input: ["a"] Output: Return 1, and the first 1 characters of the input array should be: ["a"] Explanation: Nothing is replaced.
Example 3:
Input: ["a","b","b","b","b","b","b","b","b","b","b","b","b"] Output: Return 4, and the first 4 characters of the input array should be: ["a","b","1","2"]. Explanation: Since the character "a" does not repeat, it is not compressed. "bbbbbbbbbbbb" is replaced by "b12". Notice each digit has it's own entry in the array.
Note:
- All characters have an ASCII value in
[35, 126]
. 1 <= len(chars) <= 1000
.
2.思路
超时思路:第一次我想到的也是新建一个数组,但是很想知道在原数组,能否实现。具体思路是,发现与当前char不一致时,统计当前char循环了多少次,更新原数组的值(填入重复次数转换成的char),并进行左移,(紧接数字char)。
如 a,a,a,b,b,c,c 第一次移动后为 a,3,b,b,c,c,c.下一次循环时,并不读取所有字符,而是读取移动后的字符长度。例如对上一个例子来说,a,a,a,b,b,c,c,c是从第一个b开始左移1为得到的,那么循环的终点值也相应减1.
不知道我有没有说清楚,反正这个算法有点凌乱,61/70 case的时候超时了。
正确思路:新建一个数组res,拷贝当前的char值,当循环遍历发现与当前char值不一致时,将char的重复次数进行转换——如果为1,则不需要进行变动;否则进行转换,如重复了123次,则将'1','2','3'插入res数组的正确位置中.
int转char :char a=(char) (8+48); 即将8(int)转换成了'8'.
3.算法
(如果感兴趣的话,注释掉的是超时的代码)
public static int compress(char[] chars) {
int n=chars.length;
if(n==0||n==1)
return n;
// int res=0;//return this number
// int repeat=1;//for every element in the arrays
// char temp=chars[0];
// int temp_head=0;
// int current=1;//current index of the arrays
// int n1=n;
// while(current<n1){
// if(chars[current]==temp){
// repeat++;
// current++;
// }else{
// if(repeat==1){
// System.out.println("repeat ==1");
// res+=1;
// temp=chars[current];
// temp_head=current;
// current=current+1;
// }
// if(repeat!=1){
// System.out.println("repeat !=1");
// temp=chars[current];
// res=res+1+lenOfChar(repeat);
// int len=lenOfChar(repeat);
// for(int i=len-1;i>=0;i--){
// int c=(int)(repeat/(Math.pow(10,i)));
// repeat=(int)(repeat-c*Math.pow(10,i));
// chars[temp_head+len-i]=(char) (c + 48);
// }
// for(int i=current;i<n1;i++){
// chars[i-current+temp_head+1+len]=chars[i];
// }
// for(int i=0;i<n;i++){
// System.out.print(chars[i]);
// }
// n1=n1-(current-(temp_head+len)-1);
// System.out.println();
// System.out.println("n1 "+n1);
// current=temp_head+len+1;
// System.out.println("current "+current);
// System.out.println();
// repeat=1;
// temp_head=current;
// current=current+1;
// }
// }
// }
// if(repeat==1)
// res++;
// if(repeat!=1){
// res=res+1+lenOfChar(repeat);
// int len=lenOfChar(repeat);
// for(int i=len-1;i>=0;i--){
// int c=(int)(repeat/(Math.pow(10,i)));
// repeat=(int)(repeat-c*Math.pow(10,i));
// chars[temp_head+len-i]=(char) (c + 48);
// }
// }
// for(int i=0;i<n;i++){
// System.out.print(chars[i]);
// }
// return res;
char res[]=new char[n];
res[0]=chars[0];
int res_index=1;
char temp=chars[0];
int count=1;
for(int i=1;i<n;i++){
if(temp==chars[i]){
count+=1;
}else{
if(count!=1){
int len=lenOfChar(count);
for(int j=len-1;j>=0;j--){
int c=(int)(count/(Math.pow(10,j)));
count=(int)(count-c*Math.pow(10,j));
res[res_index++]=(char) (c + 48);
}
count=1;
}
res[res_index++]=chars[i];
temp=chars[i];
}
}
if(count!=1){
int len=lenOfChar(count);
for(int j=len-1;j>=0;j--){
int c=(int)(count/(Math.pow(10,j)));
count=(int)(count-c*Math.pow(10,j));
res[res_index++]=(char) (c + 48);
}
}
for(int i=0;i<n;i++){
chars[i]=res[i];
}
return res_index;
}
private static int lenOfChar(int count){
int res=1;
while(count/10!=0){
res+=1;
count/=10;
}
return res;
}