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. 用Android NDK编译FFmpeg

    附(2018-01-06):     有一个将x264及lame等库集成进去了且基于android的ffmpeg的编译方法,地址参见:       https://github.com/writing ...

  2. Ubuntu 16.04 安装 JDK 及 Eclipse 详细步骤(转发:https://blog.csdn.net/bluish_white/article/details/56509446)

    2017.3.1更新 修正了一些命令,现在按照文章步骤配置不会出现问题了. JDK 安装及配置 参考来源:http://www.linuxidc.com/Linux/2017-02/140908.ht ...

  3. PHP 错误日志/安全配置

    PHP 常用配置 /php/bin/php -i | head Loaded Configuration File => /php/etc/php.ini 查看php配置目录 vim /usr/ ...

  4. springmvc 整合微信

    springmvc 整合微信 方式一: ① 配置验证 @RequestMapping(value = "/into", method = RequestMethod.GET, pr ...

  5. 20145222黄亚奇《网络对抗》- shellcode注入&Return-to-libc攻击深入

    20145222黄亚奇<网络对抗>- shellcode注入&Return-to-libc攻击深入 shellcode注入实践过程

  6. idea中如何debug本地maven项目

    方法一:使用maven中的jetty插件调试本地maven项目 1.打断点 2.右击“jetty:run”,选择Debug运行 3.浏览器发送http请求,开始调试 方法二:利用远程调试功能调试本地m ...

  7. 搭建 Spring 开发环境

    把以下 jar 包加入到工程的 classpath 下: Spring 的配置文件: 一个典型的 Spring 项目需要创建一个或多个 Bean 配置文件, 这些配置文件用于在 Spring IOC ...

  8. iOS动画进阶 - 手摸手教你写 Slack 的 Loading 动画

    如果移动端访问不佳,可以访问我的个人博客 前几天看了一篇关于动画的博客叫手摸手教你写 Slack 的 Loading 动画,看着挺炫,但是是安卓版的,寻思的着仿造着写一篇iOS版的,下面是我写这个动画 ...

  9. idea软件编码已经设置好了为utf-8,但是svn中down下来的文件格式本身不是utf-8的,此时打开后会出现中文乱码解决方法

    我是个idea的忠实用户,新公司的项目都是用eclipse做的,通过svn拉下代码后发现,注释的内容里,中文内容都是乱码.问过项目负责人,说可能是GBK编码. 但是,我通过idea的setting设置 ...

  10. MyCat配置和使用

    1.什么是MyCat2.MyCat应用场景3.使用MyCat路由实现读写分离4.SpringBoot动态数据源切换原理5..SpringBoot项目实现读写分离 使用MyCat实现读写分离 什么是My ...