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. 利用canvas阴影功能与双线技巧绘制轨道交通大屏项目效果

    利用canvas阴影功能与双线技巧绘制轨道交通大屏项目效果 前言 近日公司接到一个轨道系统的需求,需要将地铁线路及列车实时位置展示在大屏上.既然是大屏项目,那视觉效果当然是第一重点,咱们可以先来看看项 ...

  2. C++ 虚基类的定义、功能、规定

    原文声明:http://blog.sina.com.cn/s/blog_93b45b0f01011pkz.html 虚继承和虚基类的定义是非常的简单的,同时也是非常容易判断一个继承是否是虚继承的,虽然 ...

  3. 第7章 Python类型、类、协议目录

    第7.1节 面向对象程序设计的相关知识 第7.2节 关于面向对象设计的一些思考 第7.3节 Python特色的面向对象设计:协议.多态及鸭子类型 第7.4节 Python中与众不同的类 第7.5节 揭 ...

  4. PyQt(Python+Qt)学习随笔:树型部件QTreeWidget中的项编辑方法editTriggers、editItem和openPersistentEditor作用及对比分析

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 在树型部件QTreeWidget中,有三种方法触发进行项数据的编辑:editTriggers触发编辑 ...

  5. Fiddle重定向请求

    以当当网和淘宝网为例: 1.打开浏览器,在地址栏中输入www.dangdang.com,进入当当主页. 2.在规则编辑器中设置规则,将dangdang重定向至taobao,并打开规则. 3.再次刷新当 ...

  6. jarvisoj flag在管理员手上

    jarvisoj flag在管理员手上 涉及知识点: (1)代码审计和cookie注入 (2)哈希长度拓展攻击 解析: 进入题目的界面.看到 那么就是想方设法的变成admin了.挂上御剑开始审计.发现 ...

  7. Python接口测试-使用requests模块发送post请求

    本篇主要记录下使用python的requests模块发送post请求的实现代码. #coding=utf-8 import unittest import requests class PostTes ...

  8. 安装nginx并安全地配置和启动

    一.安装nginx >>参考文章<< 安装教程,看代码&注释 # .sh # 如果centos服务器是最低安装,则先安装weget yum install -y wge ...

  9. go学习的第7天

    不容易啊,坚持7天了呢,今天开始看视频学习 https://www.bilibili.com/video/BV1pt41127FZ?from=search&seid=4441824587572 ...

  10. starsWidth 和endWidth ie不兼容方案

    if (typeof String.prototype.startsWith != 'function') { String.prototype.startsWith = function (pref ...