38. 外观数列

    技术2025-10-10  4

    class Solution { public: string countAndSay(int n) { if(n==1){ return "1"; } string str=countAndSay(n-1);//递归把之前的那个数组搞出来 //之后用计数器,相等就加加然后改字符串!!!有了之前的字符串应该好做了吧 string res=""; int count=1; for(int i=0;i<str.length();i++){ if(str[i]==str[i+1]){ count++; } else{ res+=to_string(count); res+=str[i]; count=1; //continue; } //res+=to_string(count); //res+=str[i]; } return res; } };

    PS:注意如果用了continue并在函数末尾输出的话会在n=3时就出错了,等于多输出了一遍。 直接在最后不相等时写一个输出就好,反正count++了! 自己还是菜hhh

    Processed: 0.011, SQL: 9