【Leetcode】【Medium】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?
解题思路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的更多相关文章
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【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 ...
- ACM金牌选手整理的【LeetCode刷题顺序】
算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...
- 【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 ...
- 【LeetCode每天一题】Rotate List(旋转链表)
Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: I ...
- 【LeetCode每天一题】Rotate Image(旋转矩阵)
You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise). N ...
- 【leetcode刷题笔记】Rotate Image
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- 【leetcode刷题笔记】Rotate List
Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...
- 【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 ...
随机推荐
- 读取P12格式证书的密钥
不想存储p12证书内容,只想存储证书密钥,可通过以下实现读取证书的密钥出来: package com.zat.ucop.service.util; import org.apache.commons. ...
- Centos6.5上安装sonarqube6.7.6
Centos6.5已经装备好,可以联网 sonarqube6.7.6下载地址:https://www.sonarqube.org/downloads/ 步骤: 安装MySQL5.7 MySQL详细的安 ...
- for、while、do while 3种循环异同点
for (; ; ){ 循环体} while(循环条件){ 循环体} do{ 循环体}while(循环条件); 执行顺序不同: for循环和while循环:先判断条件为true时,然后再执行 do w ...
- 基于web端去除空格小工具
读论文时,不时需要抓取PDF版的段落,可是复制到word的时候会出现很多空格,利用javascript强大的功能,几行命令实现了去除段落里的空格,实现如下: <!DOCTYPE html PUB ...
- JSON中的坑
坑一. 在使用localStorage时,我们会给一个key存取一个value,这个value可以是一个普通的字符串,也可以是一个对象,如果是一个字符串,我们就需要通过JSON.stringify来转 ...
- JDBC(1)-连接数据库
主要步骤包括: 加载驱动: 连接数据库: 使用语句操作数据库: 关闭数据库连接,释放资源. 1.需要导包: 2.加载数据驱动: mysql驱动名:com.mysql.jdbc.Driver 加载方式: ...
- 关于new Option()
先来了解下,如何运用js实现select动态添加option. //1.动态创建select function createSelect(){ var mySelect = document.crea ...
- Netbeans8的中文乱码
Netbeans8的中文乱码 前提: 不是编码的问题,统一改成UTF-8,之后文字还是显示方块.使用以下方式解决: 菜单 -> 工具 -> 选项 -> 字体和颜色 -> 语法 ...
- Jedis连接redis
今天与大家分享下,Jedis连接池使用.先看一段JAVA 代码: JedisPoolConfig config = new JedisPoolConfig(); con ...
- Intellij IDEA 远程debug、远程tomcat部署项目