【一天一道LeetCode】#48. Rotate Image
一天一道LeetCode系列
(一)题目
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?
(二)解题
90度旋转图像,我们不难看出
经过这样的变换后,图像就旋转了90度。
class Solution {
public:
void rotate(vector<vector<int>>& matrix) {
int n = matrix.size()-1;
vector<vector<int>> tmp = matrix;//深拷贝
for(int i = 0 ; i< n+1; i++)
{
for(int j = 0 ; j < n+1 ; j++)
{
matrix[j][n-i] = tmp[i][j];//赋值转换
}
}
}
};
网上还有一种解法去先转置在对称变换。代码如下:
class Solution {
public:
void rotate(vector<vector<int> > &matrix) {
int dim = matrix.size();
int temp = 0;
for (int i = 0; i < dim; ++i) { //先转置
for (int j = i+1; j < dim; ++j) {
temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
for (int i = 0; i < dim/2; ++i) { //然后对称变换
for (int j = 0; j < dim; ++j) {
temp = matrix[j][i];
matrix[j][i] = matrix[j][dim - i -1];
matrix[j][dim - i -1] = temp;
}
}
}
};
【一天一道LeetCode】#48. Rotate Image的更多相关文章
- [array] leetcode - 48. Rotate Image - Medium
leetcode - 48. Rotate Image - Medium descrition You are given an n x n 2D matrix representing an ima ...
- 每日一道 LeetCode (48):最长回文子串
每天 3 分钟,走上算法的逆袭之路. 前文合集 每日一道 LeetCode 前文合集 代码仓库 GitHub: https://github.com/meteor1993/LeetCode Gitee ...
- [LeetCode] 48. Rotate Image 旋转图像
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- LeetCode 48. Rotate Image(旋转图像)
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- LeetCode 48 Rotate Image(2D图像旋转问题)
题目链接: https://leetcode.com/problems/rotate-image/?tab=Description Problem:给定一个n*n的二维图片,将这个二维图片按照顺时 ...
- [leetcode 48] rotate image
1 题目 You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwi ...
- leetCode 48.Rotate Image (旋转图像) 解题思路和方法
Rotate Image You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees ...
- [leetcode]48. Rotate Image旋转图像
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- LeetCode 48. Rotate Image My Submissions Question (矩阵旋转)
题目大意:给一个矩阵,将其按顺时针旋转90°. 题目分析:通法是先将矩阵转置,然后再反转每一行,或者是先反转每一列,然后再将其转置.I just want to say"It's amazi ...
- LeetCode 48. Rotate Image (C++)
题目: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwis ...
随机推荐
- PHP FTP 函数
PHP FTP 简介 FTP 函数通过文件传输协议 (FTP) 提供对文件服务器的客户端访问. FTP 函数用于打开.登录以及关闭连接,同时用于上传.下载.重命名.删除及获取文件服务器上的文件信息.不 ...
- J-Link固件烧录以及使用J-Flash向arm硬件板下载固件程序
这篇文章的最初版本是在15年写的https://blog.csdn.net/u010592722/article/details/45575663,后来又遇到了一些新问题,故更新在了这里. 一.始于安 ...
- RDO Stack: Failed connect to server
Issue: When you create an instance, but cannot connect to the VNC Server because of the error messag ...
- postgresql 登录查看表定义
su - postgres psql \connect database_name; \d table_name
- Android属性动画完全解析(中),ValueAnimator和ObjectAnimator的高级用法
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/43536355 大家好,在上一篇文章当中,我们学习了Android属性动画的基本用法 ...
- 网站开发进阶(四十四)input type="submit" 和"button"的区别
网站开发进阶(四十四)input type="submit" 和"button"的区别 在一个页面上画一个按钮,有四种办法: 这就是一个按钮.如果你不写ja ...
- Java 资源本地化与国际化
资源包 在编写应用程序的时候,需要面对的一个问题是如何来处理与locale相关的一些信息.比如,页面上的一些静态文本就希望能够以用户习惯的语言显示.最原始的做法是将这些信息硬编码到程序中(可能是一大串 ...
- Unsupported major.minor version 52.0
今天运行项目,切换一下eclipse,运行程序突然发现普通的类main()方法无法运行,报错详细信息如下: Exception in thread "main" java.lang ...
- [Python]查看python路径以及安装包的路径
特别是linux系统,装了多个python,有时候找不到python的绝对路径,有时候装了个django,又找不到django安装到哪里了..当然查看的方法有很多种,这里列出几种,供没有经验的人参考下 ...
- [lua]写个简单的Lua拓展-sleep函数
这几天在做一个小项目,其中用到了一些基本的API, 例如sleep,获取当前目录等等,lua标准库中没有提供这些接口,虽然所第三方库中也都有实现,但是要用的就那么几个函数,在一个嵌入式系统中安装那么多 ...