【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 ...
随机推荐
- Android 自定义 ListView 显示网络上 JSON 格式歌曲列表
本文内容 环境 项目结构 演示自定义 ListView 显示网络上 JSON 歌曲列表 参考资料 本文最开始看的是一个国人翻译的文章,没有源代码可下载,根据文中提供的代码片段,自己新建的项目(比较可恶 ...
- 【DB】MYSQL相关细节
在进行统计API模块测试时候,需要用SQL进行查询,并和API的返回结果进行对比: 而SQL中一些以前用过的细节需要记住: 补充一下show的部分用法: MySQL中有很多的基本命令,show命令也是 ...
- SQL到NoSQL概览性总结之一 数据库应用场景选型
数据库类型与实例 适合场景 不适合场景 场景举例 关系数据库 基于集合理论,具有行和列的二维表,严格使用类型 开源MySQL/MariaDB, PostgreSQL 商业:Oracle,DB2,SQL ...
- vSphere Replication:虚拟机的保护伞
http://server.zdnet.com.cn/server/2013/0401/2151318.shtml ZDNet至顶网服务器频道 04月01日 新闻消息: 保护IT环境的一个基本方面就是 ...
- 写一个function,清除字符串前后的空格。(兼容所有浏览器)
代码: function trim(str) { if (str && typeof str === "string") { return str.replace( ...
- 在vim中安装及配置NERDTree插件
使用Vundle插件安装,在.vimrc中加入以下代码: Plugin 'scrooloose/nerdtree' 打开vim,输入命令如下: :BundleInstall 等待安装完毕 配置NERD ...
- GoldenGate安装与卸载
软件下载 http://www.oracle.com/technetwork/middleware/goldengate/downloads/index.html 安装 卸载(Using Oracle ...
- Java之创建对象>3.Enforce the singleton property with a private constructor or an enum type
1. 通过一个公开的字段来获取单例 // Singleton with public final field public class Elvis { public static final Elv ...
- java获取文件流
CreateTime--2017年9月1日14:49:21 Author:Marydon servlet获取文件流的两种方式 方式一:使用绝对路径(推荐使用) import java.io.Inp ...
- django之创建第9个项目-管理后台admin
django之创建第9个项目-管理后台admin配置 1.配置setting文件INSTALLED_APPS = ( 'django.contrib.auth', 'django.cont ...