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. 关于 tf.nn.softmax_cross_entropy_with_logits 及 tf.clip_by_value

    In order to train our model, we need to define what it means for the model to be good. Well, actuall ...

  2. 关于shared pool的深入探讨(三)

    基本命令: ALTER SESSION SET EVENTS 'immediate trace name LIBRARY_CACHE level LL'; 其中LL代表Level级别,对于9.2.0及 ...

  3. Codeforces Round #468(div2)

    A Friends Meeting 题意:有两个人在数轴上的不同位置,现在他们需要到一个位置碰面.每次每人只能向左或向右走1个单位,轮流进行.每个人第一次走时疲劳度+1,第二次走时疲劳度+2,以此类推 ...

  4. Android平台利用OpenCL框架实现并行开发初试

    http://www.cnblogs.com/lifan3a/articles/4607659.html 在我们熟知的桌面平台,GPU得到了极为广泛的应用,小到各种电子游戏,大到高性能计算,多核心.高 ...

  5. 【Flask】Flask Restful api

    ### 安装: Flask-Restful需要在Flask 0.8以上的版本,在Python2.6或者Python3.3上运行.通过pip install flask-restful即可安装. ### ...

  6. Saltstack 命令参数整理

    命令:salt-key # 测试 命令:salt-key -A # 管理所有minion 命令:salt-key -a # 管理定义minion 命令:salt-key -d ID名字 # 删除单个m ...

  7. jQuery幻灯片播放器插件

    在线演示 本地下载

  8. shell脚本实现进度条

    使用shell脚本编写进度条 可已加入到shell脚本当中 主要作用:好看 美观 没毛用 (一) 普通进度条: #!/bin/bashb='' for ((i=0;$i<=20;i++)) do ...

  9. BUG: scheduling while atomic 分析【转】

    本文转载自:https://blog.csdn.net/cfy_phonex/article/details/12090943 遇到一个典型的schedule问题.   <3>[26578 ...

  10. Android 6.0中在/dev下添加新设备驱动下Selinux相关设置【转】

    本文转载自:https://blog.csdn.net/fantasy_wxe/article/details/52013922 错误1: 07-23 13:06:57.617   117   117 ...