Java 将2d矩阵中的所有零移位

Java 将2d矩阵中的所有零移位,java,arrays,algorithm,sorting,Java,Arrays,Algorithm,Sorting,我有一个二维数组,如下所示: 2 0 0 2 0 4 2 0 0 2 0 4 2 0 0 2 0 4 我想把所有的零移到左边,所以我做了这个方法: public static void shiftLeft(int [][] array){ for (int j = 0; j < array.length; j++) { for (int i = 0; i < array.length - 1; i++) { if ((array

我有一个二维数组,如下所示:

2 0 0 2 0 4
2 0 0 2 0 4
2 0 0 2 0 4
我想把所有的零移到左边,所以我做了这个方法:

public static void shiftLeft(int [][] array){

    for (int j = 0; j < array.length; j++) {

        for (int i = 0; i < array.length - 1; i++) {

            if ((array[j][i] != 0) && (array[j][i + 1] == 0)) {
                array[j][i + 1] = array[j][i];
                array[j][i] = 0;
            }
        }
    }
}

如何使所有的零向左移动?

在我看来,最简单的方法是使用3个嵌套循环

变量i在行上迭代

变量j1查找从每行左侧开始的第一个非零元素

变量j2查找j1之后的第一个零元素并交换它们。 下面的代码假设二维矩阵A被声明为A[N][M],其中N和M分别是行数和列数

for(int i =0;i<N;i++){
  for(int j1=0;j1<M;j1++){
    if (A[i][j1]==0)
      continue;
    for(int j2=j1;j2<M;j2++){
      if( A[i][j2]==0){
         //swap
         int tmp=A[i][j1];
         A[i][j1]=A[i][j2];
         A[i][j2]=tmp;
       }
     }
  }
}

for(int i=0;i事实上Trugis的答案也是正确的,但它只会将零与第一个非零进行交换。因此数字的顺序会改变

此答案不会改变数字的顺序:

    int[][] A = {   { 2, 3, 4, 2, 4, 4, 5, 0, 0, 0 },
                    { 0, 0, 0, 0, 0, 4, 3, 4, 5, 6 },
                    { 2, 0, 4, 2, 0, 4, 1, 2, 3, 4 }};

    int N = A.length;
    int M = A[0].length;
    int firstZeros = 0;

    for(int i = 0; i < N; i++) { // Loop over the rows 
        for(int j1 = 0; j1 < M; j1++) {
            // If there is a zero we pass by
            if (A[i][j1] == 0 && firstZeros == j1) {
                firstZeros++;
                continue;
            }
            // Otherwise, we have a value so we want to check if there is a zero afterwards
            for(int j2 = j1+1; j2 < M; j2++) {
                // If we find a zero we move it to the left
                if(A[i][j2] == 0) {
                    for (int j3 = j2; j3 > firstZeros; j3--) {
                        // Change zero with previous value
                        A[i][j3] = A[i][j3-1];
                        A[i][j3-1] = 0;
                    }
                    firstZeros++;
                }
            }
        }
        firstZeros = 0;
    }
int[]A={{2,3,4,2,4,4,5,0,0,0},
{ 0, 0, 0, 0, 0, 4, 3, 4, 5, 6 },
{ 2, 0, 4, 2, 0, 4, 1, 2, 3, 4 }};
int N=A.长度;
int M=A[0]。长度;
int firstZeros=0;
对于(int i=0;ifirstZeros;j3--){
//用以前的值更改零
A[i][j3]=A[i][j3-1];
A[i][j3-1]=0;
}
firstZeros++;
}
}
}
第一个零=0;
}

对于(int i=0;i
您的算法对我有效,但有时,当初始矩阵有多个非零值且没有空格(0)时在这两者之间,调用该方法后的最终结果会导致一些值自身交换。由于@Trugis的代码不起作用。请检查您的代码并在此处进行解释。@FacundoSchiavoni您能告诉我您使用的导致算法行为不正确的输入吗?我测试了您的算法并进行了比较如果使用我的,您可以在上看到我的测试代码。您的输出(输出作为注释写在代码底部)似乎是错误的。哇,我的解决方案是如此错误:-p你应该纠正你的,以避免交换和更改数字的顺序,这是我试图做的,但没有成功,我想^^所以我更新了我的答案,以实际工作,这一次我测试了它,但你可以自己看看。
    int[][] A = {   { 2, 3, 4, 2, 4, 4, 5, 0, 0, 0 },
                    { 0, 0, 0, 0, 0, 4, 3, 4, 5, 6 },
                    { 2, 0, 4, 2, 0, 4, 1, 2, 3, 4 }};

    int N = A.length;
    int M = A[0].length;
    int firstZeros = 0;

    for(int i = 0; i < N; i++) { // Loop over the rows 
        for(int j1 = 0; j1 < M; j1++) {
            // If there is a zero we pass by
            if (A[i][j1] == 0 && firstZeros == j1) {
                firstZeros++;
                continue;
            }
            // Otherwise, we have a value so we want to check if there is a zero afterwards
            for(int j2 = j1+1; j2 < M; j2++) {
                // If we find a zero we move it to the left
                if(A[i][j2] == 0) {
                    for (int j3 = j2; j3 > firstZeros; j3--) {
                        // Change zero with previous value
                        A[i][j3] = A[i][j3-1];
                        A[i][j3-1] = 0;
                    }
                    firstZeros++;
                }
            }
        }
        firstZeros = 0;
    }