[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 ...
随机推荐
- eclipse 老坑巨滑之内存溢出OOM
绪:今天接手一个古老项目,tomcat6+jdk6.被 java.lang.OutOfMemoryError: PermGen space 啪啪打脸, 网上确实有很多解决方法,主要有三种类型:一 ...
- C++里面this关键字的用法和功能
1.this指针的用处 一个对象的this指针并不是对象本身的一部分,不会影响sizeof(对象)的结果.this作用域是在类内部,当在类的非静态成员函数中访问类的非静态成员的时候,编译器会自动将对象 ...
- fist-第六天冲刺随笔
这个作业属于哪个课程 https://edu.cnblogs.com/campus/fzzcxy/2018SE1 这个作业要求在哪里 https://edu.cnblogs.com/campus/fz ...
- Vue+EasyPOI导出Excel(带图片)
一.前言 平时的工作中,Excel 导入导出功能是非常常见的功能,无论是前端 Vue (js-xlsx) 还是 后端 Java (POI),如果让大家手动编码实现的话,恐怕就很麻烦了,尤其是一些定制化 ...
- 无需付费,教你IDEA社区版中开发Web项目(SpringBoot\Tomcat)
1.IDEA 版本介绍 最近有小伙伴私信我说 IDEA 破解怎么总是失效?难道就没有使用长一点的吗... 咳咳,除了给我留言「激活码」外,或许社区版可能完全满足你的需求. 相信有挺多小伙伴可能不清楚或 ...
- 第11.16节 Python正则元字符“()”(小括号)与组(group)匹配模式
一. 什么是组 关于组匹配模式,Python官网上说得比较简单,也没有这个名词,只有组这个名词,老猿查了比较多的资料和做了相关测试之后才理解. 组匹配模式,就是在匹配的正则表达式中使用小括号" ...
- 项目测试环境自动化部署[jenkins前后端配置、Nginx配置]
持续部署:关注点在于项目功能部署到服务器后可以正常运行,为下一步测试环节或最终用户正式使用做准备.(问题点:一个环节有问题,其他环节跟着有问题) 持续集成:关注点是在于尽早发现项目整体运行问题,尽早解 ...
- C# 继承类的值赋
C# 继承类的值赋 /// <summary> /// 将父类的值赋值到子类中 /// </summary> /// <typeparam name="TPar ...
- 20分钟带你掌握JavaScript Promise和 Async/Await
转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具.解决方案和服务,赋能开发者. 原文出处:https://www.freecodecamp.org/news/learn-promise-a ...
- redis学习之——redis.conf配置(基本)文件学习
# Redis configuration file example # Note on units: when memory size is needed, it is possible to sp ...