0038.cpp (580B)
1 class Solution { 2 public: 3 string countAndSay(int n) { 4 string crnt = "1"; 5 for (int i = 1; i < n; i++) { 6 string res = ""; 7 int c = crnt.front(), count = 1; 8 for (int i = 1; i < crnt.size(); i++) { 9 if (crnt[i] == c) 10 count++; 11 else { 12 res += to_string(count) + (char)c; 13 count = 1; 14 c = crnt[i]; 15 } 16 } 17 crnt = res + to_string(count) + (char)c; 18 } 19 return crnt; 20 } 21 };