【数组】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 ...
随机推荐
- UVa 11039 Building designing (贪心+排序+模拟)
题意:给定n个非0绝对值不相同的数,让他们排成一列,符号交替但绝对值递增,求最长的序列长度. 析:我个去简单啊,也就是个水题.首先先把他们的绝对值按递增的顺序排序,然后呢,挨着扫一遍,只有符号不同才计 ...
- 记录:Web相关政策之备案号、视频播放
(一)备案号链接: 服务器在国内的网站受工信部监管,并受其颁布的<管理办法>约束.根据<互联网信息服务管理办法>及<非经营性互联网信息服务备案管理办法>的法律法规, ...
- SAX, JSON , DOM 数据解析
//解析:将特定数据格式(如:xml,json)中提取出来所需的内容 //SAX: Simply API for XML, xml解析的一种方式,逐行解析,读一行内容,取一行内容,速度慢,占用内存小, ...
- (水题) Div 3 -- SGU -- 105
链接: http://vj.acmclub.cn/contest/view.action?cid=168#problem/E 时限:250MS 内存:4096KB 64位IO格式:%I ...
- java基础-day5
第05天 java基础知识 今日内容介绍 u 方法的概述及基本使用 u 方法的练习及注意事项 u 方法的重载及参数传递 u 方法的操作数组的练习 第1章 方法的概述及基本使用 1.1 方法定义格 ...
- handsontable 问题
碰到问题了,去官网上找community:http://docs.handsontable.com/0.16.1/tutorial-quick-start.html 1. 描述:把handson ta ...
- What if you are involved in an automobile accident in the US
What if you are involved in an automobile accident in the US With increasing Chinese tourists and vi ...
- 集合(二)LinkedList
上一篇中讲解了ArrayList,本篇文章讲解一下LinkedList的实现. LinkedList是基于链表实现的,所以先讲解一下什么是链表.链表原先是C/C++的概念,是一种线性的存储结构,意思是 ...
- delphi 6数据库连接之长短模式(sqlserver)
delphi 6数据库连接之长短模式(sqlserver) 标签: delphi数据库 2015-08-12 20:59 351人阅读 评论(0) 收藏 举报 分类: delphi(3) 版权声明 ...
- SQL 数据库开发一些精典的代码(转自 咏南工作室)
1.按姓氏笔画排序: Select * From TableName Order By CustomerName Collate Chinese_PRC_Stroke_ci_as 2.数据库加密: s ...