leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0443.cpp (670B)


0 class Solution { 1 public: 2 int compress(vector<char> &chars) { 3 char prev = chars[0]; 4 int count = 1, crnt = 0; 5 for (int i = 1; i < chars.size(); i++) { 6 if (chars[i] == prev) 7 count++; 8 else { 9 chars[crnt++] = prev; 10 if (count != 1) 11 for (char c : to_string(count)) 12 chars[crnt++] = c; 13 count = 1; 14 prev = chars[i]; 15 } 16 } 17 chars[crnt++] = prev; 18 if (count != 1) 19 for (char c : to_string(count)) 20 chars[crnt++] = c; 21 22 return crnt; 23 } 24 };