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?

方阵顺时针旋转90度,方法:

  1. 按行reverse
  2. 交换元素 A[i, j] = A[j, i]

逆时针90度方法:

  1. 按列reverse
for (auto vi : matrix) reverse(vi.begin(), vi.end());
  1. 交换元素 A[i, j] = A[j, i]

代码们:

// clockwise rotate
// 1. 按行reverse 2. 交换元素 A[i, j] = A[j, i]
/*
* clockwise rotate
* first reverse up to down, then swap the symmetry
* 1 2 3 7 8 9 7 4 1
* 4 5 6 => 4 5 6 => 8 5 2
* 7 8 9 1 2 3 9 6 3
*/
void rotate(vector<vector<int>>& A) {
const int n = A.size();
reverse(A.begin(), A.end()); for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
swap(A[i][j], A[j][i]);
}
}
}
/*
* anticlockwise rotate
* first reverse left to right, then swap the symmetry
* 1 2 3 3 2 1 3 6 9
* 4 5 6 => 6 5 4 => 2 5 8
* 7 8 9 9 8 7 1 4 7
*/
void anti_rotate(vector<vector<int>>& A) {
const int n = A.size();
for(atuo vi : A) reverse(vi.begin(), vi.end()); for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
swap(A[i][j], A[j][i]);
}
}
}

48. Rotate Image(中等)的更多相关文章

  1. [Leetcode][Python]48: Rotate Image

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 48: Rotate Imagehttps://leetcode.com/pr ...

  2. [array] leetcode - 48. Rotate Image - Medium

    leetcode - 48. Rotate Image - Medium descrition You are given an n x n 2D matrix representing an ima ...

  3. 刷题48. Rotate Image

    一.题目说明 题目是48. Rotate Image,简而言之就是矩阵顺时针旋转90度.不允许使用额外的矩阵. 经过观察(写一个矩阵,多看几遍就知道了),旋转90度后: 第1行变为len-1列(最后一 ...

  4. 48. Rotate Image - LeetCode

    Question 48. Rotate Image Solution 把这个二维数组(矩阵)看成一个一个环,循环每个环,循环每条边,每个边上的点进行旋转 public void rotate(int[ ...

  5. [LeetCode] 48. Rotate Image 旋转图像

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  6. [leetcode 48] rotate image

    1 题目 You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwi ...

  7. 48. Rotate Image

    题目: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwis ...

  8. leetCode 48.Rotate Image (旋转图像) 解题思路和方法

    Rotate Image You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees ...

  9. LeetCode OJ 48. Rotate Image

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

随机推荐

  1. Stanford依存句法关系解释

    ROOT:要处理文本的语句 IP:简单从句 NP:名词短语 VP:动词短语 PU:断句符,通常是句号.问号.感叹号等标点符号 LCP:方位词短语 PP:介词短语 CP:由'的'构成的表示修饰性关系的短 ...

  2. multiprocessing.Process() ----------python中的多进程

    python 当中 使用封装好的 multiprocessing 为我们实现创建多进程任务. 1 Process()方法创建子进程 使用multiprocessing.Process() 方法产生一个 ...

  3. 使用 C#/.NET Core 实现单体设计模式

    本文的概念内容来自深入浅出设计模式一书 由于我在给公司做内培, 所以最近天天写设计模式的文章.... 单体模式 Singleton 单体模式的目标就是只创建一个实例. 实际中有很多种对象我们可能只需要 ...

  4. 相同域名不同端口的两个应用,cookie名字、路径都相同的情况下,会覆盖吗

    首先答案是: 会的. 本地测试流程: 两个相同的应用,代码完全相同:只是部署在两个不同的tomcat:域名都是localhost 应用A:部署在http://localhost:8087/ 应用B:部 ...

  5. linux系统下Apache日志分割(按天生成文件)

    Apache日志按天显示,修改Apache http.conf文件,注释默认的日志文件,修改为下面2行 ErrorLog "| /usr/local/apache/bin/rotatelog ...

  6. git checkout+文件丢失

    坑:不知什么时候, 应该是初学git的时候, 在桌面git init了一下, 这次忘记切目录直接在桌面git checkout了, 导致文件丢失了. 解决: 简单复原: git reflog # 查看 ...

  7. 使用WSUS离线下载补丁并安装在非联网的windows系统中(以Windows Server 2008 r2为例)

    首先我失去https://serverfault.com/questions/322938/finding-and-downloading-all-available-win2008-r2-and-w ...

  8. springboot快速入门

    SpringBoot简介 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再 ...

  9. [LeetCode] Redundant Connection II 冗余的连接之二

    In this problem, a rooted tree is a directed graph such that, there is exactly one node (the root) f ...

  10. Mysql运算符与函数(胖胖老师)

    use test;create table `employee`(    emp_no int unsigned,    emp_name varchar(30),    emp_sex varcha ...