public class DayCode { public static void main(String[] args) { int[][] matrix = new int[][]{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; int[][] ans = new DayCode().transpose(matrix); System.out.println("ans is " + Arrays.deepToString(ans)); }
/** * https://leetcode-cn.com/problems/transpose-matrix/ * 时间复杂度 O (rows * cols) * 空间复杂度 O (rows * cols) * @param matrix * @return */ public int[][] transpose(int[][] matrix) { int rows = matrix.length; int cols = matrix[0].length; int[][] ans = new int[cols][rows]; for (int r = 0; r < rows; r++) { for (int c = 0; c < cols; c++) { ans[c][r] = matrix[r][c]; } } return ans; }}
评论