LeetCode: Spiral Matrix II 解题报告-三种方法解决旋转矩阵问题
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 解题报告-三种方法解决旋转矩阵问题的更多相关文章
- 【LeetCode】59. Spiral Matrix II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 维护四个边界和运动方向 保存已经走过的位置 日期 题 ...
- 三种方法解决android帮助文档打开慢
三种方法解决android帮助文档打开慢 经查是因为本地文档中的网页有如下两段js代码会联网加载信息,将其注释掉后就好了 <link rel="stylesheet" h ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- css - 三种方法解决LI和内部Img的上下间距问题
在火狐浏览器和谷歌浏览器(qq浏览器,谷歌内核)bug类似这张图: img的高度是190*127 但是放到li中,li并没有设置高度,却和内部的图片之间上下错位. 若强行给li设置高度127,他和im ...
- LeetCode: Unique Paths II 解题报告
Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Fol ...
- [LeetCode] Spiral Matrix II 螺旋矩阵之二
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- [Leetcode] spiral matrix ii 螺旋矩阵
Given an integer n, generate a square matrix filled with elements from 1 to n 2 in spiral order. For ...
- 【LeetCode】885. Spiral Matrix III 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】240. Search a 2D Matrix II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
随机推荐
- java 复制Map对象(深拷贝与浅拷贝)
java 复制Map对象(深拷贝与浅拷贝) CreationTime--2018年6月4日10点00分 Author:Marydon 1.深拷贝与浅拷贝 浅拷贝:只复制对象的引用,两个引用仍然指向 ...
- 细说HTML元素的隐藏和显示
CSS文档对HTML的显示和隐藏有2个属性可供选择: 1.display 2.visiblity 这2个有什么区别呢? display: display版本:CSS1/CSS2 兼容性:IE4+ NS ...
- java中的Checked Exception和Unchecked Exception的区别
Java 定义了两种异常: - Checked exception: 继承自 Exception 类是 checked exception.代码需要处理 API 抛出的 checked excepti ...
- LoadRunner脚本关联动态数据的最简单方法
为什么要关联动态数据呢?举个例子,在对我们平台的工作流性能测试时, 在待办任务里面选择一条记录执行发送操作,LoadRunner VuGen会详细记录下来流程发送操作的细节,但在回放脚本的时候会有问题 ...
- C#:确保绑定到同一数据源的多个控件保持同步
下面的代码示例演示如何使用 BindingSource 组件,将三个控件(两个文本框控件和一个 DataGridView 控件)绑定到 DataSet 中的同一列.该示例演示如何处理BindingCo ...
- yum插件yum-fastestmirror
yum多个mirror自动选择速度最快的mirror,yum-fastestmirror插件,它会自动选择最快的mirror 配置文件: /etc/yum/pluginconf.d/fastestmi ...
- function声明的深刻含义和jquery属性注入区别
在js中有两类对象 1.json对象,仅仅代表对象而已 2.function声明的对象 (1) 它定义了构造器 可以用new 对象 来初始化 数据对象 (2) 它指明对象是一个函数对象 通过后面加 ...
- jquery 事件小事例
用户名变灰 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> & ...
- poj1611---The Suspects
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 19754 Accepted: 9576 Des ...
- 【LeetCode】32. Longest Valid Parentheses (2 solutions)
Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...