之字/z型打印矩阵

题目描述

给定一个矩阵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 循环去遍历矩阵呢?甚至可能你做过一些题目,比如
牛客网–关于之字形打印矩阵 这两种虽然题目名称上差不多,但是其实题意是不一样的哦,别被误导了~

那该如何解这道题呢?

我们先看下这道题是要怎么进行遍历的。

先思考一下几个问题:

  1. 如何遍历这个矩阵?for 循环吗还是 while 循环?
  2. 这个循环的终止条件是什么?
  3. 遍历该怎么走呢?如果是一维数组,从前到后或者从后往前都可以,但是二位数组呢?一行一行的,还是一列一列的?

解法一



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);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值