Solution using a few lines


  • 0
    class Solution {
    public:
        vector<int> grayCode(int n) {
            int num=1<<n;
            vector<int>res(num,0);
            for(int i=1;i<num;++i){
                res[i]=i^(i>>1);
            }
            return res;
        }
    };

Log in to reply
 

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