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. python 爬取喜马拉雅节目生成RSS Feed

    记录于:2020年12月03日用了N年的手机在经历N次掉落之后终于扛不住了,后背都张嘴了,估计再摔一次电池都能飞出来.换了手机,由于之前有听喜马拉雅的习惯,但是手机里自带有播客软件,强迫症逼着我不能下 ...

  2. 小样本学习最新综述 A Survey on Few-shot Learning | Introduction and Overview

    目录 01 Introduction Bridging this gap between AI and humans is an important direction. FSL can also h ...

  3. BugkuCTF-web-速度要快

    打开题目显示一串文字,应该是提示. 查看源代码 OK ,now you have to post the margin what you find post提交参数margin burp发送后发现响应 ...

  4. charles功能(二)修改response请求参数

    1.接口处 鼠标右击,选择breakpoints(允许本接口使用breakpionts功能) 2.开始设置断点值 4.重新请求接口(charles的界面变为可编辑状态),修改请求参数,执行请求 5.最 ...

  5. PyQt(Python+Qt)学习随笔:树型部件QTreeWidget提供的currentItem当前项操作访问方法

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 当前项是指当前鼠标和键盘焦点所在项,在项可以进行选择操作时,当前项可以是选中状态,也可以是未选中状态 ...

  6. Python学习随笔:获取当前主机名和用户名的方法

    在Python中,要获取当前主机的主机名和登录用户名很简单: 使用os.getlogin():返回当前登录用户名 使用socket.gethostname():返回当前机器主机名 代码如下: > ...

  7. Gitlab勾选Remove Source Branch后本地仍然能够看到该分支

    现象: Gitlab合并Merge Request时,勾选了Remove Source Branch,但是本地仍然能够看到该分支(git branch -a),而远程仓库中该分支已经不存在. 解决: ...

  8. 【题解】「UVA10116」Robot Motion

    Simple Translation 让你模拟一个机器人行走的过程,如果机器人走入了一个循环,输出不是循环的长度和是循环的长度,如果最终走出来了,输出走的步数. Solution 直接模拟即可,本题难 ...

  9. OI学习过程记录

    这帖子本来是教练为了给低年级学生分享经验而让我写的学习经历,不过等我退役之后可能就变成回忆录了. 初三 WC 前:上了正睿的线上课程,练了一些模拟赛,同时也正在学文化课. 然后,莫名奇妙1膜考了全校前 ...

  10. 笔记-Recursive Queries

    Recursive Queries \[m_{l,r}=\textrm{id}(\max_{i=l}^r a_i)\\ f(l,r)= \begin{cases} (r-l+1)+f(l,m_{l,r ...