Problem of set-up in circles

Source: Internet
Author: User

1. Given an orthopedic matrix matrix, print it in a circular fashion.

For example:

1 2 3 4

5 6 7 8

9 10 11 12

13 14 15 16

Printing results are:

1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10

Additional space complexity required: O (1)

Answer:

This paper mainly introduces a matrix processing method, this method can not only be used for this problem, but also suitable for many other face test, is the matrix lap processing. In the matrix with the upper-left corner of the TR,TC and the lower-right corner of the DR,DC can represent a sub-matrix, such as The Matrix in the title, when (TR,TC) = (0,0), (DR,DC) = (3,3), the matrix is the entire matrix, then the outermost part of this sub-matrix is as follows:

1 2 3 4

5 8

9 12

13 14 15 16

If the outer layer of the sub-matrix can be printed in circles, then (TR,TC) = (0,0), (DR,DC) = (3,3), the result is: 1 2 3 4 8 12 16 15 14 13 9 5. Next, the TR and TC are added 1, i.e. (TR,TC) = (1), so that the DR,DC minus the (DR,DC) = (2,2), the sub-matrix represented at this time is:

6 7

10 11

When the matrix is printed out, the result is: 6 7 11 10. Add TR,TC 1, i.e. (TR,TC) = (2,2), dr,dc minus 1, i.e. (DR,DC) = ((). The upper-left coordinate is found to the right or bottom of the bottom coordinate, and the whole process stops. The results that have been printed are the result of our request. The specific code reference is as follows:

1  Public voidSpiralorderprint (int[] matrix)2 {3     intTr=0;4     intTc=0;5     intDr=matrix.lenth-1;6     intDc=matrix[0].length-1;7Whiel (Tr<=dr && tc<=DC)8   {9Printedge (Matrix,tr++,tc++,dr--, dc--);Ten  One    } A } -   -   Public voidPrintedge (int[] Matrix,intTrintTcintDrintDC) the  { -     if(tr==DR) -   { -       for(inti=tc;i<=dc;i++) +System.out.print (m[tr][i]+ ""); -   } +  Else if(tc==DC) A  { at      for(inti=tr;i<=dr;i++) -System.out.print (m[i][tc]+ ""); -    } -}Else - { -     intCurc=TC; in     intCurr=tr; -      while(curc!=DC) to    { +System.out.print (m[tr][curc]+ ""); -Curc++; the     } *    while(curr!=DR) $  {Panax NotoginsengSystem.out.print (m[curr][dc]+ ""); -curr++; the   } +   while(curc!=TC) A    { theSystem.out.print (m[dr][curc]+ ""); +curc--; -     } $    while(curr!=TR) $  { -System.out.print (m[curr][tc]+ ""); -curr--; the   } -}
View Code

Problem of set-up in circles

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.