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个位置)都进行替换操作。

代码:

 class Solution {
public:
void rotate(vector<vector<int>>& matrix) {
int n = matrix.size() - ;
for (int i = ; i <= n / ; ++i) {
for (int j = i; j < n - i; ++j) {
int reserve = matrix[i][j];
int p = ;
while (p--) {
if (p == )
matrix[i][j] = reserve;
else
matrix[i][j] = matrix[n-j][i];
int tmp = i;
i = n - j;
j = tmp;
}
}
}
return;
}
};

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

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

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

 class Solution {
public:
void rotate(vector<vector<int>>& matrix) {
reverse(matrix.begin(), matrix.end());
for (int i = ; i < matrix.size() - ; ++i) {
for (int j = i; j < matrix.size(); ++j) {
swap(matrix[i][j], matrix[j][i]);
}
}
return;
}
};

【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. asp.net WebService技术简介

    1.什么是web service? 这里借助百度百科专业的解释:web service是一个平台独立的.低耦合的.自包含的.基于可编程的web应用程序(说简单点,就是使用web service继续不需 ...

  2. JS框架设计之模块加载系统

    任何语言一到大规模应用阶段,必然要拆封模块,有利于维护和团队协作,与Java走得最近的dojo率先引进了加载器,使用document.write与同步Ajax请求实现,后台dojo以JSONP的方法来 ...

  3. Centos 7网卡设置

    #yum install net-tools.x86_64 #cd /etc/sysconfig/network-scripts/ #mv ifcfg-eno16780032 ifcfg-eth0 手 ...

  4. svn update 报错,必须先cleanup,然后cleanup失败解决方法

    一 问题描述: 1.svn update失败,提示已被locked,请执行cleanup 2.执行svn cleanup,提示cleanup failed to process the followi ...

  5. window.history,页面中的返回按钮

    一.页面中的返回按钮事件 window.history可以不加window这个前缀 他的方法有: window.history.go(-1); //-n表示后退n页,n表示前进n页,或者是一个url ...

  6. Centos 从零开始 (二)

    因为我是搞 nodejs的 所以以后会安装一些依赖于node的 比如mongodb数据库等. 6:安装nodejs 安装的时候遇到个小问题.yum install nodejs 报错 说没有这个包.然 ...

  7. Opencv中图像的遍历与像素操作

    Opencv中图像的遍历与像素操作 OpenCV中表示图像的数据结构是cv::Mat,Mat对象本质上是一个由数值组成的矩阵.矩阵的每一个元素代表一个像素,对于灰度图像,像素是由8位无符号数来表示(0 ...

  8. [android] socket在手机上的应用

    1.手机助手 1.1 USB链接 可以读取手机的PID和VID,确定唯一的设备,可以给手机安装对应的驱动等 socket在固定端口通信 1.2 WIFI链接 pc在电脑在整个网段发送UDP数据包,手机 ...

  9. 四、jdbctemplate使用

    这里使用mysql数据库,省略数据库创建过程 1.添加依赖 <!--jdbc--> <dependency> <groupId>org.springframewor ...

  10. System.arraycopy的测试

    ArrayList的源码中数组的拷贝用到该方法: public static void arraycopy(Object src, --源数组 int srcPos, --源数组要复制的起始位置 Ob ...