420. Count and Say

string countAndSay(int n) { // write your code here string res = "1"; int cnt = 0; char start; for(int i = 2; i <= n; i++){ string temp = ""; int j = 0; while(j < res.size()){ cnt = 1; start = res[j]; while(j < res.size() - 1 && res[j] == res[j + 1]){ cnt++; j++; } temp += (to_string(cnt) + (start)); ++j; } //temp += (to_string(cnt) + to_string(start)); res = temp; } return res; }

Comments

Popular posts from this blog

算法的比较