class Solution {
public:
string countAndSay(int n) {
if(n < 0) return "";
string ans = "1";
string res = "";
for(int i = 1;i < n; i++){
int count = 0,j = 0,l=0 ;
res ="";
while(l < ans.size()){
while(ans[j] == ans[l] && j < ans.size()){
count++;
j++;
}
int f = 0;
while(count > 0){
f = f*10+count % 10;
count/=10;
}
while(f > 0){
res += char('0'+f%10);
f /= 10;
}
res += ans[l];
l = j;
}
swap(res,ans);
}
return ans;
}
};leetcode 38. Count and Say
最新推荐文章于 2025-12-16 18:10:48 发布
637

被折叠的 条评论
为什么被折叠?



