LeetCode每日一题2021.8.21-压缩字符串
#class Solution {
public int putnum(char[] chars,int i,int num){//返回值是下次写入的坐标点
if(i>=chars.length || num<0){
return i;
}else{
int j=0;
if(num<10){
chars[i]=String.valueOf(num).charAt(0);
return i+1;
}else{
j=putnum(chars,i,num/10);
j=putnum(chars,j,num%10);
return j;
}
}
}
public int compress(char[] chars) {
if(chars.length<=1)
return chars.length;
char temp=‘a’;
if(chars[0]‘a’)
temp=‘b’;
int i=0;
int j=0;
int count=0;
while(i<chars.length){
if(chars[i]!=temp){//开始判断
if(count0){
temp=chars[i];
i++;
count=1;
continue;
}else{
if(count1){
chars[j]=temp;
j++;
}else{
chars[j]=temp;
j=putnum(chars,j+1,count);
}
}
temp=chars[i];
count=1;
}else{
count++;
}
i++;
}
System.out.println(count);
System.out.println(j);
if(count1){
chars[j]=temp;
return j+1;
} else{
chars[j]=temp;
j=putnum(chars,j+1,count);
}
return j;
}
}