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功能来实现文字或图像的旋转.缩放.倾 ...
随机推荐
- 算法导论-动态规划(最长公共子序列问题LCS)-C++实现
首先定义一个给定序列的子序列,就是将给定序列中零个或多个元素去掉之后得到的结果,其形式化定义如下:给定一个序列X = <x1,x2 ,..., xm>,另一个序列Z =<z1,z2 ...
- deep learning 的java库
deepLearning4j: https://github.com/deeplearning4j/libnd4j/issues/208 deepLearn ...
- HDOJ_1010 Tempter of the Bone
http://acm.hdu.edu.cn/showproblem.php?pid=1010 奇偶剪枝:可以把map看成这样: 0 1 0 1 0 1 1 0 1 0 1 0 0 1 0 1 0 1 ...
- 全球各大运营商黑莓ROM官网下载链接汇总
全球各大运营商黑莓ROM官网下载链接汇总: 先来关于8830的. 4.2.2.196的rom 8830EFS_PBr4.2.2_rel365_PL3.0.0.85_A4.2.2.196_Sprint ...
- LeetCode104: Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- oracle学习 十二 使用.net程序调用带返回值的存储过程(持续更新)
数据库返回的是结果集,存储过程返回的是一个或者多个值,所以不要使用while循环去读取,也不要使用datareader函数去调用.v_class_name是返回函数 使用.net调用oracle数据库 ...
- VS2010编译器下针对C#和C++的opencv的配置方法
我们大家都知道opencv是针对C.C++编写的,没有独立的编译调试工具.所以今天就捣鼓了一下在xp vs2010下配置C++和C#环境下的opencv.请大家一步一步的按步骤操作.本人亲自鉴定可行. ...
- angular select中ng-options使用
function selectCtrl($scope) { $scope.selected = ''; $scope.model = [{ id: 10001, mainCategory: '男', ...
- IntegrityError错误
Python插入数据库提交失败,一直走IntegrityError错误,没打印错误信息(一定注意编码规范,记住打印错误信息),以为插不进去,弄了好久,最后打印了错误信息 (sqlite3.Integr ...
- jQuery 的选择器
本文来自网上转帖 1. 基础选择器 Basics 名称 说明 举例 #id 根据元素Id选择 $("divId") 选择ID为divId的元素 element 根据元素的名称选择, ...