Leetcode 54.Spiral matrix (spiral matrix) thinking and method of solving problems

Source: Internet
Author: User

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].


Idea: spiral array, need to control the direction of output, my implementation is to define a Boolean array, the same size as the array, the data is initially all false. Then define an int variable to represent the direction, the data boundary or the next data is true, then change direction until it crosses or is all true. Ends the loop.

The specific code is as follows:

public class Solution {public list<integer> Spiralorder (int[][] a) {list<integer> List = new ArrayList    <Integer> ();    if (A.length = = 0 | | a[0].length = = 0) {return list;        } int i = 0;//line int j = 0;//column boolean[][] b = new Boolean[a.length][a[0].length]; int o = 0;//for direction, 0: Right, 1: Lower, 2: Left, 3: On//within range, out of range over while (I < a.length && I >= 0 &&am P            J < A[0].length && J >= 0) {if (B[i][j]) {//If all has gone, end loop break; } list.add (A[i][j]);                    Add result b[i][j] = true;//has been added as true to indicate that the switch (o) {case 0://is in the right direction.                        if (j = = A[0].length-1 | | b[i][j+1]) {o = 1;//go to the right or marked, go down in the direction                    i++;                    }else{J + +;                                    } break;           Case 1:         if (i = = A.length-1 | | b[i+1][j]) {o = 2;//go to the bottom or marked, direction left to go j--;                    }else{i++;                } break;                        Case 2:if (j = = 0 | | b[i][j-1]) {o = 3;//go to leftmost or marked, direction upward                    i--;                    }else{j--;                } break;                        Case 3:if (i = = 0 | | b[i-1][j]) {o = 0;//go to the top or marked, direction to the right.                    j + +;                    }else{i--;            } break;    }} return list; }}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Leetcode 54.Spiral matrix (spiral matrix) thinking and method of solving problems

Related Article

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.