[Leetcode][048] Rotate Image 略详细 (Java)

题目在这里 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 loop
16         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 unchanged
20             // so it is j < endIndex, not j <= endIndex
21             for (int offset = 0; startIndex + offset < endIndex ; offset++) {
22                 // number in the first row
23                 int temp1 = matrix[startIndex][startIndex + offset];
24                 // number in the last column
25                 int temp2 = matrix[startIndex + offset][endIndex];
26                 // number in the last row
27                 int temp3 = matrix[endIndex][endIndex - offset];
28                 // number in the first column
29                 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 row
13         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 symmetry
22         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 }
时间: 2024-10-12 02:07:54

[Leetcode][048] Rotate Image 略详细 (Java)的相关文章

[Leetcode] Longest Consecutive Sequence 略详细 (Java)

题目参见这里 https://leetcode.com/problems/longest-consecutive-sequence/ 这个题目我感觉很难,看了半天别人写的答案,才明白个所以然.下面的代码是我自己的改编,写的好像很复杂的样子,主要也是为了方便自己理解,耐着性子看完,应该就理解了. [个人分析]: 题目难度主要是要求O(N)完成.本来最自然的想法是先排序然后再去扫一遍得到结果,可是这样,明显就是O(NlgN).怎么样从NlgN 提高到O(N)呢?  ==> 空间换时间,空间从O(1)

Java for LeetCode 048 Rotate Image

You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Follow up: Could you do this in-place? 解题思路: 找规律即可,JAVA实现如下: static public void rotate(int[][] matrix) { int[][] matrixArray=new int[matrix.length][ma

LeetCode 048 Rotate Image

题目要求:Rotate Image You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Follow up:Could you do this in-place? 代码如下: class Solution { public: void rotate(vector<vector<int> > &matrix) { const in

leetcode 61 Rotate List ----- java

Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1->2->3->4->5->NULL and k = 2,return 4->5->1->2->3->NULL.题目意思不难,就是说给一个数k,然后从右向左数第k个节点,然后以这个节点为开头,重新组成一个链表. 需要注意的就是如果k大于链表长度len

LeetCode:Rotate Array

1.题目名称 Rotate Array(平移数组) 2.题目地址 https://leetcode.com/problems/rotate-array/ 3.题目内容 英文:Rotate an array of n elements to the right by k steps. 中文:将一个长度为n的数组,向右平移k个单位 4.解题方法1 一个比较易于理解的方法是新开辟一个与原数组等长的数组,循环考察原数组的元素,将它们放到新数组中平移后的位置上,最后再将新数组上的元素赋回原数组. 一段可以

[LeetCode] 61. Rotate List 旋转链表

Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: Input: 1->2->3->4->5->NULL, k = 2 Output: 4->5->1->2->3->NULL Explanation: rotate 1 steps to the right: 5->1->2->3-&g

LeetCode --- 61. Rotate List

题目链接:Rotate List Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given 1->2->3->4->5->NULL and k = 2, return 4->5->1->2->3->NULL. 这道题的要求是向右旋转链表k步. 其实就是把链表后面l-k个节点放到前面,可以采用快慢指针处理.不

【Leetcode】Rotate Image

You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Follow up: Could you do this in-place? 思路:第一种思路是一层一层的进行旋转,比较直观:第二种思路则比较取巧,首先沿着副对角线翻转一次,然后沿着水平中线翻转一次. 代码一: class Solution { public: void rotate(vector<

leetcode 119 Pascal&#39;s Triangle II ----- java

Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. Note:Could you optimize your algorithm to use only O(k) extra space? 上一道题的延伸版,就是直接求出第k行的数,要求用o(k)的空间复杂度. 也是直接相加就可以了. public class Solution { pub