Rotation of the Matrix

Source: Internet
Author: User

One, given a matrix, is represented by a two-dimensional array, not necessarily a phalanx (n*n), the transpose of the matrix (to the right), and the left. Like what:

1 2 3

4 5 6

7 8 9

Transpose right:

1 4 7

2 5 8

3 6 9

Another example:

1 2 3

4 5 6

Turn left

3 6

2 5

1 4

Second, realize the idea

Assuming the original matrix is m*n, the transpose becomes the n*m. Set the original matrix is arr[m][n], create a new matrix Rev[n][m]

For the right to transpose, it is the linear algebra inside of the request at, for Arr[m][n] inside each element arr[i][j], assign it to rev[j][i] can.

For turning left, for each element inside Arr[m][n] Arr[i][j], assign it to rev[n-j-1][i].

Three, complete code

 Public classtest{ Public Static voidMain (string[] args) {int[] arr1 = {{1,2,3},{4,5,6},{7,8,9}}; int[] arr2 = {{1,2,3},{4,5,6}};        Reverseright (ARR1); System.out.println ("---------");                Reverseright (ARR2); System.out.println ("***********");        Reverseleft (ARR1); System.out.println ("-------------");    Reverseleft (ARR2); }         Public Static voidReverseright (int[] arr) {        introw =arr.length; intCol = arr[0].length; int[] Rev =New int[Col][row];  for(inti = 0; i < row; i++)        {             for(intj = 0; J < Col; J + +) Rev[j][i]=Arr[i][j]; } StringBuilder SB=NewStringBuilder (); //Print rotated Matrix--there are col rows and row columns         for(inti = 0; I < col; i++)        {             for(intj = 0; J < Row; J + +)            {                //System.out.print (rev[i][j]+ "");Sb.append (Rev[i][j] + ""); } Sb.deletecharat (Sb.length ()-1); Sb.append ("\ n");//System.out.println ();} System.out.println (Sb.tostring ()); }         Public Static voidReverseleft (int[] arr) {        introw =arr.length; intCol = arr[0].length; int[] Rev =New int[Col][row];  for(inti = 0; i < row; i++)        {             for(intj = 0; J < Col; J + +) {Rev[col-j-1][i] =Arr[i][j]; }        }                //Print rotated Matrix--there are col rows and row columns         for(inti = 0; I < col; i++)        {             for(intj = 0; J < Row; J + +) {System.out.print (Rev[i][j]+ " ");        } System.out.println (); }    }}

Rotation of the Matrix

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.