Memory limited , why?


  • 0
    Z
    class Solution {
    public:
        string countAndSay(int n) {
            string res = "1";
            string cur = res;
            stringstream ss;
            while(--n) {
                res = "";
                int cnt = 1;
                int num = cur[0]-'0';
                for (int i=1; i<cur.length(); i++) {
                    if (cur[i] == cur[i-1])
                    {
                        cnt++;
                    } else {
                        ss <<cnt<<num;
                        res += ss.str();
                        ss.clear();
                        cnt = 1;
                        num = cur[i]-'0';
                    }
                }
                ss<<cnt<<num;
                res += ss.str();
                cur = res;
            }
            return res;
        }
    };

Log in to reply
 

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