在 Java 中交换数组中的三元组
Swap triplets in an array in Java
我正在尝试在 Java 中执行以下过程。我有一个数组,其中每个元素都是一个三元组。例如:
int [][] a = { {0,1,0},{1,2,1},{1,0,0},{0,2,0} };
我想交换数组中的每个三元组(与右侧的其他三元组)以获得以下每个矩阵:
b = { {1,2,1},{0,1,0},{1,0,0},{0,2,0} };
c = { {1,0,0},{1,2,1},{0,1,0},{0,2,0} };
d = { {0,2,0},{1,2,1},{1,0,0},{0,1,0} };
e = { {0,1,0},{{1,0,0},{1,2,1},{0,2,0} };
f = { {0,1,0},{0,2,0},{1,0,0},{1,2,1} };
g = { {0,1,0},{1,2,1},{0,2,0},{1,0,0} };
一般来说,对于 k 个三元组的矩阵,有 [(k*(k-1))/2] 种可能的交换。
我该如何解决这个问题?
doubly-nested 循环应该可以在这里工作。请注意,您要求的输出实际上是一个 3D 数组(2D 数组的数组):
public int[][] copy2DArray (int[][] input) {
int[][] output = new int[input.length][];
for (int r=0; r < input.length; ++r) {
output[r] = new int[input[r].length];
for (int c=0; c < input[0].length; ++c) {
output[r][c] = input[r][c];
}
}
return output;
}
public static void main(String[] args) {
int [][] a = { {0,1,0},{1,2,1},{1,0,0},{0,2,0} };
int numSwaps = a.length*(a.length-1) / 2;
int[][][] result = new int[numSwaps][][];
int counter = 0;
for (int i=0; i < a.length-1; ++i) {
for (int j=i+1; j < a.length; ++j) {
result[counter] = copy2DArray(a);
int[] temp = result[counter][j];
result[counter][j] = result[counter][i];
result[counter][i] = temp;
++counter;
}
}
System.out.println(Arrays.deepToString(result));
}
这会打印:
[
[[1, 2, 1], [0, 1, 0], [1, 0, 0], [0, 2, 0]],
[[1, 0, 0], [1, 2, 1], [0, 1, 0], [0, 2, 0]],
[[0, 2, 0], [1, 2, 1], [1, 0, 0], [0, 1, 0]],
[[0, 1, 0], [1, 0, 0], [1, 2, 1], [0, 2, 0]],
[[0, 1, 0], [0, 2, 0], [1, 0, 0], [1, 2, 1]],
[[0, 1, 0], [1, 2, 1], [0, 2, 0], [1, 0, 0]]
]
对于一些笔记,我过去的策略是循环遍历所有头寸交换头寸,使用两级for
循环。对于每个可能的交换,我们首先克隆您的输入 2D a
数组。然后,我们在选择的任何位置交换各个一维数组。最后,我们将该交换数组添加到 3D 结果数组。我们也可以使用类似列表的东西来存储交换的二维数组。
我正在尝试在 Java 中执行以下过程。我有一个数组,其中每个元素都是一个三元组。例如:
int [][] a = { {0,1,0},{1,2,1},{1,0,0},{0,2,0} };
我想交换数组中的每个三元组(与右侧的其他三元组)以获得以下每个矩阵:
b = { {1,2,1},{0,1,0},{1,0,0},{0,2,0} };
c = { {1,0,0},{1,2,1},{0,1,0},{0,2,0} };
d = { {0,2,0},{1,2,1},{1,0,0},{0,1,0} };
e = { {0,1,0},{{1,0,0},{1,2,1},{0,2,0} };
f = { {0,1,0},{0,2,0},{1,0,0},{1,2,1} };
g = { {0,1,0},{1,2,1},{0,2,0},{1,0,0} };
一般来说,对于 k 个三元组的矩阵,有 [(k*(k-1))/2] 种可能的交换。
我该如何解决这个问题?
doubly-nested 循环应该可以在这里工作。请注意,您要求的输出实际上是一个 3D 数组(2D 数组的数组):
public int[][] copy2DArray (int[][] input) {
int[][] output = new int[input.length][];
for (int r=0; r < input.length; ++r) {
output[r] = new int[input[r].length];
for (int c=0; c < input[0].length; ++c) {
output[r][c] = input[r][c];
}
}
return output;
}
public static void main(String[] args) {
int [][] a = { {0,1,0},{1,2,1},{1,0,0},{0,2,0} };
int numSwaps = a.length*(a.length-1) / 2;
int[][][] result = new int[numSwaps][][];
int counter = 0;
for (int i=0; i < a.length-1; ++i) {
for (int j=i+1; j < a.length; ++j) {
result[counter] = copy2DArray(a);
int[] temp = result[counter][j];
result[counter][j] = result[counter][i];
result[counter][i] = temp;
++counter;
}
}
System.out.println(Arrays.deepToString(result));
}
这会打印:
[
[[1, 2, 1], [0, 1, 0], [1, 0, 0], [0, 2, 0]],
[[1, 0, 0], [1, 2, 1], [0, 1, 0], [0, 2, 0]],
[[0, 2, 0], [1, 2, 1], [1, 0, 0], [0, 1, 0]],
[[0, 1, 0], [1, 0, 0], [1, 2, 1], [0, 2, 0]],
[[0, 1, 0], [0, 2, 0], [1, 0, 0], [1, 2, 1]],
[[0, 1, 0], [1, 2, 1], [0, 2, 0], [1, 0, 0]]
]
对于一些笔记,我过去的策略是循环遍历所有头寸交换头寸,使用两级for
循环。对于每个可能的交换,我们首先克隆您的输入 2D a
数组。然后,我们在选择的任何位置交换各个一维数组。最后,我们将该交换数组添加到 3D 结果数组。我们也可以使用类似列表的东西来存储交换的二维数组。