【LeetCode】048. 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]
]
题解:
暴力解
Solution 1
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 - - i; ++j){
int tmp = matrix[i][j];
matrix[i][j] = matrix[n - - j][i];
matrix[n - - j][i] = matrix[n - - i][n - - j];
matrix[n - - i][n - - j] = matrix[j][n - - i];
matrix[j][n - - i] = tmp;
}
}
}
};
Solution 2
class Solution {
public:
void rotate(vector<vector<int>>& matrix) {
int n = matrix.size();
for(int i = ; i < n; ++i){
for(int j = ; j < n - i; ++j){
swap(matrix[i][j], matrix[n - - j][n - - i]);
}
}
for(int i = ; i < n / ; ++i){
for(int j = ; j < n; ++j){
swap(matrix[i][j], matrix[n - - i][j]);
}
}
}
};
先沿着副对角线(/)翻转,再沿着水平中线翻转。
Solution 3
class Solution {
public:
void rotate(vector<vector<int>>& matrix) {
int n = matrix.size();
for(int i = ; i < n / ; ++i){
for(int j = ; j < n; ++j){
swap(matrix[i][j], matrix[n - - i][j]);
}
}
for(int i = ; i < n; ++i){
for(int j = ; j < i; ++j){
swap(matrix[i][j], matrix[j][i]);
}
}
}
};
先沿着水平中线翻转,再沿着主对角线(\)翻转。
Solution 4
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]);
}
}
}
};
先对原数组取其转置(即沿着主对角线翻转),然后把每行的数字翻转(沿着竖直中线翻转)。另,Solution 4 也可写为Solution 4.1
Solution 4.1
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]);
}
reverse(matrix[i].begin(), matrix[i].end());
}
}
};
【LeetCode】048. Rotate Image的更多相关文章
- 【LeetCode】61. Rotate List 解题报告(Python)
[LeetCode]61. Rotate List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...
- 【LeetCode】48. Rotate Image
Difficulty:medium More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/rotate-image/ ...
- 【LeetCode】189. Rotate Array 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 切片 递归 日期 题目地址:https://leet ...
- 【LeetCode】48. Rotate Image 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】189 - 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】61. Rotate List
Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...
- 【LeetCode】48. Rotate Image (2 solutions)
Rotate Image You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees ...
- 【LeetCode】396. Rotate Function 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/rotate-fu ...
- 【LeetCode】796. Rotate String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
随机推荐
- SDOI2012 Round1 day2 拯救小云公主(dis)解题报告
#include<cstdio> #include<cmath> #include<iostream> using namespace std; typedef l ...
- hdu 5969 最大的位或
最大的位或 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submi ...
- Zookeeper数据与存储
一.前言 前面分析了Zookeeper对请求的处理,本篇博文接着分析Zookeeper中如何对底层数据进行存储,数据存储被分为内存数据存储于磁盘数据存储. 二.数据与存储 2.1 内存数据 Zooke ...
- POJ 1694 An Old Stone Game【递归+排序】
链接: http://poj.org/problem?id=1694 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=27454#probl ...
- Django 路飞学成书写规范的总结
路飞学成书写规范的总结 命名 项目/文件/函数 -> 小写+下划线 类 驼峰式 路由 将每个功能的路由进行分割, 或者做上注释 类和方法的注释 每个类都要注释是干什么的 每个方法也要进行注释标明 ...
- 北京君正集成电路的Newton平台--穿戴式
版权声明:本文为博主原创文章,转载请注明出处. https://blog.csdn.net/hellomxj1/article/details/25324125 Newton1开发平台 • 很适合 ...
- (转)Web Service和WCF的到底有什么区别
[1]Web Service:严格来说是行业标准,也就是Web Service 规范,也称作WS-*规范,既不是框架,也不是技术. 它有一套完成的规范体系标准,而且在持续不断的更新完善中. 它使用XM ...
- R语言数据管理(四):数据导出
与read.*函数对应,导出函数为write.*函数. 比较常见的为write.csv和write.table. 一般格式: setwd("D:\\") write.table(y ...
- QT设置textEdit光标到末尾
//移动光标到末尾 QTextCursor cursor = ui->receiveTextEdit->textCursor(); cursor.movePosition(QTextCur ...
- Facial landmark detection - 人脸关键点检测
Facial landmark detection (Facial keypoints detection) OpenSourceLibrary: DLib Project Home: http: ...