Linq solution - one liner


  • 0
    A
    public class Solution {
        public IList<int> GrayCode(int n) 
        {
            return Enumerable
                   .Range(0, n)
                   .Aggregate(
                       new List<int> { 0 },
                       (acc, x) => {
                           return acc
                                     .Concat(
                                          acc
                                          .AsEnumerable()
                                          .Reverse()
                                          .Select(k => k + acc.Count()))
                                     .ToList();
                       });
        }
    }

Log in to reply
 

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