本文介绍了在螺旋的次序二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图填补螺旋的次序数组。到目前为止,我可以打印在螺旋的顺序排列,但有没有办法修改数组,这样我可以在螺旋为了填补它,然后只打印数组?我想它去按递减顺序就像一个倒计时。请帮帮忙!
公共类螺旋{
公共静态无效的主要(INT男,INT N){
//通过M * N新建米×1的整数n个阵列
INT [] []值=新INT [M] [N];
的for(int i = 0; I<米;我++)
为(诠释J = 0; J&n种; J ++)
值[I] [J] = 1 +(M * N)* I + J;
// 螺旋
对(INT I =(M * N)-1,J = 0; I&≠0; I--,J ++){
对于(时int k = j的; K<我; k ++)的System.out.println(价值[J] [K]);
对于(时int k = j的; K<我; k ++)的System.out.println(值[k]的[I]);
对于(INT K = 1; K> D]; K--)的System.out.println(值[I] [K]);
对于(INT K = 1; K> D]; K--)的System.out.println(值[k]的[J]);
}
}
}
解决方案
INT包括maxValue = target.length *目标[0] .length;
私人INT [] [] generateMatrix(INT [] []的目标,INT层面,诠释currentVal){
//总是从每层中左下角启动
INT W =水平;
INT H = target.length - - 1;
//填写底线
INT I = 0;
对(我= W; I<目标[0] .length - 水平和放大器;&安培; currentVal< = maxValue(最大值);我++){
目标[H] [I] = currentVal ++;
}
W =靶[0] .length - - 1;
INT J = 0;
//填写右边线
对于(,J = H - 1; J> =水平和放大器;&安培; currentVal< = maxValue(最大值); j--){
目标[J] [W] = currentVal ++;
}
H =水平;
//填补上述行
对(我= W - 1; I> =水平和放大器;&安培; currentVal< = maxValue(最大值);我 - ){
目标[H] [I] = currentVal ++;
}
W =水平;
//填补了左线
为(J = H + 1; J< target.length - 级 - 1安培;&安培; currentVal< = maxValue(最大值),J ++){
目标[J] [W] = currentVal ++;
}
如果(currentVal>包括maxValue)
将目标;
返回generateMatrix(目标,++级,currentVal);
}
I'm trying to fill an array in spiral order. So far, I can print the array in spiral order, but is there a way to modify the array so that i can fill it in spiral order and then just print the array? I'd like it to go in decreasing order like a countdown. Please help!
public class Spiral {
public static void main(int m, int n) {
// create m by n array of integers 1 through m*n
int[][] values = new int[m][n];
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
values[i][j] = 1 + (m*n)*i + j;
// spiral
for (int i = (m*n)-1, j = 0; i > 0; i--, j++) {
for (int k = j; k < i; k++) System.out.println(values[j][k]);
for (int k = j; k < i; k++) System.out.println(values[k][i]);
for (int k = i; k > j; k--) System.out.println(values[i][k]);
for (int k = i; k > j; k--) System.out.println(values[k][j]);
}
}
}
解决方案
int maxValue = target.length * target[0].length;
private int[][] generateMatrix(int[][] target, int level, int currentVal) {
// always start from lower left corner in each layer
int w = level;
int h = target.length - level - 1;
// fill the bottom line
int i = 0;
for (i = w; i < target[0].length - level && currentVal <= maxValue; i++) {
target[h][i] = currentVal++;
}
w = target[0].length - level - 1;
int j = 0;
// fill the right line
for (j = h - 1; j >= level && currentVal <= maxValue; j--) {
target[j][w] = currentVal++;
}
h = level;
// fill the above line
for (i = w - 1; i >= level && currentVal <= maxValue; i--) {
target[h][i] = currentVal++;
}
w = level;
// fill the left line
for (j = h + 1; j < target.length - level - 1 && currentVal <= maxValue; j++) {
target[j][w] = currentVal++;
}
if (currentVal > maxValue)
return target;
return generateMatrix(target, ++level, currentVal);
}
这篇关于在螺旋的次序二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!