1 static int wing=[]() 2 { 3 std::ios::sync_with_stdio(false); 4 cin.tie(NULL); 5 return 0; 6 }(); 7 8 class Solution 9 { 10 public: 11 int compress(vector<char>& chars) 12 { 13 14 int count=0; 15 int i=0,j=0; 16 int sz=chars.size(); 17 while(j<sz) 18 { 19 count=0; 20 char cur=chars[j]; 21 while(chars[j]==cur) 22 { 23 count++; 24 j++; 25 if(j==sz) 26 break; 27 } 28 if(count==1) 29 { 30 chars[i++]=cur; 31 continue; 32 } 33 chars[i]=cur; 34 i++; 35 string s=to_string(count); 36 for(char c:s) 37 chars[i++]=c; 38 } 39 return i; 40 } 41 };
双指针扫描字符串并压缩