Clean C++ gray code


  • 1
    L
    class Solution {
    public:
        vector<int> grayCode(int n) {
            
            vector<int> gray(1 << n, 0);
            for(int i = 0; i < n; i ++)
            {
                int count = 1 << (i + 1);        
                for(int j = count / 2; j < count; j ++)
                    gray[j] = (1 << i) | gray[count - 1 - j];
            }
            
            return gray;
        }
    };

Log in to reply
 

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