C++ code converted from [JAVA ----- Easy Version to Understand!!!!]


  • 0
    D
    class Solution {
    public:
        vector<int> grayCode(int n) {
            vector<int> result;
            if(n<0) return result;
            if(n==0) {result.push_back(0); return result;}
            result = grayCode(n-1);
            //cout<<result.size();
            int x=1<<n-1;
            for(int i=result.size()-1;i>=0;i--) { //reversing the list
                result.push_back(x+result[i]);
            }
            return result;
        }
    };

Log in to reply
 

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