左上到右下
public static void main(String[] args) { int[][] matrix = new int[][]{ new int[]{2, 3, 5,10}, new int[]{5, 2, 4, 1}, new int[]{3, 3, 1, 1} }; process(matrix); } private static void process(int[][] A) { int row = A.length; int col = A[0].length; // 首先以第0行,列递增为起点遍历col次 for (int j = 0; j < col; j++) { int r = 0; int c = j; while (r < row && c >= 0) System.out.println(A[r++][c--]); } // 然后以第col-1列,行递增为起点遍历row-1次 for (int i = 1; i < row; i++) { int r = i; int c = col - 1; while (r < row && c >= 0) System.out.println(A[r++][c--]); } }
从左下到右上
public static void main(String[] args) { int[][] matrix = new int[][]{ new int[]{2, 3, 5,10}, new int[]{5, 2, 4, 1}, new int[]{3, 3, 1, 1} }; process(matrix); } private static void process(int[][] A) { int row = A.length; int col = A[0].length; for(int j = 0;j=0&&c>=0) System.out.println(A[r--][c--]); } for(int i = row - 1;i >= 0; i--){ int r = i; int c = col-1; while(r>=0&&c>=0){ System.out.println(A[r--][c--]); } } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)