1. 程式人生 > >【LeetCode】38.報數

【LeetCode】38.報數

ring return say AS ++ pub () highlight ret

class Solution {
public:
    string countAndSay(int n) {
        string str,ans;
        str="1";
        if(n--==1) return str;
        while(n--){
            ans.clear();
            int left=0,right=0;
            while(right<str.size()){
                if(str[right]==str[left]) right++;
                else{
                    ans+=(right-left+‘0‘);
                    ans+=str[left];
                    left=right;
                    right++;
                }
            }
            ans+=(right-left+‘0‘);
            ans+=str[left];
            str=ans;
        }
        
        return ans;
    }
};

  

【LeetCode】38.報數