【LeetCode-面试算法经典-Java实现】【059-Spiral Matrix II(螺旋矩阵II)】
【059-Spiral Matrix II(螺旋矩阵II)】
【LeetCode-面试算法经典-Java实现】【全部题目文件夹索引】
原题
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 ]
]
题目大意
给定一个整数n。生成一个n*n的矩阵,用1-n^2的数字进行螺旋填充。
解题思路
採用计算生成法,对每个位置计算相应的数。
代码实现
算法实现类
public class Solution {
public int[][] generateMatrix(int n) {
int[][] result = new int[n][n];
int layer;
int k;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
layer = layer(i, j, n); // 当前坐标外有几层
// n * n - layer * layer外围层使用的最后一个数字(也是最大的)
// 坐标所在的当前层使用的第一个数字
k = n * n - (n - 2 * layer) * (n - 2 * layer) + 1;
result[i][j] = k;
// (n - 2 * layer - 1):四个(n - 2 * layer - 1)就是(x,y)坐标所在层的全部元素个数
if (i == layer) { // 情况一、坐标离上边界近期
result[i][j] = k + j - layer;
} else if (j == n - layer - 1) { // 情况二、坐标离右边界近期
result[i][j] = k + (n - 2 * layer - 1) + i - layer;
} else if (i == n - layer - 1) { // 情况三、坐标离下边界近期
result[i][j] = k + 3 * (n - 2 * layer - 1) - (j - layer);
} else { // 情况三、坐标离左边界近期
result[i][j] = k + 4 * (n - 2 * layer - 1) - (i - layer);
}
}
}
return result;
}
/**
* 在一个n*n的矩阵中,计算(x,y)坐标外有多少层,坐标从0開始计算
*
* @param x 横坐标
* @param y 纵坐标
* @param n 矩阵大小
* @return 坐标外的层数
*/
public int layer(int x, int y, int n) {
x = x < n - 1 - x ?
x : n - 1 - x; // 计算横坐标离上下边界的近期距离
y = y < n - 1 - y ? y : n - 1 - y; // 计算纵坐标离左右边界的近期距离
return x < y ? x : y; // 较小的值为坐标的外围层数
}
}
评測结果
点击图片,鼠标不释放,拖动一段位置,释放后在新的窗体中查看完整图片。
特别说明
欢迎转载。转载请注明出处【http://blog.csdn.net/derrantcm/article/details/47164439】
【LeetCode-面试算法经典-Java实现】【059-Spiral Matrix II(螺旋矩阵II)】的更多相关文章
- LeetCode 54. Spiral Matrix(螺旋矩阵)
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- [LeetCode] 59. Spiral Matrix II 螺旋矩阵 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 螺旋矩阵之三
On a 2 dimensional grid with R rows and C columns, we start at (r0, c0) facing east. Here, the north ...
- PAT 1105 Spiral Matrix[模拟][螺旋矩阵][难]
1105 Spiral Matrix(25 分) This time your job is to fill a sequence of N positive integers into a spir ...
- LeetCode OJ:Spiral MatrixII(螺旋矩阵II)
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- LeetCode OJ:Spiral Matrix(螺旋矩阵)
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- 【LeetCode-面试算法经典-Java实现】【139-Word Break(单词拆分)】
[139-Word Break(单词拆分)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a string s and a dictionary of w ...
- 【LeetCode-面试算法经典-Java实现】【053-Maximum Subarray(最大子数组和)】
[053-Maximum Subarray(最大子数组和)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Find the contiguous subarray w ...
- 【LeetCode-面试算法经典-Java实现】【062-Unique Paths(唯一路径)】
[062-Unique Paths(唯一路径)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 A robot is located at the top-left c ...
随机推荐
- JavaScript 继承——三种继承方法及其优劣
原文地址 本文内容 目的 继承的第一步--最简单的继承 私有变量/成员和原型 三种继承方式及其优劣 基本的原型继承 Yahoo JavaScript 模块模式 创建闭包的构造函数 三种方法的代码执 ...
- 基于webview的选择滑动控件(PC和wap版)
有了webview,大家开发ios或者安卓的app就方便很多啦. 第一可以增量更新: 第二webview可以同时兼容ios和安卓,减少开发量哦. --------------------------- ...
- PAT《数据结构学习与实验指导》实验项目集 2-05 2-06 2-07 2-08
题目地址:here pat 2-05 求集合数据的均方差 没什么可说的,大水题 #include<cstdio> #include<cmath> int main() { in ...
- 使用Gulp
为什么要使用Gulp 在前端开发中通常须要做,预处理语言的编译.js文件的压缩.css文件的压缩.图片的压缩等一系列工作,而使用Gulp能够自己主动化的完毕这些工作,从而提高站点的开发效率,在我的博客 ...
- Vim 中如何去掉 ^M 字符
基于 DOS/Windows 的文本文件在每一行末尾有一个 CR(回车)和 LF(换行),而 UNIX 文本只有一个换行,即win每行结尾为\r\n,而linux只有一个\n如果win下的文档上传到l ...
- JSjs获取当前时间的前一天/后一天(昨天/明天)
Date curDate = new Date(); var preDate = new Date(curDate.getTime() - 24*60*60*1000); //前一天 var next ...
- 数组的翻转(非reverse())
方法一: var arr = [1,2,3,4]; var arr2 = []; while(arr.length) { var num = arr.pop(); //删除数组最后一个元素并返回被删除 ...
- Android学习笔记十:异步处理
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/7520700.html 一:基础概念 UI线程:当Android程序第一次启动时,Android会同时启动一条主 ...
- java new关键字
//new关键字://1.表示创建一个对象//2.表示实例化对象//3.表示申请内存空间 在python中其实就是一个实例化的过程
- Eclipse和MyEclipse使用技巧--Eclipse各版本介绍
进入eclipse的下载官网 http://www.eclipse.org/downloads/ 发现,会有多种版本提供下载. 对于刚接触Java开发的初学者,在下载eclipse时,对官网上面提 ...