Rotate Matrix by one

Source: Internet
Author: User

Remember Amazon's OA topic, as if given a matrix, let the matrix of each element to the right shift a position. The problem was not well thought out before. Today I just brushed the rotate matrix, so just a piece of thinking.

The idea is similar to the Leetcode Spiral Matrix:

    The
    1. assumes that the matrix is a phalanx
    2. setting top, left, bot, right four boundary variables, and then a circle of shift from the outer ring to the most inner ring.
    3. sets a count when the count < total elements in matrix shift
    4. is recorded at the beginning of each lap Matrix[top][left], and then starts the shift ol>
    5. from top to bot
    6. from left to right
    7. from bot to top
    8. right to left
      1. results from the last edge try to update the new Matrix[top][left], this time the top for the old, and left has been updated once,  we want to be divided into two situations consider
        1. count! = totalElements-1, when we want to:
          1. matrix[top][left] Update to TMP
          2. count++
          3. top++
          4. go to the next lap /li>
        2. otherwise count = = TotalElements-1, also divided into two cases
          1. totalelements is odd, we do not change Matrix[top][left]
          2. totalelements is even, we still need to update matrix[top][left] = tmp
        3. then count++ end Loop Returns the result
  1. If a given matrix is not a square, then we have to add more judgments, such as when the last row or last column is not updated. The last line or the last column can be bot-top or right-left to find out the

Time Complexity-o (MN), Space complexity-o (1), in place.

 Public classSolution { Public voidRotatematrixbyone (int[] matrix) {        if(Matrix = =NULL|| Matrix[0] = =NULL) {            return; }        intm =matrix.length; intn = matrix[0].length; intleft = 0, top = 0, right = n-1, bot = m-1; intCount = 0; inttotalelements = m *N;  while(Count <totalelements) {                        intTMP =Matrix[top][left]; if(Count <totalelements) {                 for(inti = top; i < bot; i++) {Matrix[i][left]= Matrix[i + 1][left]; Count++; } Left++; }            if(Count <totalelements) {                 for(inti = left-1; I < right; i++) {Matrix[bot][i]= Matrix[bot][i + 1]; Count++; } bot--; }            if(Count <totalelements) {                 for(inti = bot + 1; i > top; i--) {Matrix[i][right]= Matrix[i-1][right]; Count++; } Right--; }            if(Count <totalelements) {                 for(inti = right + 1; i > left; i--) {Matrix[top][i]= Matrix[top][i-1]; Count++; }                if(Count! = totalElements-1) {Matrix[top][left]=tmp; } Else if(totalelements% 2 = = 0) {Matrix[top][left]=tmp; } Count++; Top++; }                    }            }}

Rotate Matrix by one

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.