import java.util.Arrays;

/**
* Created by lvhao on 2017/7/6.
* 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 ]
]
54题螺旋取数的逆过程,生成螺旋数组,思路是一样的
*/
public class Q59SpiralMatrix2 {
public static void main(String[] args) {
for (int[] num : generateMatrix(5))
System.out.println(Arrays.toString(num));
}
public static int[][] generateMatrix(int n) {
int[][] res = new int[n][n];
//特殊情况
if (n == 0)
return res;
int[] nums = new int[n*n];
//设置cur记录当前取到哪个数了
int cur = 0;
//生成原始数据
for (int i = 1; i <= (n*n); i++) {
nums[i-1] = i;
}
//外循环是层数
for (int i = 0; i < n / 2; i++) {
//里边四个循环分别生成上右下左四边,记得每次取完数cur+1
//上边
for (int j = 0; j < n - (i * 2) - 1; j++) {
res[i][i+j] = nums[cur];
cur++;
}
//右边
for (int j = 0; j < n - (i * 2) - 1; j++) {
res[i+j][n-i-1] = nums[cur];
cur++;
}
//下边
for (int j = 0; j < n - (i * 2) - 1; j++) {
res[n-1-i][n-1-i-j] = nums[cur];
cur++;
}
//左边
for (int j = 0; j < n - (i * 2) - 1; j++) {
res[n-1-i-j][i] = nums[cur];
cur++;
}
}
//n是奇数时最后一个数循环不到,单独考虑
if (n%2 != 0)
{
res[n/2][n/2] = nums[n*n-1];
}
return res;
}
}

[leetcode]54. Spiral Matrix2生成螺旋数组的更多相关文章

  1. [leetcode]54. Spiral Matrix二维数组螺旋取数

    import java.util.ArrayList; import java.util.List; /** * Given a matrix of m x n elements (m rows, n ...

  2. 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 ...

  3. [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 ...

  4. Leetcode 54:Spiral Matrix 螺旋矩阵

    54:Spiral Matrix 螺旋矩阵 Given a matrix of m x n elements (m rows, n columns), return all elements of t ...

  5. leetCode 54.Spiral Matrix(螺旋矩阵) 解题思路和方法

    Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...

  6. leetcode 54. Spiral Matrix 、59. Spiral Matrix II

    54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...

  7. LeetCode - 54. Spiral Matrix

    54. Spiral Matrix Problem's Link ------------------------------------------------------------------- ...

  8. Leetcode 54. Spiral Matrix & 59. Spiral Matrix II

    54. Spiral Matrix [Medium] Description Given a matrix of m x n elements (m rows, n columns), return ...

  9. 【LeetCode每天一题】Spiral Matrix II(螺旋数组II)

    Given a positive integer n, generate a square matrix filled with elements from 1 to n2 in spiral ord ...

随机推荐

  1. SQL优化之SQL 进阶技巧(下)

    上文( SQL优化之SQL 进阶技巧(上) )我们简述了 SQL 的一些进阶技巧,一些朋友觉得不过瘾,我们继续来下篇,再送你 10 个技巧 一. 使用延迟查询优化 limit [offset], [r ...

  2. Cys_Control(五) MMenu

    一.查看Menu原样式 1.通过Blend查看Menu原有样式 Menu的原有样式结构较为简单,由边框Border及集合控件 ItemsPresenter 组成,原有样式如下 <Style x: ...

  3. lcm的和(莫比乌斯反演)

    马上开学了,加一个操作系统和数据库标签 不玩了,求1-n和1-m的lcm(i,j)和 首先想到把lcm(i,j)转化为i * j / gcd(i, j) 然后gcd,要素察觉,开始枚举d使得gcd(i ...

  4. 大数据-redis-redis启动出错

    redis启动出错Creating Server TCP listening socket 127.0.0.1:6379: bind: No error 解决方法(1) 首先如果你是从官方redis官 ...

  5. 第10.1节 Python的模块及模块导入

    一. 什么是模块 Python中的模块即单个的Python代码文件,为什么称为模块呢?这是因为在Python中,每个独立的Python文件都可以作为被其他代码导入的模块使用,导入的模块有自己的名字空间 ...

  6. PyQt及PyCharm学习中遇到的问题

    在PyQt及PyCharm学习过程中,老猿遇到了如下问题: 问题: 刚安装的PyCharm执行代码报"ModuleNotFoundError: No module named XXXX&qu ...

  7. jarvisoj babyphp

    jarvisoj babyphp 涉及知识点: (1)GitHack处理.git源码泄露 (2)php代码注入 解析: 进入题目界面. 看到题目中的用了git那么第一反应肯定是可能存在.git源码泄露 ...

  8. sails框架结合mocha

    sails框架(testing&model and orm): http://sailsjs.org/documentation/concepts/testing orm(对象关系映射): h ...

  9. Day3 【Scrum 冲刺博客】

    每日会议总结 昨天已完成的工作 方晓莹(PIPIYing) 开始人员管理页 搭建与后台对接的相关配置 方子茵(Laa-L) 完成车辆查询接口 黄芯悦(Sheaxx) 完善社区通知页面 完善社区活动页面 ...

  10. 彻底搞懂js __proto__ prototype constructor

    在开始之前,必须要知道的是:对象具有__proto__.constructor(函数也是对象固也具有以上)属性,而函数独有prototype 在博客园看到一张图分析到位很彻底,这里共享: 刚开始看这图 ...