Rotate Image 旋转图像
You are given an n x n 2D matrix representing an image.
Rotate the image by 90 degrees (clockwise).
Note:
You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.
Example 1:
Given input matrix =
[
[1,2,3],
[4,5,6],
[7,8,9]
], rotate the input matrix in-place such that it becomes:
[
[7,4,1],
[8,5,2],
[9,6,3]
]
Example 2:
Given input matrix =
[
[ 5, 1, 9,11],
[ 2, 4, 8,10],
[13, 3, 6, 7],
[15,14,12,16]
], rotate the input matrix in-place such that it becomes:
[
[15,13, 2, 5],
[14, 3, 4, 1],
[12, 6, 8, 9],
[16, 7,10,11]
]
一个n x n的二维矩阵表示一个图像,将图像顺时针旋转90度。要求in-place,所以就不能用额外的空间了。
解法1: 先以对角线为轴翻转得到其转置矩阵,再以中间竖轴翻转。
1 2 3 1 4 7 7 4 1
4 5 6 --> 2 5 8 --> 8 5 2
7 8 9 3 6 9 9 6 3
解法2: 先以反对角线翻转,在以中间水平轴翻转。
1 2 3 9 6 3 7 4 1
4 5 6 --> 8 5 2 --> 8 5 2
7 8 9 7 4 1 9 6 3
解法一
class Solution {
public:
void rotate(vector<vector<int> > &matrix) {
int n=matrix.size();
for(int i=;i<n;++i)
{
for(int j=;j<i;++j)
swap(matrix[i][j],matrix[j][i]);
}
for(int i=;i<n;++i)
for(int j=;j<n/;++j)
swap(matrix[i][j],matrix[i][n-j-]);
}
};
Rotate Image 旋转图像的更多相关文章
- 048 Rotate Image 旋转图像
给定一个 n × n 的二维矩阵表示一个图像.将图像旋转 90 度(顺时针).注意:你必须在原矩阵中旋转图像,请不要使用另一个矩阵来旋转图像.例 1:给出的输入矩阵 = [ [1,2,3], [4 ...
- Leetcode48. Rotate Image旋转图像
给定一个 n × n 的二维矩阵表示一个图像. 将图像顺时针旋转 90 度. 说明: 你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵.请不要使用另一个矩阵来旋转图像. 示例 1: 给定 m ...
- [LeetCode] 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 (旋转图像) 解题思路和方法
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 旋转图像
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- [CareerCup] 1.6 Rotate Image 翻转图像
1.6 Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, write a m ...
- [LeetCode] 61. Rotate List 旋转链表
Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: I ...
- LeetCode解题录-1~50
[leetcode]1. Two Sum两数之和 Two Pointers, HashMap Easy [leetcode]2. Add Two Numbers两数相加 Math, LinkedLis ...
随机推荐
- 【C语言】 二叉树的基本运算
• 二叉树节点类型BTNode: typedef struct node { char data; struct node *lchild, *rchild; } BTNode; 创建二叉树 void ...
- NFS服务自动搭建及挂载脚本
一.写脚本的动机 由于最近老是搭建NFS,虽然不复杂,但是很繁琐.安装服务.修改配置文件.手动挂载.写入开机自动挂载等于是就写了一个脚本 二.脚本说明及审明 作用:该脚本主要实现NFS自动安装,客户端 ...
- 003 关于shell基础,大数据的前期准备
一:正则表达式 1.通配符与正则表达式的区别 通配符有 *,?,[]. 区别: 通配符用来匹配文件名 正则表达式是为了匹配字符串 2.“.*”的意思 .:任意字符 *:匹配前一个字符任意次 3.过滤出 ...
- PHP函数之array_chunk
有时候需要对数组进行按分页处理,之前的做法是计算出数组大小,按分页计算出偏移量,再从起始偏移量处开始遍历页大小个数据.现在不用这么麻烦了,原来PHP函数里有个现成的函数array_chunk可以配合我 ...
- ListView优化中的细节问题
1.android:layout_height属性: 必须将ListView的布局高度属性设置为非“wrap_content”(可以是“match_parent / fill_parent / ...
- POJ 3903 Stock Exchange 【最长上升子序列】模板题
<题目链接> 题目大意: 裸的DP最长上升子序列,给你一段序列,求其最长上升子序列的长度,n^2的dp朴素算法过不了,这里用的是nlogn的算法,用了二分查找. O(nlogn)算法 #i ...
- Oracle - Dbms Output window
Ensure that you have your Dbms Output window open through the view option in the menubar. Click on t ...
- saxon 处理xslt
下载saxon : https://sourceforge.net/projects/saxon/?source=typ_redirect 下载后拿到: saxon9he.jar 运行CMD: C:\ ...
- 每天刷Web面试题(前10天汇总)
一.算法题部分 1. 如何获取浏览器URL中查询字符串中的参数? function getParamsWithUrl(url) { var args = url.split('?'); ...
- [CC-LONCYC]Lonely Cycles
[CC-LONCYC]Lonely Cycles 题目大意: \(T(T\le1000)\)组数据. 给定一张简单图(不含重边与自环),图中有\(n(n\le2\times10^5)\)个节点和\(m ...