Matrix Spiral Traverse Spiral matrix,spiral Matrix2

Source: Internet
Author: User

Spiralmatrix:

Given a matrix of m x n elements (m rows, n columns), return all elements of the Matri X 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] .

Algorithm analysis: In fact, is a double loop traversal, each time is a loop, and then traverse the inside a circle ..., need to record m,n, after each traversal, M,n minus 2, but also to record coordinates x, y;

The special case is n=1;m=1; in fact, M,n is odd.

public class Spiralmatrix {public list<integer> spiralorder (int[][] matrix) {list<integer> res = new Arraylist<> (); if (matrix.length = = 0 | | matrix = = NULL) {return res;} int m = Matrix.length;int n = matrix[0].length;//must first determine if the matrix is empty, otherwise the array subscript bounds int x = 0, y = 0;while (M > 0 && n > 0) {i F (m = = 1) {for (int i = 0; i < n; i + +) {Res.add (matrix[x][y++]);} break;//Don't forget break}else if (n = = 1) {for (int j = 0; J < m; J + +) {Res.add (matrix[x++][y]);} break;} for (int i = 0; i < n-1; i + +) {Res.add (matrix[x][y++]);} for (int i = 0; i < m-1; i + +) {Res.add (matrix[x++][y]);} for (int i = 0; i < n-1; i + +) {Res.add (matrix[x][y--]);} for (int i = 0; i < m-1; i + +) {Res.add (matrix[x--][y]);} X++;y++;m-= 2;n-= 2;} return res;}}

SPIRALMATRIX2:

Given an integer n, generate a square matrix filled with elements from 1 to n2 in Spiral order.

For example,
Given N = 3 ,

You should return the following matrix:
[[1, 2, 3], [8, 9, 4], [7, 6, 5]]

Algorithm analysis: And the previous algorithm, similar, but this is a n*n matrix, can actually extend to the m*n matrix. N*n makes emptying easier, especially when n=1.

public class SpiralMatrix2 {public int[][] Generatematrix (int n) {int[][] res = new Int[n][n];if (n = = 0) {return res;} int x = 0, y = 0;int val = 1;while (n > 0) {if (n = = 1) {Res[x][y] = val;break;} for (int i = 0; i < n-1; i + +) {res[x][y++] = Val;val + +;} for (int i = 0; i < n-1; i + +) {Res[x++][y] = Val;val + +;} for (int i = 0; i < n-1; i + +) {res[x][y--] = Val;val + +;} for (int i = 0; i < n-1; i + +) {Res[x--][y] = Val;val + +;} X ++;y ++;n-= 2;} return res;}}

Matrix Spiral Traverse Spiral matrix,spiral Matrix2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.