面试题:打印蛇形二维数组

简介: 如何这样打印一个数组: 01 02 03 04 0516 17 18 19 0615 24 25 20 0714 23 22 21 0813 12 11 10 09 我的一个解答: static void Main(string[] args) { ...

如何这样打印一个数组:

01 02 03 04 05
16 17 18 19 06
15 24 25 20 07
14 23 22 21 08
13 12 11 10 09

我的一个解答:

static void Main(string[] args)
        {
            var m = 5;
            var matrix = new int[m, m];

            var row = 0;
            var col = 0;
            matrix[row, col] = 1;

            var moveVertically = false;

            if (m <= 0)
                return;

            while (matrix[row, col] != m * m)
            {
                if (!moveVertically)
                {
                    // Detect the right cube and try to move one setp
                    if ((col + 1) < m && matrix[row, col + 1] == 0)
                    {
                        matrix[row, col + 1] = matrix[row, col] + 1;
                        col++;
                        moveVertically = false;
                        continue;
                    }

                    // Detect the left side and try to move one step
                    if ((col - 1) >= 0 && matrix[row, col - 1] == 0)
                    {
                        matrix[row, col - 1] = matrix[row, col] + 1;
                        col--;
                        moveVertically = false;
                        continue;
                    }

                    moveVertically = true;
                }

                if (moveVertically)
                {
                    // Detect up and try to move one step
                    if ((row - 1) >= 0 && matrix[row - 1, col] == 0)
                    {
                        matrix[row - 1, col] = matrix[row, col] + 1;
                        row--;
                        moveVertically = true;
                        continue;
                    }

                    // Detect the down side and try to move one step
                    if ((row + 1) < m && matrix[row + 1, col] == 0)
                    {
                        matrix[row + 1, col] = matrix[row, col] + 1;
                        row++;
                        moveVertically = true;
                        continue;
                    }

                    moveVertically = false;
                }
            }

            Print(matrix);
        }

        private static void Print(int[,] matrix)
        {
            for (var row = 0; row < matrix.GetLength(0); row++)
            {
                for (var col = 0; col < matrix.GetLength(1); col++)
                {
                    Console.Write(string.Format(" {0}", matrix[row, col].ToString("D2")));
                }
                Console.WriteLine();
            }
        }

 

相关文章
|
11月前
|
算法 C++
【面试必刷TOP101】二分查找-I & 二维数组中的查找
【面试必刷TOP101】二分查找-I & 二维数组中的查找
44 0
|
4月前
(力扣)面试题04. 二维数组中的查找
(力扣)面试题04. 二维数组中的查找
31 0
|
4月前
|
C++
【一刷《剑指Offer》】面试题 3:二维数组中的查找
【一刷《剑指Offer》】面试题 3:二维数组中的查找
|
4月前
LeetCode(面试题:二维数组中的查找)
LeetCode(面试题:二维数组中的查找)
36 0
|
机器学习/深度学习
【第6期】面试BAT前应该知道的二维数组
【第6期】面试BAT前应该知道的二维数组
145 0
【第6期】面试BAT前应该知道的二维数组
|
机器学习/深度学习
【面试】输出"蛇形"矩阵
腾讯实习在线笔试的一道题目。   根据输入的数字(< 1000),输出这样的"蛇形"矩阵,如下。输入n,输出(n * n)阶矩阵,满足由外到内依次增大。
209 0
|
搜索推荐 PHP
PHP面试题:写一个二维数组排序算法函数,能够具有通用性,可以调用php内置函数(array_multisort())
PHP面试题:写一个二维数组排序算法函数,能够具有通用性,可以调用php内置函数(array_multisort())
138 0
剑指offer 面试题3—二维数组中找数
题目: 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。 基本思想: 首先选取数组中右上角的数字。如果等于要找的数字,结束。如果大于要找的数字,剔除这个数字所在的列;如果小于要找的数字,剔除这个数字所在的行。 public static boolean
1321 0
|
1月前
|
存储 Java
【IO面试题 四】、介绍一下Java的序列化与反序列化
Java的序列化与反序列化允许对象通过实现Serializable接口转换成字节序列并存储或传输,之后可以通过ObjectInputStream和ObjectOutputStream的方法将这些字节序列恢复成对象。
|
1月前
|
XML 存储 JSON
【IO面试题 六】、 除了Java自带的序列化之外,你还了解哪些序列化工具?
除了Java自带的序列化,常见的序列化工具还包括JSON(如jackson、gson、fastjson)、Protobuf、Thrift和Avro,各具特点,适用于不同的应用场景和性能需求。