Very simple c++ solution, 4ms


  • 0
    class Solution {
    public:
        vector<int> grayCode(int n) {
            vector<int> ret;
            ret.push_back(0);
            for(int i=1;i<(1<<n);i++)
                ret.push_back(ret.back() ^ (i&(-i)));
            return ret;
        }
    };

Log in to reply
 

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