rotate image(旋转数组)
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(旋转数组)的更多相关文章
- [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 ...
- [LeetCode] 189. Rotate Array 旋转数组
Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: Input: ...
- rotate array 旋转数组
class Solution {public: void rotate(vector<int>& nums, int k) { int n=nums.size(); int i=0 ...
- Rotate Array 旋转数组 JS 版本解法
Given an array, rotate the array to the right by k steps, where k is non-negative. 给定一个数组,并且给定一个非负数的 ...
- 189 Rotate Array 旋转数组
将包含 n 个元素的数组向右旋转 k 步.例如,如果 n = 7 , k = 3,给定数组 [1,2,3,4,5,6,7] ,向右旋转后的结果为 [5,6,7,1,2,3,4].注意:尽可能找 ...
- [LeetCode] Rotate List 旋转链表
Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 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 ...
- 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 ...
- LeetCode 189. 旋转数组(Rotate Array)
189. 旋转数组 LeetCode189. Rotate Array 题目描述 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: [1,2,3,4,5,6, ...
随机推荐
- Volley,小并发网络请求的好帮手
不得不说,当不了解一件事情的时候,就会像当然的认为,其很神秘.但是当真正的接触到了这些神秘的item,就不会有这种感觉了.作为一个android开发新手的我,刚接触到了Volley这个开源的网络请求框 ...
- [GitHub]第三讲:简单分支操作
Git 最核心的操作对象是版本( commit ),最核心的操作技巧就是分支. 什么是分支? 仓库创建后,一旦有了新 commit,默认就会放到一个分支上,名字叫 master.前面咱们一直看到的多个 ...
- WIP 投料报 Invalid Serial Number
1.接口表数据检查无误 2.同样数据界面能正常完成 界面做trace SQL ID: b2mw8gjyv7guh Plan Hash: 2015965341 DELETE FROM MTL_SERIA ...
- 部署Openfire3.9.3源码部署
1,下载Openfire3.9.3源码代码:http://www.igniterealtime.org/downloads/index.jsp 2,具体的配置请参考http://blog.csdn.n ...
- Git版本控制:Git高级教程
http://blog.csdn.net/pipisorry/article/details/50669350 Git有很多命令行参数,使用起来非常方便.可以运行 man git log ,来看一下这 ...
- 当freemarker中EL表达式的值为空时出现异常的解决方法
<#list pageView.list as msg> <form name="msgForm" id="msgForm" ...
- CentOS 64-bit下安装JDK和Tomcat并设置Tomcat开机启动操作步骤
准备文件如下: 1.CentOS-6.4-x86_64-bin-DVD1.iso 2.jdk-7u67-linux-x64.rpm 3.apache-tomcat-7.0.55.tar.gz 安装步骤 ...
- 尚学堂马士兵struts2 课堂笔记(四)
27 结果类型 主要就四种种 dispatch和rediret chain和drdirectaction <package name="resultTypes" namesp ...
- JAVA内部类_2
(d)匿名内部类 如果只创建这个类的第一个对象,就无需命名. 由于构造器的名字必须与类名相同,而匿名类没有类名,所以匿名类没有构造器. 取而代之的是将构造器参数传递给超类构造器. 在内部类实现接口的时 ...
- Java 8时间和日期API 20例
本文由 ImportNew - Sandy 翻译自 javarevisited.欢迎加入翻译小组.转载请见文末要求. 伴随lambda表达式.streams以及一系列小优化,Java 8 推出了全新的 ...