Rotate Image - LeetCode 48
来源:互联网 发布:windows多线程注意什么 编辑:程序博客网 时间:2024/04/29 11:45
题目描述:
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?
Tags: Array
分析:
down->left;
right->down;
up->right。
注意四条边的元素的总数为4*(n-1-lay)其中lay为层数,因此每一条边只有n-1-lay个数要处理。
迭代过程中千万注意下标计算:通过观察可得以下规律:
up的行下标为lay(层数);
down的行下标为n-lay-1;
left的列下标为lay;
right的列下标为n-lay-1。
迭代的时候:
left->up时,up的列下标与left的行下标之和为n-1;
down ->left时,left的行下标与down的列下标相等;
right->down,down的列下标与right的行下标之和为n-1;
up->right,right的行下标与up的列下标相等。
方法二、现将矩阵沿副对角线对称交换,然后再按水平中线对称交换即可。或者先按水平中线对称交换,然后再按主对角线对称交换。
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?
Tags: Array
分析:
此题要求将一个 n x n 方阵顺时针旋转90度。
方法一、分析后可发现:每一层可以看作是一个四边形,并且每一层的的处理是可以分开进行的。每一层的处理过程是:
up->tmp ;
left->up;down->left;
right->down;
up->right。
注意四条边的元素的总数为4*(n-1-lay)其中lay为层数,因此每一条边只有n-1-lay个数要处理。
迭代过程中千万注意下标计算:通过观察可得以下规律:
up的行下标为lay(层数);
down的行下标为n-lay-1;
left的列下标为lay;
right的列下标为n-lay-1。
迭代的时候:
left->up时,up的列下标与left的行下标之和为n-1;
down ->left时,left的行下标与down的列下标相等;
right->down,down的列下标与right的行下标之和为n-1;
up->right,right的行下标与up的列下标相等。
方法二、现将矩阵沿副对角线对称交换,然后再按水平中线对称交换即可。或者先按水平中线对称交换,然后再按主对角线对称交换。
以下是C++实现代码,从外层到内层逐一处理:
/**/////方法一/////////6ms//////////////////////*/class Solution {public: void rotate(vector<vector<int>>& matrix) { if(matrix.empty()) return; int n = matrix.size(); int lay = 0; for(; lay <= n / 2; lay++) /*each layer*/ { for(int i = lay; i < n-lay-1; i++) { int tmp = matrix[lay][i]; matrix[lay][i] = matrix[n-i-1][lay]; // up = left matrix[n-i-1][lay] = matrix[n-lay-1][n-i-1]; // left = down matrix[n-lay-1][n-i-1] = matrix[i][n-lay-1]; // down = right matrix[i][n-lay-1] = tmp; // right = up; } } }};
/*//////////方法二//////////6ms////////////////////////*/class Solution {public: void rotate(vector<int > &matrix) { int i,j,temp; int n=matrix.size(); // 沿着副对角线反转 for (int i = 0; i < n; ++i) { for (int j = 0; j < n - i; ++j) { temp = matrix[i][j]; matrix[i][j] = matrix[n - 1 - j][n - 1 - i]; matrix[n - 1 - j][n - 1 - i] = temp; } } // 沿着水平中线反转 for (int i = 0; i < n / 2; ++i){ for (int j = 0; j < n; ++j) { temp = matrix[i][j]; matrix[i][j] = matrix[n - 1 - i][j]; matrix[n - 1 - i][j] = temp; } } }};
0 0
- [leetcode 48] Rotate Image
- [LeetCode 48]Rotate Image
- leetcode || 48、Rotate Image
- leetcode-48 Rotate Image
- Rotate Image - LeetCode 48
- Leetcode 48: Rotate Image
- Leetcode #48 Rotate Image
- LeetCode(48) Rotate Image
- leetcode 48: Rotate Image
- Leetcode#48||Rotate Image
- leetcode 48:Rotate Image
- 【leetcode】【48】Rotate Image
- Leetcode 48 - Rotate Image
- LeetCode 48 - Rotate Image
- 【LeetCode-48】Rotate Image
- LeetCode 48 Rotate Image
- Leetcode 48 Rotate Image
- [Leetcode] #48 Rotate Image
- 关于Android的上下文“Context”
- 一些学习链接
- 5-14 数据结构啊poi D.折叠纸片
- 1.最简单的操作系统(二)makefile
- 写在前面
- Rotate Image - LeetCode 48
- 第三章 第39题
- Unity3D之触摸输入实现在指定地形上拖拽物体
- Laravel5 之 发送邮件
- opencv学习之threshol函数
- iOS7学习点滴-笔记2
- IDO one smart wristwatch Bluetooth Sync Smart Watch Pedometer and Sleep fitness bracelet
- 字典树(Trie树)用法及例子(一)
- 5-14 数据结构啊poi F.文化