给定一个矩阵matrix,按照“Z”字形的方式打印这个矩阵。
样例输入:
1 2 3 4
5 6 7 8
9 10 11 12
输出:
1 2 5 9 6 3 4 7 10 11 8 12
或者变一下题目:
输入:
1 3 4 9
2 5 8 10
6 7 11 12
输出:
1 2 3 4 5 6 7 8 9 10 11 12
https://www.nowcoder.com/practice/d2efe600e73d47a2ba1533dc926cbb46
其实不管在 Leetcode 还是牛客网,会发现矩阵相关的题相对较少,大多都是诸如二叉树、动态规划、链表、数组之类的题,矩阵相关的确实比较少。
我觉得可以把矩阵归到数组这一类吧~
题目说要按照“之”字打印数字,乍一听,就会想着怎么去遍历这个矩阵。不知道你第一想法会不会是去双重 for 循环去遍历矩阵呢?甚至可能你做过一些题目,比如
牛客网–关于之字形打印矩阵 这两种虽然题目名称上差不多,但是其实题意是不一样的哦,别被误导了~
那该如何解这道题呢?
我们先看下这道题是要怎么进行遍历的。
先思考一下几个问题:
let matarix = [];
matarix.push([1,3,4, 9])
matarix.push([2,5,8, 10])
matarix.push([6,7,11, 12])
function traverse(matarix) {
let rows = matarix.length;
let cols = matarix[0].length;
let curRow = 0;
let curCol = 0;
let arrow = 'down';
while (curRow < rows && curCol < cols) {
console.log(matarix[curRow][curCol]);
if (curRow === rows - 1 && curCol === cols - 1) {
break;
}
if (arrow === 'right') {
if (curRow > 0 && curCol < cols - 1) {
curRow--;
curCol++;
} else if (curRow === 0 && curCol < cols - 1) {
curCol++;
arrow = 'down';
} else if (curCol === cols - 1 && curRow < rows - 1) {
curRow++;
arrow = 'down'
}
} else if (arrow === 'down') {
if (curRow < rows - 1 && curCol > 0) {
curRow++;
curCol--;
} else if (curRow === rows - 1 && curCol < cols - 1) {
curCol++;
arrow = 'right';
} else if (curCol === 0 && curRow < rows - 1) {
curRow++;
arrow = 'right';
}
}
}
}
traverse(matarix);
import java.util.Scanner;
public class Main {
public static void printMatrix(int[][] matrix) {
int xA = 0;
int yA = 0;
int xB = 0;
int yB = 0;
int rows = matrix.length - 1;
int cols = matrix[0].length - 1;
boolean fromUp = false;
while (xA <= rows) {
printLine(matrix, xA, yA, xB, yB, fromUp);
xA = yA == cols ? xA + 1 : xA;
yA = yA == cols ? yA : yA + 1;
yB = xB == rows ? yB + 1 : yB;
xB = xB == rows ? xB : xB + 1;
fromUp = !fromUp;
}
}
public static void printLine(int[][] matrix, int xA, int yA, int xB, int yB, boolean fromUp) {
if (fromUp) {
while (yA >= yB) {
System.out.print(matrix[xA++][yA--] + " ");
}
} else {
while (yA >= yB) {
System.out.print(matrix[xB--][yB++] + " ");
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int[][] matrix = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
matrix[i][j] = sc.nextInt();
}
}
printMatrix(matrix);
}
}
let matarix = [];
matarix.push([1,3,4, 9])
matarix.push([2,5,8, 10])
matarix.push([6,7,11, 12])
matarix.push([13,14,15, 16])
matarix.push([17,18,19, 20])
var printMatrix = function (matarix) {
let rows = matarix.length;
let cols = matarix[0].length;
let ans = new Array(rows * cols);
let index = 0;
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
let temp = Math.abs((i & 1) * (cols - 1) - j);
ans[index++] = matarix[i][temp];
}
}
return ans;
}
printMatrix(matarix);