Print matrix spiral

Problem

Print a matrix in spiral fashion.

Solution

We will first print the periphery of the matrix by the help of 4 for loops. Then recursively call this function to do the same thing with inner concentric rectangles. We will pass this information by a variable named depth, which will tell how many layers from outside should be ignored.

Code

public class PrintMatrixSpiral
{
 public static void main(String[] args)
 {
  int[][] matrix =
  {
  { 3, 4, 5, 6, 2, 5 },
  { 2, 4, 6, 2, 5, 7 },
  { 2, 5, 7, 8, 9, 3 },
  { 2, 4, 7, 3, 5, 8 },
  { 6, 4, 7, 3, 5, 7 } };

  printSpiral(matrix);
 }

 public static void printSpiral(int[][] matrix)
 {
  printSpiral(matrix, 0);
 }

 private static void printSpiral(int[][] matrix, int depth)
 {
  if (matrix == null && matrix.length == 0)
   return;
  int rows = matrix.length;
  int cols = matrix[0].length;
  if (2 * depth > Math.min(rows, cols))
   return;
  for (int i = depth; i < cols - depth - 1; ++i)
  {
   System.out.print(matrix[depth][i] + ",");
  }
  for (int i = depth; i < rows - depth - 1; ++i)
  {
   System.out.print(matrix[i][cols - depth - 1] + ",");
  }
  for (int i = rows - depth; i > depth; --i)
  {
   System.out.print(matrix[rows - depth - 1][i] + ",");
  }
  for (int i = rows - depth - 1; i > depth; --i)
  {
   System.out.print(matrix[i][depth] + ",");
  }
  printSpiral(matrix, ++depth);
 }
}
        
时间: 2024-08-27 13:47:42

Print matrix spiral的相关文章

51. 顺时针打印矩阵[print matrix in clockwise direction]

[题目] 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字. 例如:如果输入如下矩阵: 1            2            3            4 5            6            7            8 9            10          11           12 13          14          15           16 则依次打印出数字1, 2, 3, 4, 8, 12, 16, 15, 14

59. Spiral Matrix &amp;&amp; Spiral Matrix II

Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. For example, Given the following matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] You should return [1,2,3,6,9,8,7,4,5]. 思路: 可参考剑指

Spiral Matrix &amp;&amp; Spiral Matrix II

Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. For example, Given the following matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] You should return [1,2,3,6,9,8,7,4,5]. 解题思路: 求一个

[leedcode 54] Spiral Matrix

Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. For example,Given the following matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] You should return [1,2,3,6,9,8,7,4,5]. public class Solution {

G面经Prepare: Print Zigzag Matrix

For instance, give row = 4, col = 5, print matrix in zigzag order like: [1, 8, 9, 16, 17] [2, 7, 10, 15, 18] [3, 6, 11, 14, 19] [4, 5, 12, 13, 20] 1 package GooglePhone; 2 3 import java.util.Arrays; 4 5 public class PrintMatrix { 6 7 static void prin

LeetCode 885. Spiral Matrix III

原题链接在这里:https://leetcode.com/problems/spiral-matrix-iii/ 题目: On a 2 dimensional grid with R rows and C columns, we start at (r0, c0) facing east. Here, the north-west corner of the grid is at the first row and column, and the south-east corner of the

HDU - 233 Matrix

原题地址:http://acm.hdu.edu.cn/showproblem.php?pid=5015 解题思路:一看到题目,感觉是杨辉三角形,然后用组合数学做,不过没想出来怎么做,后来看数据+递推思想,感觉可能是矩阵快速幂,可惜一直不知道a*10+3的 +3怎么处理,果然还是图样图森破啊!如果矩阵能搞出来的话,后面的就简单了,真可惜一直到比赛结束也没想出来,看来这种矩阵的题目做的太少了,真后悔线性代数没有认真学.. 今天晚上又想了一会,完全可以把+3那个放到新的一阶矩阵上,值始终等于3,那么对

冬令营DAY3 T1 Matrix

题目描述 Description    生活中,我们常常用 233 表示情感.实际上,我们也会说 2333,23333,等等. 于是问题来了: 定义一种矩阵,称为 233 矩阵.矩阵的第一行依次是 23, 233,2333,23333,等. 此外,对矩阵的第 i 行.第 j 列的元素有 a[i][j] = a[i-1][j] + a[i][j-1],若 i, j 均大于 1. 告诉了你矩阵第一列的第 2~n 个元素,你能否算出矩阵的第 n 行.第 m 列的元素呢? 输入描述 Input Desc

[Swift]LeetCode73. 矩阵置零 | Set Matrix Zeroes

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in-place. Example 1: Input: [   [1,1,1],   [1,0,1],   [1,1,1] ] Output: [   [1,0,1],   [0,0,0],   [1,0,1] ] Example 2: Input: [   [0,1,2,0],   [3,4,5,2],   [1,3,1,5]