C# - recursive backtracking


  • 0
        public IList<IList<int>> Combine(int n, int k) 
        {
            IList<IList<int>> lists = new List<IList<int>>();
            NextList(lists, n, new int[k], 0);
            return lists;
        }
        
        public void NextList(IList<IList<int>> lists, int n, int[] curr, int currLen)
        {
            int k = curr.Length;
            if (currLen == k)
            {
                lists.Add(new List<int>(curr));
                return;
            }
            
            int start = currLen == 0 ? 1 : curr[currLen - 1] + 1;
            int end = n - k + currLen + 1;
            for (int i = start; i <= end; i++)
            {
                curr[currLen] = i;
                NextList(lists, n, curr, currLen + 1);
            }
        }
    

Log in to reply
 

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