C# solution


  • 0
    K
    public class Solution {
        public IList<IList<int>> Combine(int n, int k)
        {
            IList<IList<int>> templist  = new List<IList<int>>();
            IList<IList<int>> newlist  = new List<IList<int>>();
            if (k==1) {
                for (int i=1;i<=n;i++) {
                    IList<int> row = new List<int>();
                    row.Add(i);
                    newlist.Add(row); 
                }
                return newlist;
            }
            else {
                newlist = Combine(n-1,k-1);
                for (int i=0;i<newlist.Count;i++)
                    newlist[i].Add(n);
                    
                if (n-1>=k) {
                    templist = Combine(n-1,k);
                    for (int i=0;i<templist.Count;i++)
                        newlist.Add(templist[i]);
                }
                return newlist;
            }
        }
    }
    

Log in to reply
 

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