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. eclipse to avoid the message, disable the...

      标题 CreateTime--2018年5月9日10:38:15 Author:Marydon 1.问题描述 2.问题解析 这是因为eclipse的智能提示超时引起的,将超时间调大即可,如:200 ...

  2. SettingsEclipse&MyEclipse

      eclipse优化 迁移时间--2017年5月20日09:39:16 CreateTime--2016年11月18日11:27:02 Author:Marydon ModifyTime--2017 ...

  3. Content-Length实体的大小

    •15.2 Content-Length实体的大小 Content-Length首部指出了报文中实体主体的字节大小,这个大小包含了所有内容的编码,如对文本进行gzip压缩的话,那么Content-Le ...

  4. linux limits.conf 配置

    转自:http://kerry.blog.51cto.com/172631/300784 limits.conf 文件实际是 Linux PAM(插入式认证模块,Pluggable Authentic ...

  5. HashTable、List、ArrayList的经典使用和相互转换

    1.添加引用 using System.Collections; 2.创建并添加数据 Hashtable hs = new Hashtable(); hs.Add("Name1", ...

  6. C中字符串分割函数strtok的一个坑

    strtok的典型用法是: p = strtok(s4, split); while(p != NULL){ printf("%s\n", p); p = strtok(NULL, ...

  7. 马老师 生产环境mysql主从复制、架构优化方案

    Binlog日志(主服务器) => 中继日志(从服务器 运行一遍,保持一致).从服务器是否要二进制日志取决于架构设计.如果二进制保存足够稳定,从性能上来说,从服务器不需要二进制日志.默认情况下, ...

  8. Android Developers:使ListView滑动流畅

    流畅滑动ListView的关键是保持应用程序的主线程(UI线程)从免于繁重处理.确保你的任何硬盘访问,网络访问或者SQL访问在一个单独的线程中.为了测试你的应用个程序的状态,你能启动StrictMod ...

  9. Linux命令-安全复制命令:scp

    scp是有Security的文件copy,基于ssh登录.操作起来比较方便,比如要把当前一个文件copy到远程另外一台主机上. 命令格式: scp [可选参数] 源文件 目标文件 scp 本地文件 远 ...

  10. Linux 分区注意事项

    必须分区: 1)/(根分区) 2)/swap(交换分区,当内存不超过4G时,建议swap大小为内存2倍,若超过4G,建议交换分区跟内存一样大) 推荐分区 /boot(启动分区,单独分区,最新200M)