C#LeetCode刷题之#59-螺旋矩阵 II(Spiral Matrix II)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3678 访问。
给定一个正整数 n,生成一个包含 1 到 n2n^2n2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵。
输入: 3
输出: [
[ 1, 2, 3 ],
[ 8, 9, 4 ],
[ 7, 6, 5 ]
]
Given a positive integer n, generate a square matrix filled with elements from 1 to n2n^2n2 in spiral order.
Input: 3
Output: [
[ 1, 2, 3 ],
[ 8, 9, 4 ],
[ 7, 6, 5 ]
]
示例
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3678 访问。
public class Program {
public static void Main(string[] args) {
var n = 3;
var res = GenerateMatrix(n);
ShowArray(res);
Console.ReadKey();
}
private static void ShowArray(int[,] matrix) {
for(var i = 0; i < matrix.GetLength(0); i++) {
for(var j = 0; j < matrix.GetLength(1); j++) {
Console.Write($"{matrix[i, j]} ");
}
Console.WriteLine();
}
}
public static int[,] GenerateMatrix(int n) {
var list = new List<int>();
var max = (int)Math.Ceiling(n / 2d);
var matrix = new int[n, n];
var count = 0;
Spiral(matrix, 0, n, n, max, ref count);
return matrix;
}
private static void Spiral(int[,] matrix,
int level,
int m,
int n,
int max,
ref int count) {
if(level >= max) return;
//顶端
for(var j = level; j < n - level; j++) {
matrix[level, j] = ++count;
}
//右端
for(var i = level + 1; i < m - level - 1; i++) {
matrix[i, n - level - 1] = ++count;
}
//底端
if(level != m - level - 1) {
for(var j = n - level - 1; j >= level; j--) {
matrix[m - level - 1, j] = ++count;
}
}
//左端
if(level != n - level - 1) {
for(var i = m - level - 2; i >= level + 1; i--) {
matrix[i, level] = ++count;
}
}
Spiral(matrix, ++level, m, n, max, ref count);
}
}
以上给出1种算法实现,以下是这个案例的输出结果:
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3678 访问。
1 2 3
8 9 4
7 6 5
分析
显而易见, 以上算法的时间复杂度为:O(n2)O(n^2)O(n2) 。
C#LeetCode刷题之#59-螺旋矩阵 II(Spiral Matrix II)的更多相关文章
- C#LeetCode刷题之#54-螺旋矩阵(Spiral Matrix)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3672 访问. 给定一个包含 m x n 个元素的矩阵(m 行, ...
- C#LeetCode刷题之#867-转置矩阵(Transpose Matrix)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3756 访问. 给定一个矩阵 A, 返回 A 的转置矩阵. 矩阵的 ...
- leetcode刷题记录——数组与矩阵
@ 目录 283. 移动零 566. 重塑矩阵 485. 最大连续1的个数 240. 搜索二维矩阵 II 378. 有序矩阵中第K小的元素 645. 错误的集合 287. 寻找重复数 667. 优美的 ...
- C#LeetCode刷题之#566-重塑矩阵( Reshape the Matrix)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3720 访问. 在MATLAB中,有一个非常有用的函数 resha ...
- LeetCode 54. 螺旋矩阵(Spiral Matrix) 剑指offer-顺时针打印矩阵
题目描述 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, ...
- 【leetcode刷题笔记】Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- 【leetcode刷题笔记】Search in Rotated Sorted Array II
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- 【leetcode刷题笔记】Remove Duplicates from Sorted Array II
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- 【leetcode刷题笔记】Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
随机推荐
- commons-fileload图片文件上传工具 , servlet文件图片上传案列
本案列是java maven工程小项目,提供个大家学习! 1.在pom.xml文件中导入依赖: <!--文件上传依赖--><dependency> <groupId&g ...
- Ethical Hacking - GAINING ACCESS(9)
Server Side Attack Nexpose - Analysing Scan Results and Generating Reports OS and Software Inforatio ...
- 机房vscode使用方法
问题 众所周知,机房中的电脑有一个win7系统,(非常的好,摆脱linux了),同时win7上有一个 vscode ,更好了. 但是!vscode 由于老师不允许联网,导致插件无法安装,更为恶心的事, ...
- ciscn_2019_c_1
0x01 检查文件,64位 检查开启的保护情况 开启了NX保护 0x02 IDA静态分析 在主函数这里并没有常见的gets栈溢出,尝试再这里面的子函数找找,发现了encrypt函数,进去查看 发现这个 ...
- Java集合框架1-- HashMap
HashMap的知识点可以说在面试中经常被问到,是Java中比较常见的一种数据结构.所以这一篇就通过源码来深入理解下HashMap. 1 HashMap的底层是如何实现的?(基于JDK8) 1.1 H ...
- vue的双向数据绑定实现原理(简单)
如果有人问你,学vue学到了什么,那双向数据绑定,是必然要说的. 我们都知道,在vue中,使用数据双向绑定我们都知道是v-modle实现的. 实现原理是通过Object.defineProperty的 ...
- java判断当前系统是win还是linux
private static final boolean isWin = System.getProperty("os.name").toLowerCase().contains( ...
- 关于页面布局中,如何让一个div水平和垂直居中的五个方案
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 郭的手机出现提示:条码扫描器,抱歉,Android相机出现问题。您可能需要重启设备
郭的手机出现提示:条码扫描器,抱歉,Android相机出现问题.您可能需要重启设备 ++++++++++++++++++ 原因是系统没有给应用使用摄像头的权限,我到楼下设置对方手机,选择“设置”-&g ...
- JS中Math.random()的使用和扩展
Math.random()方法返回大于等于 0 小于 1 的一个随机数.对于某些站点来说,这个方法非常实用,因为可以利用它来随机显示一些名人名言和新闻事件. 在连续整数中取得一个随机数 值 = Mat ...