LeetCode 38. Count and Say
Solution1:我的答案
【C++】int转换为string的两种方法(to_string、字符串流)
怎么这么慢。。。
关于该问题的一个比较深入的分析连接:http://www.cnblogs.com/TenosDoIt/p/3776356.html
class Solution {
public:
string countAndSay(int n) {
string res = "1";
if (n == 1) return res;
else {
for (int i = 1; i < n; i++) {
string temp = "";
int slow = 0, fast = 0;
while (fast < res.size()) {
while (res[slow] == res[fast])
fast++;
temp = temp + to_string(fast - slow) + res[slow];
slow = fast;
}
res = temp;
}
}
return res;
}
};