标签:
Pretty straight forward. count numbers and put char.
1 class Solution { 2 public: 3 string getNext(string s) { 4 ostringstream oss; 5 char rec = s[0]; 6 int len = s.size(), num = 1; 7 for (int i = 1; i < len; i++) { 8 if (rec != s[i]) { 9 oss << char(num + ‘0‘) << rec; 10 rec = s[i]; 11 num = 1; 12 } else num++; 13 } 14 oss << char(num + ‘0‘) << rec; 15 return oss.str(); 16 } 17 string countAndSay(int n) { 18 string result = "1"; 19 for (int i = 2; i <= n; i++) { 20 result = getNext(result); 21 } 22 return result; 23 } 24 };
LeetCode – Refresh – Count and Say
标签:
原文地址:http://www.cnblogs.com/shuashuashua/p/4349360.html