【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 ...
随机推荐
- 使用jstl报http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar错误
今天创建了一个maven项目,想使用jstl报http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the ...
- loadscript加载
function load_script(xyUrl, callback){ var head = document.getElementsByTagName('head')[0]; var scri ...
- Struts2(九)OGNL标签一与Struts2标签
一.什么是OGNL Object Graph Navigation Language对象图导航语言. 是Struts2默认的表达式语言,开源,功能更强大.和EL表达式有点相似 存取对象的属性,调用对 ...
- gson 忽略掉某些字段不进行转换
增加 transient 修饰进行解决,例如: private transient final DecimalFormat df = new DecimalFormat("#0.00&qu ...
- Spring的PropertyPlaceholderConfigurer事例应用
在开发的过程中,经常发现一些类似:${log4j.level}之类的内容,后来才知道原因.下面解释一下: 1.PropertyPlaceholderConfigurer是个bean工厂后置处理器的实现 ...
- psql 查询表大小
select schemaname,tablename,pg_relation_size(schemaname||'.'||tablename) as tabsize from pg_tables o ...
- 【DB2】数据库的事务日志已满。SQLSTATE=57011
问题描述 在使用数据库的时候报错如上图,我们先使用db2 get db cfg for sample查看相关配置参数,其中sample为数据库名称 C:\Users\Thinkpad>db2 g ...
- javascript库之Mustache库使用说明
一.简单示例 代码: function show(t) { $("#content").html(t); } var view = { title: 'YZF', cacl: fu ...
- SpringBoot配置RestTemplate的代理和超时时间
application.properties: #代理设置 proxy.enabled=false proxy.host=192.168.18.233 proxy.port=8888 #REST超时配 ...
- CentOS7统计某个进程当前的线程数
方式一: cat /proc/[pid]/status 展示结果中,Threads后边对应的数字就是进程拥有的线程数量 方式二: |wc -l