【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 ...
随机推荐
- System.Security.Cryptography.CryptographicException 微信支付中公众号发红包时候碰到的错误。
转 留记录.我是第二个错误原因 我总结了一下出现证书无法加载的原因有以下三个 1.证书密码不正确,微信证书密码就是商户号 解决办法:请检查证书密码是不是和商户号一致 2.IIS设置错误,未加载用户配置 ...
- ios 开发之旅
你可能还在跟我一样傻傻的研究,怎么用visual studio 开发ios 里,哪就浪费时间吧!因为在安装 xmarin的时候,自动可以选择ios for Visual studio ,安装完也不能编 ...
- pow() 函数
pow() 函数用来求 x 的 y 次幂(次方),其原型为: double pow(double x, double y); #include<iostream> #include< ...
- ckeditor添加代码插入功能及高亮显示(插件)
Auto SyntaxHighlighter SyntaxHighlighter CKEditor Button 下载以上两个插件,启用 以下可有可无: (设置在编辑器的显示样式) ckeditor高 ...
- js 获取 客户区 大小
js 获取 客户区 大小 本文内容来自<javascript高级程序设计(第二版)> 内容, 只是方便大家以后可能会用到... <script type="text/jav ...
- Java 实例 - 标签(Label)
Java 实例 Java 中的标签是为循环设计的,是为了在多重循环中方便的使用break 和coutinue . 以下实例当在循环中使用 break 或 continue 循环时跳到指定的标签处: ...
- HTTP Error 502.5 - Process Failure asp.net core error in IIS
在windows server 2012 上安装完dotnet-win-x64.1.1.1.exe 和 DotNetCore.1.0.4_1.1.1-WindowsHosting.exe后,没有重启服 ...
- [转]微信小程序联盟 跳坑《一百八十一》设置API:wx.openSetting使用说明
本文转自:http://www.wxapp-union.com/forum.php?mod=viewthread&tid=4066 这个API解决了过去一个长久以来无法解决的问题,如何让用户重 ...
- 记一次使用cmd执行java文件遇到的坑...包括“使用java命令运行class文件提示“错误:找不到或无法加载主类“的问题”
今天写了一个java文件,类似聊天软件的东西.在eclipse里输入输出显得没感觉,于是乎就准备在cmd里输入和显示输出.如下图,我准备运行的是ChatDemo.class文件.路径是:D:\work ...
- [Java反射基础四]通过反射了解集合泛型的本质
本文接上文"方法反射的基本操作",利用反射了解下java集合中泛型的本质 1.初始化两个集合,一个使用泛型,一个不使用 ArrayList list1 = new ArrayLis ...