Simple 0ms C++ solution


  • 0
    C
    class Solution {
    public:
        string say(string str) {
            string rs = "";
            char count = '1';
            char mark = str[0];
            for (int i = 1; i < str.length(); ++i) {
                if (str[i] == mark) {
                    ++count;
                } else {
                    rs += count;
                    rs += mark;
                    mark = str[i];
                    count = '1';
                }
            }
            rs += count;
            rs += mark;
            
            return rs;
        }
        string countAndSay(int n) {
            string rs = "1";
            
            for (int i = 1; i < n; ++i)
                rs = say(rs);
                
            return rs;
        }
    };
    

Log in to reply
 

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