classSolution{publicintcompress(char[] chars){int len = chars.length;char cur =' ';int left =0, right =0;while(right < len){// 判断是否需要压缩if(cur != chars[right]){
chars[left++]= chars[right];
cur = chars[right++];}else{Integer num =1;// 压缩,向后移动遍历指针,直到字符不同while(right < len && chars[right]== cur){
right++;
num++;}// 将num转成字符数组,然后按位写入for(char c :String.valueOf(num).toCharArray()){
chars[left++]= c;};}}// 遍历完成后,修改指针的位置即为结果的长度return left;}}