C++ easy to follow


  • 0
    G
    class Solution {
    public:
        
        string getCountAndSay(const string &s){
            string result;
            int count = 1;
            for(int i = 1; i <= s.size(); ++i){
                if(i == s.size() || s[i] != s[i- 1]){
                    ostringstream ss;
                    ss << count << s[i-1];
                    result += ss.str();
                    count = 1;
                }
                else{
                    count++;
                }
            }
            
            return result;
        }
        
        string countAndSay(int n) {
            string first = "1";
            
            for(int i = 1; i < n; ++i){
                first = getCountAndSay(first);
            }
            
            return first;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.