You are given an n x n 2D matrix representing an image.

Rotate the image by 90 degrees (clockwise).

Note:
You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.

Example 1:

Given input matrix =
[
[1,2,3],
[4,5,6],
[7,8,9]
], rotate the input matrix in-place such that it becomes:
[
[7,4,1],
[8,5,2],
[9,6,3]
]

Example 2:

Given input matrix =
[
[ 5, 1, 9,11],
[ 2, 4, 8,10],
[13, 3, 6, 7],
[15,14,12,16]
], rotate the input matrix in-place such that it becomes:
[
[15,13, 2, 5],
[14, 3, 4, 1],
[12, 6, 8, 9],
[16, 7,10,11]
]

这题就相当于找规律。。
规律:将第一行和最后一行交换,第二行和倒数第二行交换,。。。然后将关于对角线对称的元素交换。

代码如下:注意二维数组的长度表示。

class Solution {
public void rotate(int[][] matrix) {
//第一步:将第一行和最后一行交换,第二行和倒数第二行交换。。
for(int i=0;i<matrix.length/2;i++)
for(int j=0;j<matrix[0].length;j++){
int tmp=matrix[i][j];
matrix[i][j]=matrix[matrix.length-1-i][j];
matrix[matrix.length-1-i][j]=tmp;
} //第二步:交换关于对角线对称的元素
for(int i=0;i<matrix.length;i++)
for(int j=i+1;j<matrix[0].length;j++){
int tem=matrix[i][j];
matrix[i][j]=matrix[j][i];
matrix[j][i]=tem;
}
}
}

rotate image(旋转数组)的更多相关文章

  1. [LeetCode] Rotate Array 旋转数组

    Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array  ...

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

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

  3. rotate array 旋转数组

    class Solution {public: void rotate(vector<int>& nums, int k) { int n=nums.size(); int i=0 ...

  4. Rotate Array 旋转数组 JS 版本解法

    Given an array, rotate the array to the right by k steps, where k is non-negative. 给定一个数组,并且给定一个非负数的 ...

  5. 189 Rotate Array 旋转数组

    将包含 n 个元素的数组向右旋转 k 步.例如,如果  n = 7 ,  k = 3,给定数组  [1,2,3,4,5,6,7]  ,向右旋转后的结果为 [5,6,7,1,2,3,4].注意:尽可能找 ...

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

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

  9. LeetCode 189. 旋转数组(Rotate Array)

    189. 旋转数组 LeetCode189. Rotate Array 题目描述 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: [1,2,3,4,5,6, ...

随机推荐

  1. 【一天一道LeetCode】#172. Factorial Trailing Zeroes

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  2. Android实战之ListView复选框

    项目中有用到复选框的例子,啊啊......在网上查找有关资料,大多都是过于繁琐,所以自己决定写个这个方面的demo... 先给个效果图: 在ListView中添加复选框主要注意以下几个问题: 1.Li ...

  3. 03-Git常用命令演示、冲突演示

    Git常用命令演示 Git的的思想其实和SVN还是蛮像的,可以参考之前svn文章一起加深了解. 新建一个user2目录,clone下代码. 修改readme.txt git status 可以看到re ...

  4. [GitHub]第一讲:浏览器中使用GitHub

    文章转载自http://blog.csdn.net/loadsong/article/details/51591407 看到一篇关于GitHub的文章,感觉不错,因此转载来以备推敲学习. 不会用 Gi ...

  5. JSP连接MySQL时老是遇到驱动错误怎么办?

    在使用JSP进行web开发的时候总是会不可避免的遇到各种各样的问题.今天我也来讲一讲我遇到的一些奇葩的问题. 驱动出错 一开始我总是以为是我导入到工程的里的jar包的问题,于是我就试验了好几个连接My ...

  6. JAVA之旅(二十一)——泛型的概述以及使用,泛型类,泛型方法,静态泛型方法,泛型接口,泛型限定,通配符

    JAVA之旅(二十一)--泛型的概述以及使用,泛型类,泛型方法,静态泛型方法,泛型接口,泛型限定,通配符 不知不觉JAVA之旅已经写到21篇了,不得不感叹当初自己坚持要重学一遍JAVA的信念,中途也算 ...

  7. iOS中 FMDB第三方SQLite数据库 UI_20

    1.什么是FMDB? FMDB是iOS平台下SQLite数据库,只不过它是OC方式封装了C语言的SQLite语句,使用起来更加面向对象 2.FMDB的优点:1.使用起来更加面向对象; 2.对比苹果自带 ...

  8. 四大组件之ContentProvider小结

    总结提高,与君共勉 1.什么是Content Provider 内容提供器( Content Provider)主要用于在不同的应用程序之间实现数据共享的功能,提供了一套完整的机制,允许一个程序访问另 ...

  9. 高通 8x12 添加 TP和按键

    1 .在tp的驱动文件中添加以下代码实现按键功能 [plain] view plain copy static ssize_t ft5x06_virtual_keys_register(struct  ...

  10. 漫谈程序员(十一)老鸟程序员知道而新手不知道的小技巧之Web 前端篇

    老鸟程序员知道而新手不知道的小技巧 Web 前端篇 常充电!程序员只有一种死法:土死的. 函数不要超过50行. 不要一次性写太多来不及测的代码,而是要写一段调试一段. UI和编码要同步做. 多写注释方 ...