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?
题目要就地算法,那么可以通过折叠矩阵上下以及斜对角线折叠来实现,代码如下:

 class Solution {
public:
void rotate(vector<vector<int>>& matrix) {
int sz = matrix.size();
if(!sz) return;
for(int i = ; i < sz; ++i){
for(int begin = , end = sz - ; begin < end; ++begin, --end){
swap(matrix[begin][i], matrix[end][i]);
}
}
for(int i = ; i < sz; ++i){
for(int j = i; j < sz; ++j){
swap(matrix[i][j], matrix[j][i]);
}
}
}
};

同样的通过乘以一个矩阵的方法当然也可以实现,不过那不是in-place算法。

LeetCode OJ:Rotate Image(旋转图片)的更多相关文章

  1. [LeetCode] 61. Rotate List 旋转链表

    Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: I ...

  2. [Leetcode] rotate image 旋转图片

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

  3. [LeetCode] 189. Rotate Array 旋转数组

    Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: Input: ...

  4. [leetcode]61. Rotate List旋转链表

    Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: I ...

  5. leetCode 61.Rotate List (旋转链表) 解题思路和方法

    Rotate List  Given a list, rotate the list to the right by k places, where k is non-negative. For ex ...

  6. [LeetCode] Rotate List 旋转链表

    Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...

  7. rotate.js实现图片旋转 (chrome,IE,firefox都可以实现)

    找了好多资料,要么是IE可以用,但是谷歌不行,,还有就是两个都可以用的,图片大小显示不全.终于找到一个好一点的js,先贴一下代码. 1.rotate.js jQuery.fn.rotate = fun ...

  8. LeetCode OJ 题解

    博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...

  9. LeetCode 48. Rotate Image(旋转图像)

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

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

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

随机推荐

  1. node.js基本工作原理及流程

    概述 Node.js是什么 Node 是一个服务器端 JavaScript 解释器,用于方便地搭建响应速度快.易于扩展的网络应用.Node.js 使用事件驱动, 非阻塞I/O 模型而得以轻量和高效,非 ...

  2. github-----文件项目的推拉二式

    将本地项目文件推送上线: $ git init $ git add . $ git commit -m "第一次修改" $ git log $ git remote add ori ...

  3. Java分布式:RPC(远程过程调用)

    Java分布式:RPC(远程过程调用) 引入RPC 比如我们有一个查询的接口IDBQuery,以及其实现类DBQueryImp,如果我们执行IDBQuery查询方法,只需要new一个DBQueryIm ...

  4. js高级---js运行原理

    概述 浏览器组成可分两部分:Shell+内核.浏览器内核又可以分成两部分:渲染引擎(layout engineer或者Rendering Engine)和JS引擎.渲染引擎功能作用 渲染引擎,负责对网 ...

  5. cdoj1341卿学姐与城堡的墙

    地址:http://acm.uestc.edu.cn/#/problem/show/1341 题目: 卿学姐与城堡的墙 Time Limit: 2000/1000MS (Java/Others)    ...

  6. hadoop08---读写锁

    ReentrantLock 直接使用lock接口的话,我们需要实现很多方法,不太方便,ReentrantLock是唯一实现了Lock接口的类,并且ReentrantLock提供了更多的方法,Reen ...

  7. Django学习笔记之模板渲染、模板语言、simple_tag、母版子版、静态配置文件

    一.首先我们用PyCharm来创建一个Django项目 终端命令:django-admin startproject sitename 图形创建:   这样一个Django项目就创建完成了,上面可以看 ...

  8. Mysql:ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

    Mysql:ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES) Linux: MyS ...

  9. Node.Js安装教程

    Node.Js安装教程 介绍下我的环境 环境 值 操作系统 win10 64bit Node.Js 8.9.4 emmmm 表格中毒了,为什么出不来效果 一.下载及安装 这个可以去Node.Js官网上 ...

  10. 一位交易巨匠的十年心得:如何使用MACD判断后期趋势,把握买卖点

    高手与散户的区别在哪里? 高手与散户的区别在哪里,从宏观上讲:一流高手用境界,二流高手用趋势,三流高手用技术,普通散户用迷糊.最高境界的人在讲心境如何,什么也不看,只用感觉就能炒好股赚钱.我说一流高手 ...