LeetCode48 Rotate Image
题目:
You are given an n x n 2D matrix representing an image.
Rotate the image by 90 degrees (clockwise). (Medium)
Follow up:
Could you do this in-place?
分析:
就是在纸上画一画找到对应关系即可, newMatrix[i][j] = matrix[n - 1 - j][i];
所以简单的方法就是开一个新数组,按照对应关系填入新数组,再拷贝回原来的数组。
代码:
class Solution {
public:
void rotate(vector<vector<int>>& matrix) {
vector<vector<int>> temp = matrix;
for (int i = ; i < temp.size(); ++i) {
for (int j = ; j < temp[].size(); ++j) {
temp[i][j] = matrix[temp.size() - - j][i];
}
}
for (int i = ; i < matrix.size(); ++i) {
for (int j = ; j < matrix[].size(); ++j) {
matrix[i][j] = temp[i][j];
}
}
return;
}
};
题目中的follow-up,要in-place,考虑原数组上如何处理。
要在原数组上处理局部交换感觉就不好想了(会交换完丢元素),把数组整体一起处理好一些。
再观察对应关系,只需要横纵坐标先颠倒,然后纵坐标上下翻转即可。
代码:
class Solution {
public:
void rotate(vector<vector<int>>& matrix) {
for (int i = ; i < matrix.size(); ++i) {
for (int j = ; j < i; ++j) {
swap(matrix[i][j], matrix[j][i]);
}
}
for (int j = ; j < matrix.size() / ; ++j) {
for (int i = ; i < matrix[].size(); ++i) {
swap(matrix[i][j], matrix[i][matrix.size() - - j]);
}
}
return;
}
};
LeetCode48 Rotate Image的更多相关文章
- Leetcode48. Rotate Image旋转图像
给定一个 n × n 的二维矩阵表示一个图像. 将图像顺时针旋转 90 度. 说明: 你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵.请不要使用另一个矩阵来旋转图像. 示例 1: 给定 m ...
- [Swift]LeetCode48. 旋转图像 | Rotate Image
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- leetcode-48.旋转图像
leetcode-48.旋转图像 point: 数组 题意 给定一个 n × n 的二维矩阵表示一个图像. 将图像顺时针旋转 90 度. 说明: 你必须在原地旋转图像,这意味着你需要直接修改输入的二维 ...
- Canvas绘图之平移translate、旋转rotate、缩放scale
画布操作介绍 画布绘图的环境通过translate(),scale(),rotate(), setTransform()和transform()来改变,它们会对画布的变换矩阵产生影响. 函数 方法 描 ...
- [LeetCode] Rotate Array 旋转数组
Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array ...
- [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] Rotate Image 旋转图像
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- jQuery.rotate.js参数
CSS3 提供了多种变形效果,比如矩阵变形.位移.缩放.旋转和倾斜等等,让页面更加生动活泼有趣,不再一动不动.然后 IE10 以下版本的浏览器不支持 CSS3 变形,虽然 IE 有私有属性滤镜(fil ...
- CSS3属性transform详解之(旋转:rotate,缩放:scale,倾斜:skew,移动:translate)
CSS3属性transform详解之(旋转:rotate,缩放:scale,倾斜:skew,移动:translate) 在CSS3中,可以利用transform功能来实现文字或图像的旋转.缩放.倾 ...
随机推荐
- cocos2d-js屏幕任何位置点击开始的实现
ctor:function () { this._super(); if ('mouse' in cc.sys.capabilities) cc.eventManager.addListener({ ...
- First & First
First有记录则返回,否则返回null FirstOrDefault有记录则返回,否则NEW一个新的实体对象返回
- 【转】Hibernate利用@DynamicInsert和@DynamicUpdate生成动态SQL语句
原文链接:http://www.cnblogs.com/quanyongan/p/3152290.html 最近在使用Hibernate4中,发现两个很有奥秘的注解 @DynamicInsert 和 ...
- UVALive 3953 Strange Billboard (状态压缩+枚举)
Strange Billboard 题目链接: http://acm.hust.edu.cn/vjudge/contest/129733#problem/A Description The marke ...
- 什么是Mocking framework?它有什么用?
原位地址:http://codetunnel.com/blog/post/what-is-a-mocking-framework-why-is-it-useful 今天我想讲下关于mocking fr ...
- thinkPHP 无法create,无法插入数据,提示非法数据对象
4.thinkPHP 无法create,提示非法数据对象解决方法:不要create+add,而用 data[]= '';+add$m_r_fa_account = D('R_fa_account'); ...
- sql查找字符串是否包含字符
SELECT [Fgoodsid] ,[Fgoodsname] ,[Fcinema] ,[Fprice] FROM [tenpaytest].[dbo].[goodsinfo]where Fgoods ...
- css 文字超出变 ... 点点点
overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
- Ext.grid.Panel 数据动态改变后刷新grid
gridPanel中加载的数据分为两种:一种是本地数据加载,那另一种就是后台数据加载. 在表格中增.删.改.查 是必不可少的. 那么数据动态改变后怎样刷新表格中的数据呢. 一.后台取数据 var gr ...
- 自行架设DNS的操作步骤及相关说明
关于什么是DNS及相关的名词及说明,请看 http://www.wdlinux.cn/bbs/viewthread.php?tid=1081&highlight=dns这里,只是说明,在wdd ...