Leetcode 之Count and Say(35)
很有意思的一道题,不好想啊。
string getNext(string &s) { char start = s[0]; int count = 0; stringstream ss; for (int i = 0; i < s.size(); i++) { if (start == s[i]) { count++; } else { ss << count << start; count = 1; start = s[i]; } } ss << count << start; return ss.str(); } string countAndSay(int n) { string s = "1"; for (int i = 0; i < n; i++) s = getNext(s); }