Java 二维阵列对角填充
这是我的普通数组,但我需要使它像这样成对角线Java 二维阵列对角填充,java,arrays,Java,Arrays,这是我的普通数组,但我需要使它像这样成对角线 1 2 3 4 5 6 7 8 9 这是一种非常愚蠢的方法来让它工作,但即使它不工作,因为我无法找到第二列元素 1 2 4 3 5 7 6 8 9 for(i=0;i
1 2 3
4 5 6
7 8 9
这是一种非常愚蠢的方法来让它工作,但即使它不工作,因为我无法找到第二列元素
1 2 4
3 5 7
6 8 9
for(i=0;i
好吧,如果你为了填充模式枚举索引,你会得到
for (i = 0; i < arr.length; ++i) {
for (n = 0; n < arr[0].length; ++n) {
if (i == 0 && n == 0){
arr[i][n] = 0;
} else if (i == 0 && n == 1) {
arr[i][n] = 2;
} else if (i == 1 && n == 0) {
arr[i][n] = 3;
} else if (n == 0) {
arr[i][n] = arr[i - 1][n] - arr[i - 2][n] + 1 + arr[i - 1][n];
} else {
arr[i][n] = arr[i][n - 1] - arr[i][n - 2] + 1 + arr[i][n - 1];
}
}
}
因此,您需要遍历这两个索引的总和。也就是说,加总。如您所见,0,0
总计0,1,0
和0,1
总计1,依此类推。给我们这样的东西:
0,0
1,0
0,1
2,0
1,1
0,2
2,1
1,2
2,2
public class FillArray{
public static void main (String[] args){
int[][] array = {
{1,2,3},
{4,5,6},
{7,8,9}}; //This is your original array
int temp = 0; //declare a temp variable that will hold a swapped value
for (int i = 0; i < array[0].length; i++){
for (int j = 0; j < array[i].length; j++){
if (i < array.length - 1 && j == array[i].length - 1){ //Make sure swapping only
temp = array[i][j]; //occurs within the boundary
array[i][j] = array[i+1][0]; //of the array. In this case
array[i+1][0] = temp; //we will only swap if we are
} //at the last element in each
} //row (j==array[i].length-1)
} //3 elements, but index starts
//at 0, so last index is 2
}
}
要在此对角线模式中进行迭代,我们可以执行以下操作:
0 1 2
1 2 3
2 3 4
//设置您的矩阵,任何大小和形状(MxN)都可以,但锯齿状数组将被破坏
int[][]矩阵={{0,0,0},{0,0,0},{0,0,0};
//number是我们将在矩阵的每个位置输入的值
整数=1;
//当number小于或等于位置总数时进行迭代
//在矩阵中。对于3x3矩阵,9。(这就是代码不适用于的原因
//锯齿状阵列)
对于(int i=0;number=0);
}
请注意,虽然此实现将适用于所有矩阵大小(和形状),但它不会尽可能高效。其中
n
是matrix.length
(假设是一个平方矩阵),这个实现是一个大O表示法中的最优O(n^2)
类算法;但是,它可以有效地执行2*n^2
迭代,而最佳解决方案只能执行n^2
您需要对x/y从索引0..n进行转换(从0到x*y),然后从索引返回到x/y
// set up your matrix, any size and shape (MxN) is fine, but jagged arrays will break
int[][] matrix = {{0,0,0},{0,0,0},{0,0,0}};
// number is the value we will put in each position of the matrix
int number = 1;
// iterate while number is less than or equal to the total number of positions
// in the matrix. So, for a 3x3 matrix, 9. (this is why the code won't work for
// jagged arrays)
for (int i = 0; number <= matrix.length * matrix[0].length; i++) {
// start each diagonal at the top row and from the right
int row = 0;
int col = i;
do {
// make sure row and length are within the bounds of the matrix
if (row < matrix.length && col < matrix[row].length) {
matrix[row][col] = number;
number++;
}
// we decrement col while incrementing row in order to traverse down and left
row++;
col--;
} while (row >= 0);
}
我把实现细节留给你了。卢克的直觉很好——你正在处理下对角线和左对角线。另外需要注意的是对角线的长度:1,2,3,2,1。我也假设平方矩阵。弄乱你的标记可以产生以下结果:
public void toPos(int index){
return...
}
public int toIndex(int x, int y){
return...
}
int len=1;
int i=1;
while(len0){
//填充长度为len的对角线
对于(int c=arr.length-1;c>(arr.length-len-1);c--)
int r=arr.length-len+2-c;
arr[r][c]=i;
i++;
}
蓝--;
}
System.out.println(Arrays.deepToString(arr));
以下是从转换为Java并根据您的问题进行调整的代码
int len = 1;
int i = 1;
while(len <= arr.length){
//Fill this diagonal of length len
for(int r = 0; r < len; r++){
int c = (len - 1) - r;
arr[r][c] = i;
i++;
}
len++;
}
len--; len--;
while(len > 0){
//Fill this diagonal of length len
for(int c = arr.length - 1; c > (arr.length - len - 1); c--){
int r = arr.length - len + 2 - c;
arr[r][c] = i;
i++;
}
len--;
}
System.out.println(Arrays.deepToString(arr));
输出:
| 1 2 3 |
| 4 5 6 |
| 7 8 9 |
这是一个简单的动态规划(ish)解决方案。你基本上是从你最后的动作中学到的 注意:这是一个
O(N^2)
算法
初始化:
intm=4;
int n=4;
int[][]数组=新的int[m][n];;
对于(int i=0;i<3;i++){
对于(int j=0;j<3;j++){
数组[i][j]=0;
}
}
工作:
数组[0][0]=1;
for(int i=0;i
打印解决方案:
for(int i=0;i
以下是解决您的问题的完整工作代码。复制和粘贴,如果你喜欢
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
System.out.print(array[i][j]);
}
System.out.println("");
}
return 0;
}
公共类填充数组{
公共静态void main(字符串[]args){
int[][]数组={
{1,2,3},
{4,5,6},
{7,8,9};//这是原始数组
int temp=0;//声明一个包含交换值的临时变量
对于(int i=0;i
您希望实现以下目标:
0,0
1,0
0,1
2,0
1,1
0,2
2,1
1,2
2,2
public class FillArray{
public static void main (String[] args){
int[][] array = {
{1,2,3},
{4,5,6},
{7,8,9}}; //This is your original array
int temp = 0; //declare a temp variable that will hold a swapped value
for (int i = 0; i < array[0].length; i++){
for (int j = 0; j < array[i].length; j++){
if (i < array.length - 1 && j == array[i].length - 1){ //Make sure swapping only
temp = array[i][j]; //occurs within the boundary
array[i][j] = array[i+1][0]; //of the array. In this case
array[i+1][0] = temp; //we will only swap if we are
} //at the last element in each
} //row (j==array[i].length-1)
} //3 elements, but index starts
//at 0, so last index is 2
}
}
在大小为NxN的栅格中,对于栅格中的每个点(x,y),可以确定如下值(仍然需要对0处的偏移进行一些校正,请参见最终公式):
- 如果您位于左上半部分,请计算您上方和左侧三角形的面积,并将您与顶部的距离相加
- 如果你在右下角
for(int i = 0; i < m; i++){ for(int j = 0; j < n; j++){ System.out.print(array[i][j]); } System.out.println(""); } return 0; }
public class FillArray{ public static void main (String[] args){ int[][] array = { {1,2,3}, {4,5,6}, {7,8,9}}; //This is your original array int temp = 0; //declare a temp variable that will hold a swapped value for (int i = 0; i < array[0].length; i++){ for (int j = 0; j < array[i].length; j++){ if (i < array.length - 1 && j == array[i].length - 1){ //Make sure swapping only temp = array[i][j]; //occurs within the boundary array[i][j] = array[i+1][0]; //of the array. In this case array[i+1][0] = temp; //we will only swap if we are } //at the last element in each } //row (j==array[i].length-1) } //3 elements, but index starts //at 0, so last index is 2 } }
1 2 4 7 3 5 8 B 6 9 C E A D F G
int N = 4; int[][] v = new[N][N]; for(int y = 0; y < N; y++) for(int x = 0; x < N; x++) v[x][y] = ( x + y < N ) ? ( ( x + y + 1 ) * ( x + y ) / 2 + y + 1 ) : ( N * N + 1 - ( N - y ) - ( 2 * N - x - y - 1 ) * ( 2 * N - x - y - 2 ) / 2 );
public static void main(String[] args) { //create an M x N array int rows = 20; int columns = 11; int[][] testData = new int[rows][columns]; //iteratively add numbers int counter = 0; for(int i = 0; i < rows; i++) { for(int j = 0; j < columns; j++) { testData[i][j] = ++counter; } } //print our test array printArray(testData); System.out.println(""); //print our diagonal array printArray(diagonal(testData)); }
static void printArray(int[][] array) { //get number of digits int count = array.length * array[0].length; //get power of function int power; //probably the only time I'd ever end a for loop in a semicolon //this gives us the number of digits we need //You could also use logs I guess but I'm not a math guy for(power = 0; count / Math.pow(10, power) > 1; power++); for(int i = 0; i < array.length; i++){ System.out.print("{"); for(int j = 0; j < array[0].length; j++){ //Let's say Power is 0. That means we have a single-digit number, so we need // +1 for the single digit. I throw in 2 to make it extra wide System.out.print(String.format("%" + Integer.toString(power + 2) + "s", Integer.toString(array[i][j]))); } System.out.println("}"); } }
static int[][] diagonal(int[][] input) { //our array info final int numRows = input.length; final int numColumns = input[0].length; int[][] result = new int[numRows][numColumns]; //this is our mobile index which we will update as we go through //as a result of certain situations int rowIndex = 0; int columnIndex = 0; //the cell we're currently filling in int currentRow = 0; int currentColumn = 0; for(int i = 0; i < numRows; i++) { for(int j = 0; j < numColumns; j++) { result[currentRow][currentColumn] = input[i][j]; //if our current row is at the bottom of the grid, we should //check whether we should roll to top or come along //the right border if(currentRow == numRows - 1) { //if we have a wider graph, we want to reset row and //advance the column to cascade if(numRows < numColumns && columnIndex < numColumns - 1 ) { //move current row down a line currentRow = 0; //reset columns to far right currentColumn = ++columnIndex; } //if it's a square graph, we can use rowIndex; else { //move current row down a line currentRow = ++rowIndex; //reset columns to far right currentColumn = numColumns - 1; } } //check if we've reached left side, happens before the //top right corner is reached else if(currentColumn == 0) { //we can advance our column index to the right if(columnIndex < numColumns - 1) { currentRow = rowIndex; currentColumn = ++columnIndex; } //we're already far right so move down a row else { currentColumn = columnIndex; currentRow = ++rowIndex; } } //otherwise we go down and to the left diagonally else { currentRow++; currentColumn--; } } } return result; }
Input { 1 2 3} { 4 5 6} { 7 8 9} { 10 11 12} Output { 1 2 4} { 3 5 7} { 6 8 10} { 9 11 12} Input { 1 2 3 4 5 6} { 7 8 9 10 11 12} { 13 14 15 16 17 18} { 19 20 21 22 23 24} { 25 26 27 28 29 30} { 31 32 33 34 35 36} Output { 1 2 4 7 11 16} { 3 5 8 12 17 22} { 6 9 13 18 23 27} { 10 14 19 24 28 31} { 15 20 25 29 32 34} { 21 26 30 33 35 36} Input { 1 2 3 4 5 6} { 7 8 9 10 11 12} { 13 14 15 16 17 18} { 19 20 21 22 23 24} { 25 26 27 28 29 30} { 31 32 33 34 35 36} { 37 38 39 40 41 42} { 43 44 45 46 47 48} { 49 50 51 52 53 54} { 55 56 57 58 59 60} { 61 62 63 64 65 66} { 67 68 69 70 71 72} { 73 74 75 76 77 78} { 79 80 81 82 83 84} { 85 86 87 88 89 90} { 91 92 93 94 95 96} { 97 98 99 100 101 102} { 103 104 105 106 107 108} { 109 110 111 112 113 114} { 115 116 117 118 119 120} { 121 122 123 124 125 126} { 127 128 129 130 131 132} { 133 134 135 136 137 138} { 139 140 141 142 143 144} { 145 146 147 148 149 150} Output { 1 2 4 7 11 16} { 3 5 8 12 17 22} { 6 9 13 18 23 28} { 10 14 19 24 29 34} { 15 20 25 30 35 40} { 21 26 31 36 41 46} { 27 32 37 42 47 52} { 33 38 43 48 53 58} { 39 44 49 54 59 64} { 45 50 55 60 65 70} { 51 56 61 66 71 76} { 57 62 67 72 77 82} { 63 68 73 78 83 88} { 69 74 79 84 89 94} { 75 80 85 90 95 100} { 81 86 91 96 101 106} { 87 92 97 102 107 112} { 93 98 103 108 113 118} { 99 104 109 114 119 124} { 105 110 115 120 125 130} { 111 116 121 126 131 136} { 117 122 127 132 137 141} { 123 128 133 138 142 145} { 129 134 139 143 146 148} { 135 140 144 147 149 150}