【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 ...
随机推荐
- SuperMap入门3——Hello World
Hello World程序很重要,对于入门来说,它可以检测我们的环境.配置是否正确,感受程序的易用性等. 添加工具 由于我是使用的VS2017+ SuperMap iObject绿色免安装版,所以新建 ...
- vsphere产品下载列表
https://my.vmware.com/cn/web/vmware/info/slug/datacenter_cloud_infrastructure/vmware_vsphere_with_op ...
- Linux--U盘安装Ubuntu12.04[转]
http://www.cnblogs.com/plokmju/p/linux_installubuntu.html 最近一直在研究Android内核驱动开发的相关事宜,使用VMware虚拟机虽然可以更 ...
- HttpLuaModule——翻译(Nginx API for Lua)
现在我已经将翻译的内容放到:http://wiki.nginx.org/HttpLuaModuleZh Nginx API for Lua Introduction 各种各样的*_by_lua和*_b ...
- HDU 3001 Travelling (三进制状态压缩 DP)
题意:有 n 个city,能够选择任一城市作为起点,每一个城市不能訪问超过2次, 城市之间有权值,问訪问所有n个城市须要的最小权值. 思路:由于每一个城市能够訪问最多两次,所以用三进制表示訪问的状态. ...
- 〖Linux〗使用纯命令行来操作VBOX(宿主机不需要X11 Server)
1. Linux安装vbox,略过 2. 查看已安装扩展插件 VBoxManage list extpacks 3. 创建一个vm: VBoxManage createvm --name " ...
- 〖Linux〗Ubuntu14.04安装32位运行库
在终端操作: sudo dpkg --add-architecture i386 echo "deb http://old-releases.ubuntu.com/ubuntu/ rarin ...
- 6、java5线程池之固定大小线程池newFixedThreadPool
JDK文档说明: 创建一个可重用固定线程数的线程池,以共享的无界队列方式来运行这些线程.在任意点,在大多数 nThreads 线程会处于处理任务的活动状态.如果在所有线程处于活动状态时提交附加任务,则 ...
- java开发微信模板消息推送
发布时间:2018-12-12 技术:springboot+maven 概述 该demo主要涉及微信模板消息推送功能, 详细 代码下载:http://www.demodashi.com/dem ...
- windows DLL中使用COM的注意事项
windows的DLL开发是有需要注意的地方的,不然会造成一些问题.由于我最近的使用不当,又造成了问题,虽然之前有写过一篇笔记, http://www.cnblogs.com/foohack/p/66 ...