Spiral Matrix (Lintcode)

Source: Internet
Author: User
Tags lintcode

Spiral Matrix

Given a matrix of m x n elements (m rows, n columns), return all elements of the Matri X in Spiral Order.

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

A rare time ac! Although the feeling is very water, but like this way of thinking clearly written down, there is no "small problem", and then submit, AC situation for me is still very few. Although the code is still a bit messy.

The idea is to define a two-dimensional array of directions, traversing in one Direction from (0,0), if a wall (the next traversal target crosses or has been traversed) continues to traverse in a direction that does not hit the wall, and ends when each direction hits a wall.

1  Public classSolution {2     /**3      * @parammatrix A matrix of M x n elements4      * @returnAn integer list5      */6      int[] f =New int[1000] [1000];7      PublicList<integer> Spiralorder (int[] matrix) {8list<integer> list =NewArraylist<integer>();9         intm =matrix.length;Ten         if(M = = 0)returnlist; One         intn = matrix[0].length; A          -         int[] D =New int[4] [2]; -D[0][0] = 0; D[0][1] = 1; theD[1][0] = 1; D[1][1] = 0; -D[2][0] = 0; D[2][1] = 1; -D[3][0] =-1; D[3][1] = 0; -          +         intcd = 0; -         inti = 0; +         intj = 0; AList.add (matrix[0][0]); atF[0][0] = 1; -         BooleanFlag =true; -          while(flag) { -             if(!IsV (i,j,d[cd],m,n)) { -                 intx = 0; -                  while(!ISV (I,j,d[cd],m,n) && x < 4) { incd = (CD + 1)% 4; -X + +; to                 } +                 if(x >= 4) flag =false; -}Else { thei + = D[cd][0]; *J + = D[cd][1]; $ List.add (Matrix[i][j]);Panax NotoginsengF[I][J] = 1; -             } the         } +          A         returnlist; the     } +      -     BooleanIsV (intIintJintD[],intMintN) { $         if(i + d[0] < 0 | | i + d[0] >= m)return false; $         if(j + d[1] < 0 | | j + d[1] >= N)return false; -         if(f[i + d[0]][j + d[1]]! = 0)return false; -         return true; the     } -}
View Code

Spiral Matrix (Lintcode)

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.