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)的更多相关文章

  1. 【LeetCode】48. Rotate Image

    Difficulty:medium  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/rotate-image/ ...

  2. 【LeetCode】48. Rotate Image 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  3. 【LeetCode】61. Rotate List 解题报告(Python)

    [LeetCode]61. Rotate List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...

  4. 【一天一道LeetCode】#48. Rotate Image

    一天一道LeetCode系列 (一)题目 You are given an n x n 2D matrix representing an image. Rotate the image by 90 ...

  5. 【LeetCode】189. Rotate Array 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 切片 递归 日期 题目地址:https://leet ...

  6. 【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  ...

  7. 【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 ...

  8. 【LeetCode】90. Subsets II (2 solutions)

    Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...

  9. 【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 ...

随机推荐

  1. css3 实现圆角边框的border-radius属性和实现阴影效果的box-shadow属性

    首先我要介绍的是border-radius属性,它的作用是实现圆角边框,其中border-radius:20px;表示,一个’体‘四个角都圆滑20px,其值如果为100px那么圆角度则为最高,如果是正 ...

  2. With Visual Studio, Open Same File In Two Windows, Updates Reflected in Both

    I’ve always been frustrated in Visual Studio (all versions I can remember including latest vs2012) w ...

  3. MySQL冷知识

    问题:在网站后台添加了扩展字段后,对于数据库表不太熟悉的,可能会花较长时间查找,如何有效提高我们的工作效率呢? 解决方法:利用SQL语句来查询字段所在的表

  4. [12] 扇形体(Fan)图形的生成算法

    顶点数据的生成 bool YfBuildFunVertices ( Yreal radius, Yreal degree, Yreal height, Yuint slices, YeOriginPo ...

  5. UVA 400 (13.08.05)

     Unix ls  The computer company you work for is introducing a brand new computer line and is developi ...

  6. Pytorch torch.optim优化器个性化使用

    一.简化前馈网络LeNet 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 im ...

  7. 错误: 找不到或无法加载主类 Files\red5-server ,原因与解决办法

    因为你把 red5放到了 Program Files 下,而Program Files 中间有个空格,启动路径不允许有空格,换个没空格的路径就OK啦

  8. Oracle中的字符处理方法

    向左补全字符串 lpad(字段名,填充长度,填充的字符) ,') from dual; 向右补全字符串 rpad(字段名,填充长度,填充的字符) ,') from dual; 返回字符串小写 sele ...

  9. 【Nodejs】“快算24”扑克牌游戏算法 1.01

    考虑到1.00版本需要改源码变更四个数字,这版修改了一下变成控制台输入了. 先把两个数四则运算得到结果想清楚就是C2代码,三个数就是C3,四个数就是C4.简单的代码简单,却是复杂的基础:复杂的脱胎于简 ...

  10. iOS开发系列课程预告

    近期在Mac和iOS上做开发,认为应该写一点东西分享给感兴趣的童鞋们.在此之前.以前有非常多同行们都在埋怨苹果Objective-C的复杂和难以上手,为此也有非常多人对今年(2014年)刚推出的Swi ...