[Leetcode][048] Rotate Image 略詳細 (Java)

來源:互聯網
上載者:User

標籤:

題目在這裡 https://leetcode.com/problems/rotate-image/

【個人分析】

   這個題目,我覺得就是考察基本功、考察細心的,演算法方面沒有太多東西,但是對於座標的使用有較高要求。

       放了兩個版本的答案,第一個版本是自己寫的,第二個是目前最佳答案的Java改寫。

 【代碼注釋】

       Solution1: 思路比較直接。既然要求in-place,那就在修改之前,先儲存原先在那個位置上的值,然後尾巴咬尾巴的去修改;大意可以參考

                                       

       Solution2: 偏數學的方法,先把矩陣“上下對調”: 第一行和最後一行換,第二行和倒數第二行換。然後再鏡像交換,第 i 行第 j 列的數和第 j 行第 i 列的數字交換。

 

 1 public class Solution { 2     /** 3      *  1  2  3  4               13  9  5  1                13  9  5  1 4      *  5  6  7  8  outer loop   14  6  7  2  inner loop    14 10  6  2 5      *  9 10 11 12 ============> 15 10 11  3 ============>  15 11  7  3 6      * 13 14 15 16               16 12  8  4                16 12  8  4 7      * @param matrix 8      */ 9     public void rotate(int[][] matrix) {10         int n = matrix.length;11         if (n == 0) {12             return;13         }14         int half = n / 2;15         // for each loop16         for (int i = 0; i < half; i++) {17             int startIndex = i;18             int endIndex = startIndex + (n - 2 * i) - 1;19             // in one row, we leave the last number unchanged20             // so it is j < endIndex, not j <= endIndex21             for (int offset = 0; startIndex + offset < endIndex ; offset++) {22                 // number in the first row23                 int temp1 = matrix[startIndex][startIndex + offset];24                 // number in the last column25                 int temp2 = matrix[startIndex + offset][endIndex];26                 // number in the last row27                 int temp3 = matrix[endIndex][endIndex - offset];28                 // number in the first column29                 int temp4 = matrix[endIndex - offset][startIndex];30                 31                 matrix[startIndex][startIndex + offset] = temp4;32                 matrix[startIndex + offset][endIndex]   = temp1;33                 matrix[endIndex][endIndex - offset]     = temp2;34                 matrix[endIndex - offset][startIndex] = temp3;35                 36             }37         }38 39     }  40 }

 

 Solution2: 

 1 public class Solution { 2     /** 3      * First reverse top-bottom, then reverse symmetry  4      * 1 2 3       7 8 9       7 4 1  5      * 4 5 6  ==>  4 5 6  ==>  8 5 2  6      * 7 8 9       1 2 3       9 6 3  7      * @param matrix 8      */ 9     public void rotate(int[][] matrix) {10         int n = matrix.length;11         int middle = n / 2;12         // reverse top-bottom, swap the ith row with (n-i)th row13         for (int i = 0; i < middle; i++) {14             for (int j = 0; j < n; j++) {15                 int temp = matrix[i][j];16                 matrix[i][j] = matrix[n - 1 - i][j];17                 matrix[n - 1 - i][j] = temp;18             }19         }20         21         // swap symmetry22         for (int i = 0; i < n; i++) {23             for (int j = i + 1; j < n; j++) {24                 int temp = matrix[i][j];25                 matrix[i][j] = matrix[j][i];26                 matrix[j][i] = temp;27             }28             29         }30 31     }32 33 }

 

[Leetcode][048] Rotate Image 略詳細 (Java)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.