Leetcode 48. Rotate Image

来源:互联网 发布:opencv threshold源码 编辑:程序博客网 时间:2024/05/29 06:49
/** * Example, matrix[i][j] 0=<i<=3, 0=<j<=3 *  * 1   2   3   4 * 5   6   7   8   * 9  10  11  12 * 13 14  15  16 *  * Transpose the matrix by switching i and j to matrix[j][i], *  * 1   5   9   13 * 2   6  10   14 * 3   7  11   15 * 4   8  12   16 *  * Swap column 0 and i, 1 and i-1 ... to *  * 13   9   5   1 * 14  10   6   2 * 15  11   7   3 * 16  12   8   4 *  */public class Solution {    public void rotate(int[][] matrix) {        int tmp;        int row = matrix.length;        int col = matrix[0].length;                // transpose the matrix, note that i<j (*)        for (int i=0; i<row; i++)            for (int j=0; j<i; j++) {                tmp = matrix[i][j];                 matrix[i][j] = matrix[j][i];                matrix[j][i] = tmp;            }                    // swap columns        for (int i=0; i<col; i++)            for (int j=0; j<row/2; j++) {                tmp = matrix[i][j];                 matrix[i][j] = matrix[i][row-1-j];                 matrix[i][row-1-j] = tmp;            }    }}

0 0