[Leetcode] [048] Rotate Image slightly verbose (Java)

Source: Internet
Author: User
Tags rotate image

The title is here https://leetcode.com/problems/rotate-image/

"Personal Analysis"

This topic, I think is to examine the basic skills, study carefully, the algorithm does not have a lot of things, but for the use of coordinates have higher requirements.

Released two versions of the answer, the first version is written by themselves, the second is the current best answer to the Java rewrite.

"Code Comment"

Solution1: The idea is more direct. Since the requirements of in-place, it is in the change before the original value in that position, and then the tail bite tail to modify, to the effect can refer

Solution2: The method of partial mathematics, first put the matrix "up and Down": the first line and the last line, the second row and the penultimate line to change. Then mirror the interchange, the number in column J of row I and the number in column I of Line J.

1  Public classSolution {2     /**3 * 1 2 3 4 (9 5 1) 9 5 14 * 5 6 7 8 Outer Loop (6 7 2 inner loop) 6 25 * 9 ============> 3 ============> 7 36 * from 8 4 8 47      * @paramMatrix8      */9      Public voidRotateint[] matrix) {Ten         intn =matrix.length; One         if(n = = 0) { A             return; -         } -         intHalf = N/2; the         //For each loop -          for(inti = 0; i < half; i++) { -             intStartIndex =i; -             intEndIndex = StartIndex + (n-2 * i)-1; +             //in one row, we leave the last number unchanged -             //so it's J < EndIndex, not J <= EndIndex +              for(intoffset = 0; StartIndex + Offset < endIndex; offset++) { A                 //Number in the first row at                 intTemp1 = Matrix[startindex][startindex +offset]; -                 //Number in the last column -                 intTemp2 = Matrix[startindex +Offset] [EndIndex]; -                 //Number in the last row -                 intTemp3 = Matrix[endindex][endindex-offset]; -                 //Number in the first column in                 intTemp4 = Matrix[endindex-Offset] [StartIndex]; -                  toMatrix[startindex][startindex + offset] =Temp4; +Matrix[startindex + Offset][endindex] =Temp1; -Matrix[endindex][endindex-offset] =Temp2; theMatrix[endindex-offset][startindex] =Temp3; *                  $             }Panax Notoginseng         } -  the     }   +}

Solution2:

1  Public classSolution {2     /**3 * First Reverse top-bottom, then reverse symmetry4 * 1 2 3 7 8 9 7 4 15 * 4 5 6 ==> 4 5 6 ==> 8 5 26 * 7 8 9 1 2 3 9 6 37      * @paramMatrix8      */9      Public voidRotateint[] matrix) {Ten         intn =matrix.length; One         intMiddle = N/2; A         //reverse top-bottom, swap the ith row with (n-i) th row -          for(inti = 0; I < Middle; i++) { -              for(intj = 0; J < N; J + +) { the                 inttemp =Matrix[i][j]; -MATRIX[I][J] = matrix[n-1-i] [j]; -Matrix[n-1-I][J] =temp; -             } +         } -          +         //Swap symmetry A          for(inti = 0; I < n; i++) { at              for(intj = i + 1; J < N; J + +) { -                 inttemp =Matrix[i][j]; -MATRIX[I][J] =Matrix[j][i]; -Matrix[j][i] =temp; -             } -              in         } -  to     } +  -}

[Leetcode] [048] Rotate Image slightly verbose (Java)

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.