convert an 2d array from rows to blocks

前端 未结 1 567
囚心锁ツ
囚心锁ツ 2021-01-25 01:48

I am trying to work in this code for weeks. I need to convert rows and columns in a 2d array to blocks. it should work on any matrix in size n*n. (that I have been given the siz

相关标签:
1条回答
  • 2021-01-25 02:18

    No Need to perform a complex logic. Try to use :

    public static int[][] blocks(int[][] matrix, int sqrtN) {   
        int[][] blocks = matrix;
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix.length; j++){ 
                blocks[i][j] = (i % sqrtN * sqrtN) + (j % sqrtN + 1) ;
        }
    }
            return blocks;
    }
    
    0 讨论(0)
提交回复
热议问题