东莞网站关键排名优秀营销软文100篇
题目链接
思路:双指针,一个记录写的位置,一个记录读的位置。
代码:
class Solution {public int compress(char[] chars) {int p = 0;//记录写的位置int index,count;//index:记录重复的字母的个数的 位数 count:记录重复的字母的个数char[] counts = new char[4];//例如 将102转为 '1','0','2'for(int i = 0;i < chars.length;i++){//i:记录读位置chars[p] = chars[i];count = 1;//计数while(i<chars.length-1 && chars[i] == chars[i+1]){i++;count++;}//转char数组while(count!=0){counts[index-1] = (char)(count%10+48);index--;count /= 10;}//进行赋值if(index!=3||counts[3]!='1'){//进行逐个的赋值记得将index复位while(index!=4){chars[++p] = counts[index];index++;}p++;continue;}//将index复位index++;p++;}return p;}
}