List<decimal> FindSumSubset(decimal sum, List<decimal> list)
{
for (int i = 0; i < list.Count; i++)
{
decimal value = list[i];
if (sum - value == 0.0m)
{
return new List<decimal> { value };
}
else
{
var subset = FindSumSubset(sum - value, list.GetRange(i + 1, list.Count -i));
if (subset != null)
{
return subset.Add(value);
}
}
}
return null;
}
我在這條線得到一個錯誤:c#我如何返回一個通用列表?
return subset.Add(value);
錯誤:
Error 1 Cannot implicitly convert type 'void' to 'System.Collections.Generic.List<decimal>'
任何人知道我可以解決這個>?
考慮使用發電機功能(或 「迭代器塊」)在這裏。 (這些東西用'yield return'在IEnumerable中返回)。 – 2010-11-11 22:15:15