using System;
using System.Collections.Generic;
class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param k int整型
* @param n int整型
* @return int整型二维数组
*/
public List<List<int>> combination (int k, int n) {
// write code here
List<List<int>> lslsN = new List<List<int>>();
List<int> lsN = new List<int>();
DG(k, n, ref lslsN, ref lsN, 0);
return lslsN;
}
public static void DG(int k, int n, ref List<List<int>> lslsN,
ref List<int> lsN, int nCurIndex) {
// write code here
int nMaxLen = n - (1 + k) * k / 2;
for (int nIndex = nCurIndex + 1; nIndex <= k + nMaxLen; nIndex++) {
List<int> lsNCpy = new List<int>();
lsNCpy.AddRange(lsN);
lsNCpy.Add(nIndex);
int nSum = 0;
lsNCpy.ForEach(r => {
nSum += r;
});
if (nSum > n)
break;
if (nSum == n && lsNCpy.Count == k) {
lslsN.Add(lsNCpy);
break;
}
DG(k, n, ref lslsN, ref lsNCpy, nIndex);
}
}
}