Leetcode 89 Solution

This article provides solution to leetcode question 89 (gray-code)

https://leetcode.com/problems/gray-code

Solution

class Solution {
public:
    vector<int> grayCode(int n) {
        vector<int> res;

        res.push_back(0);

        for (int i = 0; i < n; i++)
        {
            int m = res.size();

            for (int j = m - 1; j >= 0; j--)
            {
                res.push_back(res[j] | (1 << i));
            }
        }

        return res;
    }
};