Spiral Matrix II
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.

For example,
Given n = 3,

You should return the following matrix:
[
 [ 1, 2, 3 ],
 [ 8, 9, 4 ],
 [ 7, 6, 5 ]
]

SOLUTION 1:

还是与上一题Spiral Matrix类似的算法,我们使用x1,y1作为左上角的起点,x2,y2记录右下角,这样子旋转时会简单多了。

 public int[][] generateMatrix1(int n) {
int[][] ret = new int[n][n]; if (n == ) {
// return a [] not a NULL.
return ret;
} int number = ;
int rows = n; int x1 = ;
int y1 = ; while (rows > ) {
int x2 = x1 + rows - ;
int y2 = y1 + rows - ; // the Whole first row.
for (int i = y1; i <= y2; i++) {
number++;
ret[x1][i] = number;
} // the right column except the first and last line.
for (int i = x1 + ; i < x2; i++) {
number++;
ret[i][y2] = number;
} // This line is very important.
if (rows <= ) {
break;
} // the WHOLE last row.
for (int i = y2; i >= y1; i--) {
number++;
ret[x2][i] = number;
} // the left column. column keep stable
// x: x2-1 --> x1 + 1
for (int i = x2 - ; i > x1; i--) {
number++;
ret[i][y1] = number;
} // remember this.
rows -= ;
x1++;
y1++;
} return ret;
}

SOLUTION 2:

还是与上一题Spiral Matrix类似的算法,使用Direction 数组来定义旋转方向。其实蛮复杂的,也不好记。但是记住了应该是标准的算法。

 /*
Solution 2: use direction.
*/
public int[][] generateMatrix2(int n) {
int[][] ret = new int[n][n];
if (n == ) {
return ret;
} int[] x = {, , -, };
int[] y = {, , , -}; int num = ; int step = ;
int candElements = ; int visitedRows = ;
int visitedCols = ; // 0: right, 1: down, 2: left, 3: up.
int direct = ; int startx = ;
int starty = ; while (true) {
if (x[direct] == ) {
// visit the Y axis
candElements = n - visitedRows;
} else {
// visit the X axis
candElements = n - visitedCols;
} if (candElements <= ) {
break;
} // set the cell.
ret[startx][starty] = ++num;
step++; // change the direction.
if (step == candElements) {
step = ;
visitedRows += x[direct] == ? : ;
visitedCols += y[direct] == ? : ; // change the direction.
direct = (direct + ) % ;
} startx += y[direct];
starty += x[direct];
} return ret;
}

SOLUTION 3:

无比巧妙的办法,某人的男朋友可真是牛逼啊![leetcode] Spiral Matrix | 把一个2D matrix用螺旋方式打印

此方法的巧妙之处是使用TOP,BOOTOM, LEFT, RIGHT 四个边界条件来限制访问。其实和第一个算法类似,但是更加简洁易懂。10分钟内AC!

 /*
Solution 3: 使用四条bound来限制的方法.
*/
public int[][] generateMatrix(int n) {
int[][] ret = new int[n][n];
if (n == ) {
return ret;
} int top = , bottom = n - , left = , right = n - ;
int num = ;
while (top <= bottom) {
if (top == bottom) {
ret[top][top] = num++;
break;
} // first line.
for (int i = left; i < right; i++) {
ret[top][i] = num++;
} // right line;
for (int i = top; i < bottom; i++) {
ret[i][right] = num++;
} // bottom line;
for (int i = right; i > left; i--) {
ret[bottom][i] = num++;
} // left line;
for (int i = bottom; i > top; i--) {
ret[i][left] = num++;
} top++;
bottom--;
left++;
right--;
} return ret;
}

GitHub Code:

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/array/GenerateMatrix1.java

LeetCode: Spiral Matrix II 解题报告-三种方法解决旋转矩阵问题的更多相关文章

  1. 【LeetCode】59. Spiral Matrix II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 维护四个边界和运动方向 保存已经走过的位置 日期 题 ...

  2. 三种方法解决android帮助文档打开慢

    三种方法解决android帮助文档打开慢   经查是因为本地文档中的网页有如下两段js代码会联网加载信息,将其注释掉后就好了 <link rel="stylesheet" h ...

  3. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  4. css - 三种方法解决LI和内部Img的上下间距问题

    在火狐浏览器和谷歌浏览器(qq浏览器,谷歌内核)bug类似这张图: img的高度是190*127 但是放到li中,li并没有设置高度,却和内部的图片之间上下错位. 若强行给li设置高度127,他和im ...

  5. LeetCode: Unique Paths II 解题报告

    Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution  Fol ...

  6. [LeetCode] Spiral Matrix II 螺旋矩阵之二

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

  7. [Leetcode] spiral matrix ii 螺旋矩阵

    Given an integer n, generate a square matrix filled with elements from 1 to n 2 in spiral order. For ...

  8. 【LeetCode】885. Spiral Matrix III 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  9. 【LeetCode】240. Search a 2D Matrix II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

随机推荐

  1. Caffe源代码中Solver文件分析

    Caffe源代码(caffe version commit: 09868ac , date: 2015.08.15)中有一些重要的头文件,这里介绍下include/caffe/solver.hpp文件 ...

  2. FPGA视频拼接器的DP 的4k 功能输入, 把lvds 转为 serdes

    4k(3840 x 2160 @60hz)越来越成为一种视频趋势,怎样把4K 接入到拼接器中.是一个棘手的问题. 先看一下原理图的接口: 这是 anx1122的 lvds的接口. 以下看一下系统的框架 ...

  3. HTTP协议详解之请求篇

    •HTTP请求由3部分组成:请求行.消息报头.请求正文. 1)请求行以一个方法符号开头,以空格分开,后面跟随请求的URI和协议的版本,格式:Method Request-URI HTTP-Versio ...

  4. DevExpress.XtraLayout.LayoutControl 动态添加控件

    // Create an item within a specified group,// bound to a specified data field with the specified edi ...

  5. LRM-00109-ORACLE启动报错

    不经意间的错误操作,万里堤坝溃于蚁穴! 问题描述: SQL> startupORA-01078: failure in processing system parametersLRM-00109 ...

  6. 关于 while(1)和for(;;)效率问题的一点想法

    这几天看到好友的文章关于while(1)和for(;;)效率的讨论,手痒说了两句.回头一寻思,自己也仅仅是判断.没有做不论什么实验,我们就看看这两种写法究竟有什么差别: 实验环境:IAR EWARM ...

  7. this.class.getClassLoader().getResourceAsStream与this.class.getResourceAsStream

    本文部分转自:http://xixinfei.iteye.com/blog/1256291 this.getClass().getClassLoader().getResource("tem ...

  8. TScrollBox响应鼠标滚轮问题

    Delphi的TScrollBox本身并不响应鼠标滚轮事件(不知道为什么),但可以在ScrollBox的鼠标滚动事件中进行控制: procedure TfrmTaskNoteEdit.ScrollBo ...

  9. Windows下面安装和配置Solr 4.9(三)支持中文分词器

    首先将下载解压后的solr-4.9.0的目录里面F:\tools\开发工具\Lucene\solr-4.9.0\contrib\analysis-extras\lucene-libs找到lucene- ...

  10. 【驱动笔记9】初探IRP

    文章作者:grayfox作者主页:http://nokyo.blogbus.com原始出处:http://www.blogbus.com/nokyo-logs/34005738.html 此前我们可能 ...