9-line C++ Solution, Generating from Ground

  • 0

    I see many solutions start with "122". I think mine is more general, although this generalization is not required for this problem.

    class Solution {
        int magicalString(int n) {
            string s;
            bool one=true;
            int index=0;
            while(s.size()<n) {
                s[index++]=='2'? s.push_back(one?'1':'2') : (void)0;
            return count(s.begin(), s.begin() + n, '1');

Log in to reply

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