//implement this function iteratively
class Solution {
public:
string countAndSay(int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
string s="1";
while(--n>0){
s=countAndSay(s);
}
return s;
}
string countAndSay(string s){
if(s.size()<1)
return NULL;
stringstream out;
int count=1;
char ch=s[0];
for(int i=1;i<s.size();++i){
if(s[i]==ch){
++count;
} else {
out<<count<<ch;
count=1;
ch=s[i];
}
}
out<<count<<ch;
return out.str();
}
};
Leetcode: Count and Say
最新推荐文章于 2018-01-05 15:07:25 发布