笑了,返回字符串数组末尾加上’\0’会报
AddressSanitizer: heap-buffer-overflow on address
开两个数组,一个记录字符,一个记录个数,mark标记上一字符,若不同则更新mark
#define f(i, a, b) for (int i = a; i <= b; i++)
int compress(char* chars, int charsSize){
int a[2000]={0},b[2000]={0},ac=0;
char mark='\0';
f(i,0,charsSize-1){
if(chars[i]!=mark){
a[ac]=chars[i];
b[ac++]=1;
mark=chars[i];
}
else if(chars[i]==mark){
b[ac-1]+=1;
}
}
int now=0;
f(j,0,ac-1){
if(b[j]==1)
chars[now++]=a[j];
else {
chars[now++]=a[j];
char temp[10];
sprintf(temp,"%d",b[j]);
f(k,0,strlen(temp)-1)
chars [now++]=temp[k];
}
}
return now;
}