[leetcode]54. Spiral Matrix2生成螺旋数组
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生成螺旋数组的更多相关文章
- [leetcode]54. Spiral Matrix二维数组螺旋取数
import java.util.ArrayList; import java.util.List; /** * Given a matrix of m x n elements (m rows, n ...
- 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 54:Spiral Matrix 螺旋矩阵
54:Spiral Matrix 螺旋矩阵 Given a matrix of m x n elements (m rows, n columns), return all elements of t ...
- leetCode 54.Spiral Matrix(螺旋矩阵) 解题思路和方法
Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...
- leetcode 54. Spiral Matrix 、59. Spiral Matrix II
54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...
- LeetCode - 54. Spiral Matrix
54. Spiral Matrix Problem's Link ------------------------------------------------------------------- ...
- 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 ...
- 【LeetCode每天一题】Spiral Matrix II(螺旋数组II)
Given a positive integer n, generate a square matrix filled with elements from 1 to n2 in spiral ord ...
随机推荐
- linux系统下oracle表空间占用情况
1.我们先查询表空间的占用情况,使用sql如下: select upper(f.tablespace_name) "表空间名", d.tot_grootte_mb "表空 ...
- Java中的Set对象去重
前言部分 Set<T> 去重相信大家一定不陌生,尤其是在 Set<String>.Set<Integer> 等等,但是在使用 Set<实体> ,在不重写 ...
- Python学习随笔:使用xlwings设置和操作excel多行多列数据以及设置数据字体颜色填充色对齐方式的方法
☞ ░ 前往老猿Python博文目录 ░ 在前面老猿的文章中,<Python学习随笔:使用xlwings读取和操作Excel文件>.<Python学习随笔:使用xlwings读取和操 ...
- 第6.6节 Python动态执行小结
一. Python动态执行支持通过输入数据流或文件传入Python源代码串,进行编译后执行,可以通过这种方式扩展Python程序的功能: 二. 动态执行方法可能导致恶意攻击,因此使用时需要 ...
- PyQt(Python+Qt)学习随笔:QTreeWidget中获取可见项视口位置矩形的visualItemRect方法
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 树型部件的visualItemRect方法可以返回参数指定项在视口的位置矩形. QRect visu ...
- 记账本APP小升级
增加了显示当月总收入和总支出的功能,增加了选择收支类型的功能,删去了删除账目后恢复的功能. 1.数据库的升级 1.entity 添加了一个收支类型的字段: package com.example.ca ...
- JAVA课堂作业(2019.10.21)
1. 代码: package class20191021; class Grandparent { public Grandparent() { System.out.println("Gr ...
- Fisco Bcos学习资料连接
大牛博客:http://m.blog.csdn.net/sportshark FISCO BCOS学习资料索引;http://kb.bsnbase.com/webdoc/view/Pub4028813 ...
- 【UnitTest】使用方法
UnitTest安装 pip install unittest 详细教程: https://www.cnblogs.com/yufeihlf/p/5707929.html断言: DDT数据驱动: c ...
- 来感受Linux命令行的“真香定律”
Shell看起来只是一个黑黑的命令框,刚开始接触会觉得很丑,毕竟与Win/Mac的华丽界面比起来,命令行终端直接可以丑拒了.但是,实际上它的功能要强大得多,毕竟Linux一开始就是广泛应用于服务器,通 ...