【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 ...
随机推荐
- "___gxx_personality_v0", referenced from:
这是因为里面有用到C++ 的一些东西.所以会出现这个问题 两种解决办法. 第一种.TARGETS -> Build Phases -> Link Binary With Libraries ...
- asp.net 定时执行任务代码 定时采集数据
using System; using System.Data; using System.Configuration; using System.Collections; using System. ...
- 【原】【BG】-一次虚拟化环境实践简要记录
部分涉及到Linux.Nginx.tomcat.MySQL等的点滴操作记录,时间长了,就忘掉了,偶尔整理一下操作的history,就此简要备份一下: [原][BG]-一次虚拟化环境实践简要记录: ht ...
- 动态加载jar包(二)
上次说的加载jar包,有几个问题没有解决: 1.如果项目包含了其他的jar包如何解决? 2.如何规范上传的jar包的类和方法? 下面就解决一下上面两个问题 一.首先编写被调用的类,这次使用maven工 ...
- Hibernate缓存研究
1. 什么是缓存? 数据库的缓存指的是应用程序和物理数据源之间的数据.即把物理数据源的数据复制到缓存.有了缓存,可以降低应用程序对物理数据源的访问频率,从而提高效率.缓存的介质一般是内存,也可以是硬盘 ...
- 【Java多线程】线程同步方法概览
一:使用syncrhoized内置锁实现同步 使用互斥来实现线程间的同步,保证共享数据在同一时刻只被一个线程使用.Java中最基本的互斥手段就是syncrhoized关键字. syncrhoized的 ...
- rotate-list 旋转部分链表
Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given1- ...
- 包管理和环境管理软件Anaconda
可以用于配置linux下得虚拟环境,该软件可以针对不同服务配置不同的运行环境,方便包管理与环境管理
- KMS11激活Window系列
运行状态图 download: kms11
- 管理多tomcat实例的shell脚本
为了简化tomcat的部署复杂度, 把以前单独配置的脚本提出来做了一个带参数的, 可以用于单机环境下的多tomcat实例管理. 其中deploy功能, 会将指定的模块war按日期时间更名备份后, 再部 ...