私は整数のリストを持っています。リスト内の整数リストをフィルタリングするにはどうすればいいですか?C#フィルタリングされた整数のリストのリスト
例: - L = [[0,1,2], [], [1], [1,2,3]]
。 linqを使用してL = [[0,1,2], [1], [1,2,3]]
を取得するにはどうすればフィルタリングできますか?
ここでは[0,1,2]
は整数のリストです。
これは私の以前の質問に関連しています。私は整数リストのリストを返す再帰関数を持っているので、空リストをフィルタリングする必要があります。
int [] num_list = new int[10]{2, 3, 5, 9, 14, 19, 23, 45, 92, 100};
public List<List<int>> find_solutions(int n, int t, int w)
{
if (n == 2)
{
List<int> s = new List<int>();
for (var i=0; i <= t; i++)
{
if (i * num_list[1] + (t - i) * num_list[0] == w)
{
s.Add(i);
s.Add(t - i);
}
}
return new List<List<int>> { s };
}
List<List<int>> f = new List<List<int>>();
List<List<int>> temp_list = new List<List<int>>();
for (int i=0; i <= Math.Min(t, w/num_list[n-1]); i++)
{
temp_list = find_solutions(n - 1, t - i, w - i * num_list[n - 1]);
// I strongly believe that i am getting empty list
// and I should filter out empty list form temp_list
// and I need to insert 'i' to each list inside list
foreach(List<int> c in temp_list)
{
c.Insert(0, i);
}
f.addRange(temp_list);
}
return f;
}
Er ...大文字と小文字の「L」は混在していると思います。 –
@JoachimIsaksson:その友達に感謝し、今すぐ修正しました。 –