【数组】Rotate Image
题目:
You are given an n x n 2D matrix representing an image.
Rotate the image by 90 degrees (clockwise).
思路:
方法一:
可以见矩阵看成多个环组成,如下4*4的矩阵包括两个环,第一个环为1,2,3,4,8,12,16,15,14,13,9,5,1,第二个环为6,7,11,10。

旋转一个矩阵,相当于把每一个环都旋转。如何旋转一个环呢?以最外层的环举例: 本文地址
旋转前:
,旋转后:
我们把环分成3组:{1,4,16,13},{2,8,15,9},{3,12,14,5},每一组中:旋转后相当于把原来的数字移动到同组中下一个数字的位置
/**
* @param {number[][]} matrix
* @return {void} Do not return anything, modify matrix in-place instead.
*/
var rotate = function(matrix) {
var n=matrix.length;
if(n==0){
return;
} var circle=Math.ceil(n/2),len=n;
for(var i=0;i<circle;i++,len-=2){
var m = len - 1;
for(var j = 0; j < m; j++)
{
var tmp = matrix[i][i+j];
matrix[i][i+j] = matrix[i+m-j][i];
matrix[i+m-j][i] = matrix[i+m][i+m-j];
matrix[i+m][i+m-j] = matrix[i+j][i+m];
matrix[i+j][i+m] = tmp;
}
}
};
方法二:
先将矩阵转置,然后把转置后的矩阵每一行翻转
转置变为
每一行翻转变为 
【数组】Rotate Image的更多相关文章
- 回文数组(Rotate Array (JS))
旋转一个数组. function rotate(array,n){ var l =array.length,a=array.map(function(x){return x}),arr=[]; n=n ...
- [Swift]LeetCode189. 旋转数组 | Rotate Array
Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: Input: ...
- LeetCode 189:旋转数组 Rotate Array
公众号:爱写bug(ID:icodebugs) 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数. Given an array, rotate the array to the ...
- LeetCode 189. 旋转数组(Rotate Array)
189. 旋转数组 LeetCode189. Rotate Array 题目描述 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: [1,2,3,4,5,6, ...
- Ruby数组方法整理
数组方法整理 方法列表: all().any().none()和one():测试数组中的所有或部分元素是否满足给定条件.条件可以是语句块中决定,也可以是参数决定 append():等价于push() ...
- Find Minimum in Rotated Sorted Array leetcode java
题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- Workflow:采用坐标变换(移动和旋转)画箭头
背景 流程设计器的连线部分需要画一个箭头代表连接的方向,下图是期望的效果: 刚开始我准备采用三角函数(sin和cos)来计算三角的坐标,实现的过程真不爽(有兴趣的朋友可以试试),就在完工的时候,突然想 ...
- [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——Rotate Image(二维数组顺时针旋转90度)
问题: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockw ...
随机推荐
- Android绘图板的开发
>>继承自View >>使用Canvas绘图 每次View组件上的图形状态数据发生了改变,都应该通知View组件重写调用onDraw(Canvas canvas)方法重绘该组件 ...
- systemtap安装
一.systemtap介绍 SystemTap是一个强大的调试工具,是监控和跟踪运行中的Linux 内核的操作的动态方法,确切的说应该是一门调试语言,因为它有自己的语法,也有解析.编译.运行等过程(准 ...
- Hdu1050 Moving Tables 2016-05-19 16:08 87人阅读 评论(0) 收藏
Moving Tables Problem Description The famous ACM (Advanced Computer Maker) Company has rented a floo ...
- ECG心电图数据2
1.如何下载获取MIT-BIH的数据从下面这个官方链接页面可以下载到所有48组MIT-BIH心电数据: http://www.physionet.org/physiobank/database/mit ...
- 轻松转移github项目步骤
之前有一些项目是托管在github上的,无奈github速度太慢,而且空间有限,还不能有私有项目.后来发现开源中国的git托管(git.oschina.net)还不错,可以托管1000个项目,而且可以 ...
- spring获取webapplicationcontext,applicationcontext几种方法详解(转)
方法一:在初始化时保存ApplicationContext对象 代码: ApplicationContext ac = new FileSystemXmlApplicationContext(&quo ...
- Kafka consumer的参数
earliest: 当各分区下有已提交的offset时,从提交的offset开始消费:无提交的offset时,从头开始消费 latest :当各分区下有已提交的offset时,从提交的offset开始 ...
- ElasticSearch学习总结(二):ES介绍与架构说明
本文主要从概念以及架构层面对Elasticsearch做一个简单的介绍,在介绍ES之前,会先对ES的"发动机"Lucene做一个简单的介绍 1. Lucene介绍 为了更深入地理解 ...
- 雨天的尾巴(bzoj3307)(线段树合并+树上差分)
\(N\)个点,形成一个树状结构.有\(M\)次发放,每次选择两个点\(x,y\) 对于\(x\)到\(y\)的路径上(含\(x,y\))每个点发一袋\(Z\)类型的物品.完成 所有发放后,每个点存放 ...
- Android 美学设计基础 <1>
在做原型的时候,和设计师交流的过程中,发现在设计安卓交互的过程中,其实是存在一些基本规则的.那这些规则,可以保证第一应用美观,第二不会出现反人类的开发难度,第三,用设计师的话说就是可能会有“最好的体现 ...