Short solution in C++ 4ms solution


  • 0
    S
    class Solution {
    public:
        vector<int> grayCode(int n) {
            std::vector<int>ans;
            n =pow(2,n)-1;
            for(int i=0;i<=n;i++)
            {
                int g = i^(i/2);
                ans.push_back(g);
            }
            return ans;
        }
    };
    

Log in to reply
 

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