for Robot Artificial Inteligence

19. count and say[난이도上]

|

  • to_string() <- integer를 string으로 바꿔주는 함수
class Solution {
public:
    string countAndSay(int n) {
        if(n==0)
            return "";
        string res = "1";
        while(--n)
        {
            string current = "";
            for(int i =0; i<res.size();i++)
            {
                //first is size 1
                int count =1;
                while((i+1<res.size())&&(res[i]==res[i+1]))
                {
                    // it is false for first res however, after it is activated.
                    count++;
                    i++;
                }
                current += to_string(count) + res[i];
            }
            res = current;
        }
        return res;
    }
};

Comments