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?

解题思路1,时间复杂度o(n):

顺时针旋转的规律很好找到,为了完全在矩阵内部操作,普通的办法就是每遍历到一个位置,则将与这个位置相关的一周(旋转一周涉及到的4个位置)都进行替换操作。

代码:

  1. class Solution {
  2. public:
  3. void rotate(vector<vector<int>>& matrix) {
  4. int n = matrix.size() - ;
  5. for (int i = ; i <= n / ; ++i) {
  6. for (int j = i; j < n - i; ++j) {
  7. int reserve = matrix[i][j];
  8. int p = ;
  9. while (p--) {
  10. if (p == )
  11. matrix[i][j] = reserve;
  12. else
  13. matrix[i][j] = matrix[n-j][i];
  14. int tmp = i;
  15. i = n - j;
  16. j = tmp;
  17. }
  18. }
  19. }
  20. return;
  21. }
  22. };

解题思路2,时间复杂度o(n):

拿出一张正方型纸,将纸顺时针旋转90度,相当于先将纸向后反转180度,再以左上-右下对角线为轴,旋转180度。

所以,本题类似思路,先将矩阵按行反转,在按左上-右下对角线为轴,做两两映射交换。

  1. class Solution {
  2. public:
  3. void rotate(vector<vector<int>>& matrix) {
  4. reverse(matrix.begin(), matrix.end());
  5. for (int i = ; i < matrix.size() - ; ++i) {
  6. for (int j = i; j < matrix.size(); ++j) {
  7. swap(matrix[i][j], matrix[j][i]);
  8. }
  9. }
  10. return;
  11. }
  12. };

【Leetcode】【Medium】Rotate Image的更多相关文章

  1. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  2. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  3. 【LeetCode题意分析&解答】35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  4. ACM金牌选手整理的【LeetCode刷题顺序】

    算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...

  5. 【LeetCode从零单排】No189 .Rotate Array

    称号 Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the arr ...

  6. 【LeetCode每天一题】Rotate List(旋转链表)

    Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: I ...

  7. 【LeetCode每天一题】Rotate Image(旋转矩阵)

    You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise). N ...

  8. 【leetcode刷题笔记】Rotate Image

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  9. 【leetcode刷题笔记】Rotate List

    Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...

  10. 【LeetCode算法题库】Day7:Remove Nth Node From End of List & Valid Parentheses & Merge Two Lists

    [Q19] Given a linked list, remove the n-th node from the end of list and return its head. Example: G ...

随机推荐

  1. A problem has been detected and windows has been shut down to prevent damage

    问题描述 问题解决 参考百度经验, 未解决,重装系统,U盘启动解决.过程可以参考上一篇博文.

  2. c# xml序列化和反序列化。也就是xml的解析和反解析。

    用习惯了newTownSoft.json 的json反序列化.碰到xml是真的不习惯. 每次json反序列化都是直接把json丢到bejson网站生成一个实体类,稍微修改修改一点点变量名.然后直接ne ...

  3. (转)heartbeat原理及部署

    原文:http://yjy724.blog.51cto.com/10897133/1840794---------------------------------------------------h ...

  4. 有意思的shader案例

    屏幕水波效果 https://blog.csdn.net/puppet_master/article/details/52975666

  5. jquery清空下拉框,保留第一个

    js中可以document.getElementById("id").options.length = 1;设置 jquery中的设置方法:$("#id option[i ...

  6. Linux 命令 -- chmod

    chmod命令用来变更文件或目录的权限.在UNIX系统家族里,文件或目录权限的控制分别以读取.写入.执行3种一般权限来区分,另有3种特殊权限可供运用.用户可以使用chmod指令去变更文件与目录的权限, ...

  7. Redis数据持久化机制AOF原理分析一---转

    http://blog.csdn.net/acceptedxukai/article/details/18136903 http://blog.csdn.net/acceptedxukai/artic ...

  8. java.net.SocketException四大异常解决方案---转

    java.net.SocketException如何才能更好的使用呢?这个就需要我们先要了解有关这个语言的相关问题.希望大家有所帮助.那么我们就来看看有关java.net.SocketExceptio ...

  9. Centos7 ftp服务器搭建

    1.使用yum安装ftp服务端: yum install -y vsftpd 2.使用yum安装ftp客户端: yum install -y ftp.x86_64 3.开启ftp服务设置开机启动并查看 ...

  10. FZU2177——ytaaa——————【区间dp】

     ytaaa Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Statu ...