class Solution {
public:
string countAndSay(int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
string str = "1";
string temp;
while (n > 1) {
temp.clear();
int num = 1;
for (int i = 1; i < str.size(); i++) {
if (str[i] == str[i - 1]) {
num++;
}
else {
temp.push_back('0' + num);
temp.push_back(str[i - 1]);
num = 1;
}
}
temp.push_back('0' + num);
temp.push_back(str.back());
str = temp;
n--;
}
return str;
}
};
LeetCode Count and Say
最新推荐文章于 2013-04-05 14:46:28 发布