3ms C++ solution, easy to understand


  • 0
    C

    class Solution {
    public:
    vector<int> grayCode(int n) {
    vector<int> result;
    int size, inc;
    if(n<0)
    return result;
    result.push_back(0);
    if(n==0)
    return result;

        for(int i=0; i<n; i++){
            size = result.size();
            inc = 1 << i;
            for(int j=size-1; j!=-1; j--)
                result.push_back(result[j] + inc);
        }
        return result;
    }
    

    };


Log in to reply
 

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