C# version - O(2^n)


  • 0
    F
    public class Solution
    {
        public IList<int> GrayCode(int n)
        {
            if (n == 0) return new List<int> { 0 };
            IList<int> ans = new List<int> { 0, 1 };
            if (n == 1) return ans;
            for (int i = 2; i <= n; ++i)
            {
                for (int j = (int)Math.Pow(2, (i - 1)) - 1; j >= 0; --j)
                {
                    int k = 1 << (i - 1);
                    ans.Add(ans[j] | k);
                }
            }
            return ans;
        }
    }
    

  • 0
    F

    0_1477114877865_QQ截图20161022133840.png


Log in to reply
 

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