【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 (clockwise).
Follow up: Could you do this in-place?
最简单的想法,两重循环。
第一重循环是对于矩阵的层次(从外到内)
第二重循环是对于每一层次的四元素轮换
举例:
1 2 3
4 5 6
7 8 9
最外层:
1-->3-->9-->7
2-->6-->8-->4
最内层:
5
class Solution {
public:
void rotate(vector<vector<int> > &matrix) {
if(matrix.empty())
return;
int n = matrix.size();
int temp;
for(int i = ; i < (n+)/; i ++)
{
for(int j = i; j < n--i; j ++)
{
temp = 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] = temp;
}
}
}
};

还有一种技巧性比较强的解法。
顺时针旋转90度可以分解为两步:
1、上下颠倒reverse
2、转置transpose
举例:
1 2 3
4 5 6
7 8 9
reverse之后
7 8 9
4 5 6
1 2 3
transpose之后
7 4 1
8 5 2
9 6 3
class Solution {
public:
void rotate(vector<vector<int> > &matrix) {
if(matrix.empty() || matrix[].empty())
return;
reverse(matrix.begin(), matrix.end());
int m = matrix.size();
int n = matrix[].size();
for(int i = ; i < m; i ++)
{
for(int j = i+; j < n; j ++)
{
swap(matrix[i][j], matrix[j][i]);
}
}
}
};

【LeetCode】48. Rotate Image (2 solutions)的更多相关文章
- 【LeetCode】48. Rotate Image
Difficulty:medium More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/rotate-image/ ...
- 【LeetCode】48. Rotate Image 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】61. Rotate List 解题报告(Python)
[LeetCode]61. Rotate List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...
- 【一天一道LeetCode】#48. Rotate Image
一天一道LeetCode系列 (一)题目 You are given an n x n 2D matrix representing an image. Rotate the image by 90 ...
- 【LeetCode】189. Rotate Array 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 切片 递归 日期 题目地址:https://leet ...
- 【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】75. Sort Colors (3 solutions)
Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...
- 【LeetCode】90. Subsets II (2 solutions)
Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...
- 【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 ...
随机推荐
- Python的开源人脸识别库:离线识别率高达99.38%
Python的开源人脸识别库:离线识别率高达99.38% github源码:https://github.com/ageitgey/face_recognition#face-recognitio ...
- Linux C Socket编程发送结构体、文件详解及实例
利用Socket发送文件.结构体.数字等,是在Socket编程中经常需要用到的.由于Socket只能发送字符串,所以可以使用发送字符串的方式发送文件.结构体.数字等等. 本文:http://www.c ...
- 原生js获取宽高与jquery获取宽高的方法的关系
说明:1.因为获取高度的情况跟获取宽度的情况一样,所以以下只说获取宽度的情况. 2.以下所说的所有方法与属性所返回的值都是不带单位的. 3.为了方便说明,以下情况采用缩写表示: obj -> ...
- Maven Dependencies没有了的解决办法
头疼的问题,maven Dependencies突然没有了,别的项目都有,个别的却怎么也出不来. 以下是某大神的解决方法,特此转发,以防丢失: 网上搜索了一番,大多都是下面这种做法: 右击 Mave ...
- CSS中英文字符两端对齐实现
两端对齐实现 一般加上下面2行就可实现 display: inline-block; text-align: justify; 但是对于中英文混杂的情况,中英文难一起实现对齐,原因在下面有分析,需要如 ...
- ECMAScript5之Object学习笔记(二)
继续第二部分 Object.freeze(obj) 看字面意思就是“把一个对象冻结”. 下面我们来看个简单的例子以作说明: // a person instance var person = { na ...
- [Python] 错误“IndentationError: unindent does not match any outer indentation level”是什么意思?
文本没有对齐,建议打开文本编辑器的Tab显示(我用的Editplus是视图->空白->制表符),看缩进是否合理,调整一致就好了.
- shell命令xargs解析
1.多行变成单行 -bash-3.2# cat test.txt a b c d e f g o p q -bash-3.2# cat test.txt |xargs a b c d e f g o ...
- Discuz常见大问题-如何允许用户插入视频-如何自己在页面中插入视频
从视频的下面分享中获取html代码 然后粘贴到你创建页面的指定位置(注意从优酷复制的视频宽度和高度可能比较小,你可以自己调整,或者占据100%) 最终的实现效果
- shell 编程笔记
#! /bin/sh 寻找shell解释器 /bin/sh 是一个路径 #! /usr/bin/python 仅仅是寻找一个python的解释器 执行linux程序的方法: 使得文件具有可执行的权限 ...