【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 ...
随机推荐
- 从volatile说到i++的线程安全问题
简介 volatile关键字保证了在多线程环境下,被修饰的变量在别修改后会马上同步到主存,这样该线程对这个变量的修改就是对所有其他线程可见的,其他线程能够马上读到这个修改后值. Thread的本地内存 ...
- poj 1201 Intervals 解题报告
Intervals Time Limit: 2000MS Memory Limit: 65536KB 64bit IO Format: %lld & %llu Submit Statu ...
- 使用自由软件Icarus Verilog Simulator进行仿真
Icarus Verilog Simulator(http://iverilog.icarus.com/home)使用iverilog作为源代码编译器,编译生成vvp程序文本,使用vvp作为运行时引擎 ...
- 带你走进EJB--它都有哪些Bean
通过前面一系列EJB的博客,我们已经对EJB有了一个宏观的了解.为够更好的在企业项目中使用EJB,我们很有必要对EJB的一些基本内容进行深入,这次我们主要进行的主题是Enterprise Java B ...
- Mysql 监控 支持 mysql 多实例自动发现以及主从监控
在[/usr/local/zabbix327/bin] 目录下新建python文件,并增加执行权限,如下: #!/usr/bin/env /usr/bin/python # _*_ coding:ut ...
- Binary Tree Postorder Traversal leetcode java
题目: Given a binary tree, return the postorder traversal of its nodes' values. For example: Given bin ...
- 集合 数组 定义 转换 遍历 Arrays API MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- CI框架入门中的简单MVC样例
最简单的CI模型: 注意:模型须要用到数据库 配置文件在appcation/config.php 这里我们要用到数据库,须要将databases.php中的 相关參数填写一下,详细不再赘述. 直接进入 ...
- phpcms pc标签 start不生效的原因
start 和 page 参数不可同时使用,使用了page参数 会造成start 参数无效
- visual studio2013 php
C:\Users\Administrator\AppData\Local\Microsoft\VisualStudio\12.0\Extensions\DEVSENSE\PHP Tools for V ...