LeetCode 48. Rotate Image (C++)
题目:
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]
]
分析:
很明显,题目要求矩阵顺时针旋转90度,且要求不开辟新的空间。我们可以先求矩阵的转置,之后再将转置好的矩阵前后列交换,如下图。
程序:
class Solution {
public:
void rotate(vector<vector<int>>& matrix) {
int n = matrix.size();
for (int i = ; i < n-; i++){
for (int j = i+; j < n; j++){
swap(matrix[i][j], matrix[j][i]);
}
}
for (int i = ; i < n/; i++){
for (int j = ; j < n; j++){
swap(matrix[j][i],matrix[j][n--i]);
}
}
}
};
LeetCode 48. Rotate Image (C++)的更多相关文章
- [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. 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
题意 原地顺时针翻转一个 n*n 的矩阵 图解 下面例子中用 5*5 矩阵做示例,如下图,我们要把该矩阵顺时针翻转90度,并且不能使用另外的矩阵空间来暂存数据,而是原地改变矩阵中数值. 我的想法是这样 ...
随机推荐
- 底层文件I/O操作中read()函数的缓存问题
最近在学习Linux过程中看到文件I/O操作这里时,文件I/O操作的系统调用涉及的5个函数:open(),read(),write(),lseek(),close().在一开始就阐明这些函数的特点是不 ...
- ORACLE->SQL*Loader[20180712]
https://docs.oracle.com/cd/B28359_01/server.111/b28319/ldr_concepts.htm#g1013706 SQL*Loader将外部 ...
- MYSQL 入门全套
MySQL简介 1.什么是数据库 ? 数据库(Database)是按照数据结构来组织.存储和管理数据的仓库,它产生于距今六十多年前,随着信息技术和市场的发展,特别是二十世纪九十年代以后,数据管理不再仅 ...
- sklearn的train_test_split,果然很好用啊!
sklearn的train_test_split train_test_split函数用于将矩阵随机划分为训练子集和测试子集,并返回划分好的训练集测试集样本和训练集测试集标签. 格式: X_tra ...
- (杭电 2045)不容易系列之(3)—— LELE的RPG难题
不容易系列之(3)-- LELE的RPG难题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- Linux入门第四天——shell基础
一.shell概述 1.概述 命令行解释器(壳,也就是我们的操作界面),计算机只认识0101的二进制,我们需要通过ASCII表来进行翻译 较为官方的解释是: Shell 是一个用 C 语言编写的程序, ...
- sougoupinyin for linux 安装步骤(精简版)
download deb double-click to install select fcitx reboot click it in the bar and choose the"tex ...
- sql语句-2-字符串数字日期时间
- 【HEOI2016】排序
题面 题解 这题好神仙啊... 我们二分这个位置上的数, 然后当\(val[i] \geq mid\)的位置设为\(1\),否则为\(0\) 这样一来,这道题就变成了一个\(01\)序列排序,所以就可 ...
- 【BZOJ3142】[HNOI2013]数列
[BZOJ3142][HNOI2013]数列 题面 洛谷 bzoj 题解 设第\(i\)天的股价为\(a_i\),记差分数组\(c_i=a_{i+1}-a_i\) 则 \[ Ans=\sum_{c_1 ...