这是我在Java中将方矩阵顺时针旋转90度的代码。

import java.util.ArrayList;

public class Main {

    public static void main(String[] args) {
    // write your code here
        int size = 4;
        int[][] matrix = new int[size][size];
        for (int i = 0; i < size ; i++) {
            for (int j = 0; j < size; j++) {

                matrix[i][j] = (i+(2*j));
            }
        }
        trace(matrix, size);
        rotate(matrix);
        System.out.println("________________________");
        trace(matrix, size);

    }

    public static void rotate(int[][] matrix){
        int stop = matrix.length -1;

        rotate(matrix, stop, 0);
    }

    public static void rotate(int[][] matrix, int stop, int start){

        if(start - stop == 1) return;

        ArrayList<Integer> temp = new ArrayList<>();

        //save top row in an array
        for (int i = start; i <= stop ; i++) {
            temp.add(matrix[start][i]);
        }

        //left goes to top
        for (int i = start; i <= stop ; i++) {
            matrix[start][i] = matrix[stop - i][start];
        }

        //bottom goes to left
        for (int i = start; i <= stop ; i++) {
            matrix[i][start] = matrix[stop][i];
        }

        //right goes to bottom
        for (int i = start; i <= stop ; i++) {
            matrix[stop][stop - i] = matrix[i][stop];
        }

        //temp array goes to right
        for (int i = 0; i < temp.size() ; i++) {
            matrix[start + i][stop] = temp.get(i);
        }

        rotate(matrix, stop-1, start + 1);
    }



    public static void trace(int[][] matrix, int size){
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size ; j++) {
                System.out.print(matrix[i][j] + " ");

            }
            System.out.println();
        }
    }
}


原始数组和输出如下所示,

0 2 4 6
1 3 5 7
2 4 6 8
3 5 7 9
________________________
3 2 1 0
5 4 3 2
6 2 5 4
3 8 7 6


显然答案并不完全正确。虽然快到了。我对这些错误有些迷惑,对任何帮助都感激不尽。

最佳答案

此代码似乎工作正常。这里的测试用例是一个8x8的矩阵,对于您的测试用例,将其缩放回4x4。

public class Main {

public static void main(String[] args) {
// write your code here
    int size = 8;
    int[][] matrix = new int[size][size];
    for (int i = 0; i < size ; i++) {
        for (int j = 0; j < size; j++) {

            matrix[i][j] = (i+(2*j));
        }
    }
    trace(matrix, size);
    rotate(matrix);
    System.out.println("________________________");
    trace(matrix, size);

}

public static void rotate(int[][] matrix){
    int stop = matrix.length -1;

    rotate(matrix, stop, 0);
}

public static void rotate(int[][] matrix, int stop, int start){

    if(start - stop == 1) return;

    int [] temp = new int[stop-start];

    //save top row in an array
    for (int i = 0; i < stop-start ; i++) {
        temp[i] = matrix[start][start+i+1];
    }
    //System.out.println(Arrays.toString(temp));

    //left goes to top
    for (int i = start; i < stop ; i++) {
        matrix[start][start+stop-i] = matrix[i][start];
    }

    //bottom goes to left
    for (int i = start; i < stop ; i++) {
        matrix[i][start] = matrix[stop][i];
    }

    //right goes to bottom
    for (int i = start; i < stop ; i++) {
        matrix[stop][i] = matrix[start+stop-i][stop];
    }

    //temp array goes to right
    for (int i = 0; i < temp.length ; i++) {
        matrix[start + i + 1][stop] = temp[i];
    }

    rotate(matrix, stop-1, start + 1);
}



public static void trace(int[][] matrix, int size){
    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size ; j++) {
            System.out.printf("%2d ", matrix[i][j]);

        }
        System.out.println();
    }
}


}

关于java - 将方阵旋转90度,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35120734/

10-13 01:13