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 ...
随机推荐
- 日志套餐篇 - log4j2 logback全量套餐
日志套餐篇 - log4j2 logback全量套餐 前情提要: Log4j Log4j2 logback是当下主流的日志框架 slf4j则是新一代的日志框架接口,logback直接实现了slf4j接 ...
- [jvm] -- 判断对象是否死亡篇
判断对象是否死亡的两种方法 引用计数法 给对象中添加一个引用计数器,每当有一个地方引用它,计数器就加 1:当引用失效,计数器就减 1:任何时候计数器为 0 的对象就是不可能再被使用的. 优点: 简单 ...
- Centos 7 静态IP设置
1.编辑 ifcfg-eth0 文件,vim 最小化安装时没有被安装,需要自行安装不描述. # vim /etc/sysconfig/network-scripts/ifcfg-eth0 2.修改如下 ...
- Java程序斗地主发牌代码,List、Map集合的应用
Java集合存储的灵活运用List集合存储 54个编号 Map <key,value> key 对应的是编号 , value 是 牌的花色(红方梅黑)+ 具体的一张牌 ,比如 黑桃2 用2 ...
- Python环境那点儿事(MAC篇)
Python环境那点儿事(MAC篇) 解释器版本选择:(Python是解释型语言,相应的选择的就是解释器) 前言: 不管你是什么原因翻看此篇文章,强行安利一篇< 2018 Python官方年度报 ...
- layui常用插件(二) 时间插件
日期和时间 html <div class="layui-inline"> <!-- 注意:这一层元素并不是必须的 --> <input type=& ...
- [转]jquery如何判断checkbox(复选框)是否被选中,至少被选中一个
谁都知道 在html 如果一个复选框被选中 是 checked="checked". 但是我们如果用jquery alert($("#id").attr(&qu ...
- Python删除元组
Python删除元组: 删除元组中的某一个元素: # 删除元组中的元素 tuple_1 = ('a','b','c','d','e') # 删除第 2 个元素 tuple_1 = tuple_1[:1 ...
- 什么是 PHP 过滤器?
PHP 过滤器 PHP 过滤器用于验证和过滤来自非安全来源的数据,比如用户的输入. 什么是 PHP 过滤器? PHP 过滤器用于验证和过滤来自非安全来源的数据. 测试.验证和过滤用户输入或自定义数据是 ...
- PHP array_push() 函数
实例 向数组尾部插入 "blue" 和 "yellow": <?php$a=array("red","green" ...