LeetCode 48. 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?



题目标签:Array

  这道题目给了我们一个n * n的矩阵,让我们旋转图片。题目要求in-place,所以就不能用额外的空间了。一开始自己写了一个很原始的方法,结果虽然通过,但是速度太慢。只好去看看别人的方法,看完觉得,自己以前数学课学的东西都还给老师了。来分析一下题目,举一个例子

1  2  3           1  4  7           7  4  1

4  5  6           2  5  8           8  5  2

7  8  9           3  6  9           9  6  3

第一步,根据红色的对角线,找对应位置,互换两个数字的值。

第二步,对每一行数字,根据中线左右翻转。

Java Solution:

Runtime beats 61.89%

完成日期:07/17/2017

关键词:Array

关键点:先逆矩阵再根据中线左右翻转每一行

 1 public class Solution
 2 {
 3     public void rotate(int[][] matrix)
 4     {
 5         int n = matrix.length;
 6
 7         // along the left top to right bottom diagonal line, swap symmetrical pair
 8         for(int i=0; i<n; i++) // for each row
 9         {
10             for(int j=i+1; j<n; j++) // for each number
11             {
12                 // swap the pair
13                 int temp = matrix[i][j];
14                 matrix[i][j] = matrix[j][i];
15                 matrix[j][i] = temp;
16             }
17         }
18
19         // flip each row horizontally
20         for(int i=0; i<n; i++)
21         {
22             for(int j=0; j<n/2; j++)
23             {
24                 int temp = matrix[i][j];
25                 matrix[i][j] = matrix[i][n-1-j];
26                 matrix[i][n-1-j] = temp;
27
28             }
29         }
30     }
31 }

参考资料:

http://www.cnblogs.com/grandyang/p/4389572.html

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

时间: 2024-10-29 03:24:57

LeetCode 48. Rotate Image(旋转图像)的相关文章

leetCode 48.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? 思路:其实就是旋转数组,没有什么难度,代码如下: public class Solution { public void rotate(int[][] matrix) { int[][] a =

Leetcode 48 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? 思路:把矩阵看成是由多个正方形一层一层包围起来的.转动的时候就是转动正方形的边,只要把边上的每个数依次替换就好,四个方向也就是进行四次替换,依次对每一层进行替换. 总层数 = matrix.length/2,第i层从第mat

LeetCode 48. Rotate Image My Submissions Question (矩阵旋转)

题目大意:给一个矩阵,将其按顺时针旋转90°. 题目分析:通法是先将矩阵转置,然后再反转每一行,或者是先反转每一列,然后再将其转置.I just want to say"It's amazing!".(forgivig my poor English!) 代码如下(代码怎么写已经不重要了!): class Solution { public: void rotate(vector<vector<int>>& matrix) { int r=matrix.

LeetCode 48. Rotate Image (C++)

题目: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Note: You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do

19.2.7 [LeetCode 48] Rotate Image

You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Note: You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the

[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 题解]: 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个元素移动到链表的头部. 这道题的本质就是寻找链表的