【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 ...
随机推荐
- c语言三元组
// Triplet.cpp : 定义控制台应用程序的入口点.//#include "stdio.h"#include "stdlib.h"#define OK ...
- elasticsearch从入门到出门-04-入门的几个需求练手
第一个分析需求:计算每个tag下的商品数量 GET /ecommerce/product/_search{ "aggs": { "group_by_tags&qu ...
- They're much closer in spirit to how our brains work than feedforward networks.
http://neuralnetworksanddeeplearning.com/chap1.html Up to now, we've been discussing neural networks ...
- unity里standard pbr(一)
关注forwardbase下的 standard.shader #pragma vertex vertBase #pragma fragment fragBase #include "Uni ...
- 牛客小白月赛1 C 分元宵【快速幂】
题目链接 https://www.nowcoder.com/acm/contest/85/C 思路 有 A 种 元宵馅,B 种元宵皮 所以 我们可以认为 有Q = A * B 种 元宵 有 C 张桌子 ...
- poj 3006 Dirichlet's Theorem on Arithmetic Progressions【素数问题】
题目地址:http://poj.org/problem?id=3006 刷了好多水题,来找回状态...... Dirichlet's Theorem on Arithmetic Progression ...
- 算法(Algorithms)第4版 练习 1.4.6
(1)sum = N + N/2 + N/4 + …… + 1 假设N是2的倍数(N = 2q),则sum = N -1 ~ N (2)sum = 1+2+……N/2 同(1)分析,sum = N/ ...
- Docker学习(二)
docker run ubuntu /bin/echo 'Hello world' docker run -t -i ubuntu /bin/bash docker run 命令运行一个容器 -t ...
- 百度地图省市县乡镇街道对应ZOOM级别
百度地图省市县乡镇街道对应ZOOM级别
- 解析centos中Apache、php、mysql 默认安装路径
apache:如果采用RPM包安装,安装路径应在 /etc/httpd目录下apache配置文件:/etc/httpd/conf/httpd.confApache模块路径:/usr/sbin/apac ...