解题报告:
直接模拟。
class Solution {
public:int compress(vector<char>& chars) {int p = 0;for(int i = 0; i<chars.size();) {int j = i+1;while(j<chars.size() && chars[j] == chars[i]) j++;chars[p++] = chars[i];if(j-i > 1) {int cnt = j-i;vector<int> tmp;//其中最多三个元素while(cnt) {tmp.push_back(cnt%10);cnt /= 10;} reverse(tmp.begin(), tmp.end());for(int k = 0; k<tmp.size(); k++) chars[p++] = tmp[k]+'0';}i=j;}return p;}
};