【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 ...
随机推荐
- Spring异常:Annotation-specified bean name.. for bean class ...
Spring重命名问题.对照项目中的注解,查找@Service是否重重名.由于Spring是在注解下按配置扫描的方式去创建对象的,那么两个重名的注解也就不成立了. 备注,特别注意test包下有没有通过 ...
- javaagent笔记及一个基于javassit的应用监控程序demo
javaagent基本用法 定义入口premain public static void premain(String agentArgs, Instrumentation inst) { Syste ...
- 以前折腾的7zip图标
7z.dll替换文件 http://files.cnblogs.com/colben/7zDll.7z
- 巧用hover改变css样式和背景
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 在myeclipse中换项目的jdk版本,你需要做哪些?
首先,我们必须把jdk在系统中安装好,环境变量配好,才能进行下一步的操作…… 然后在点击项目,右键选择Properties,找到Java Build Path,拉倒最下面,把原来的jdk版本给remo ...
- PHP代码语法验证插件:SublimeLinter
1.调出控制台 Ctrl+Shift+P,输入 install package,Enter
- 判断当前IE浏览器是否支持JS
1.server 2008 r2 64位中自带的IE默认不支持js,这样一些有JS的页面就是失效,所以如果要考虑这方面的系统,需要判断浏览器是否支持JS <div class="js- ...
- maven配置编译器的版本
发现每次启动idea时,项目里很多红色错误,都是的编译器版本不对,每次都要手动修改. 其实在pom里把默认编译器版本加进去就好了. <build> <plugins> < ...
- 禅道和JIRA大对比
转自:https://blog.csdn.net/qq_40543535/article/details/78182636?locationNum=9&fps=1 禅道和JIRA大对比 置顶 ...
- 第七章使用java实现面向对象- 多线程
一.Thread类和Runnable接口 1.在java.lang包中定义了Runnable接口和Thread类. Runnable接口中只定义了一个方法,它的格式为: public abstract ...