Leetcode 443. String Compression | | Leetcode 443. String Compression 文章作者:Tyan博客:noahsnail.com | CSDN | 简书 1. Description 2. Solution1234567891011121314151617181920212223242526272829303132333435363738class Solution {public: int compress(vector<char>& chars) { if(chars.size() == 0 || chars.size() == 1) { return chars.size(); } int count = 1; int index = 0; char current = chars[0]; for(int i = 1; i < chars.size(); i++) { if(chars[i] != current) { chars[index] = current; index++; if(count > 1) { string temp = to_string(count); for(char ch : temp) { chars[index] = ch; index++; } } count = 1; current = chars[i]; } else { count++; } } chars[index] = current; index++; if(count > 1) { for(char ch : to_string(count)) { chars[index] = ch; index++; } } return index; }}; Reference https://leetcode.com/problems/string-compression/description/ 如果有收获,可以请我喝杯咖啡! 赏 微信打赏 支付宝打赏