【发布时间】:2020-09-01 11:41:35
【问题描述】:
我有一个整数数组,例如:
int[][] matrix = new int[][] {
new int[2], // array a,
new int[2], // array b,
new int[3], // array c,
new int[4], // array d,
// ... // array x
};
现在我想生成从每个数组 a、b、c、... x 中选择一个的所有可能的索引组合
这意味着在例子中:{new int[2], new int[2], new int[3]},我想得到这样的索引组合:
{a[0], b[0], c[0]}
{a[0], b[0], c[1]}
{a[0], b[0], c[2]}
{a[0], b[1], c[0]}
{a[0], b[1], c[1]}
{a[0], b[1], c[2]}
{a[1], b[0], c[0]}
{a[1], b[0], c[1]}
{a[1], b[0], c[2]}
{a[1], b[1], c[0]}
{a[1], b[1], c[1]}
{a[1], b[1], c[2]}
矩阵的长度未知,但矩阵中的每个数组至少有1个元素。
有没有人可以解决这个问题?
【问题讨论】:
-
请您自己先试试。当您遇到特定问题时,请随时在此处提问。 SO 不是代码编写服务。
-
我认为这不是代码编写问题,而是寻找有效算法的问题。我越想这个问题我就越困惑。据我了解,不可能使用经典排列来解决这个问题。我说的对吗?
-
@JulianHerbel:我不明白。你的数组有 3 个索引,0、1 和 2。但是你的结果数组包含 1,2 和 3。你的源数组的值根本不重要?
-
@TimSchmelter:也许我的问题有点令人困惑。我想要数组中所有可能的索引组合。看看treesong的答案。他解决了。再次感谢