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 ...
随机推荐
- 史上最强vue总结~万字长文---面试开发全靠它了
vue框架篇 vue的优点 轻量级框架:只关注视图层,是一个构建数据的视图集合,大小只有几十kb: 简单易学:国人开发,中文文档,不存在语言障碍 ,易于理解和学习: 双向数据绑定:保留了angular ...
- 简易防止U盘中毒
1.将U盘插入电脑,打开u盘 2.在U盘里面新建一个文本文档,将文本文档重命名autorun.inf保存完成. 3.为了防止误删次文件可以将属性设为影藏,就完成了.
- Java数组最大值和最小值
package com.mybatis.demo.exercise; /** * @author yinsenxin */ public class ArrayMaxAndMin { public s ...
- html-webpack-plugin在html中插入数据
html-webpack-plugin在html中插入数据 <!DOCTYPE html> <html> <head> <meta charset=" ...
- maven&nexus_repository 私库搭建与使用
一.nexus仓库安装 1,http://www.sonatype.org/nexus/ 下载sso版本,免费2,tar -zxvf nexus-2.11.1-01-bundle.tar.gz3 ...
- layui 魔改:上传时的真实进度条
这个问题本身不复杂,难点在于需要改 layui 的源码. HTML略. 网页的JS域: layui.use(['upload','element','layer'], function(){ var ...
- scratch编程我的世界3D史蒂夫
这个程序我们只能制作出一个3D模型而已,并不是真正的我的世界整个游戏: 效果很炫酷吧!现在我们就来看看是怎样编程的吧! 首先,这个模型是有无数个平面克隆体摞在一起,旋转后会产生一种立体的错觉,是不是有 ...
- 2.UDP协议
UDP只在IP数据报服务之上增加了很少功能,即复用分用和差错检测功能. 应用层给UDP多长的报文,UDP就照样发送,即一次发送一个完整报文 一.UDP首部格式 这里的长度是指(首部+数据) UDP校验 ...
- @Autowired @Qualifier
spring2.1中允许用户通过@Autowired注解对Bean的属性变量.属性Setter方法以及构造函数进行标注,配合AutowiredAnnotationBeanProcessor完成Bean ...
- ST表解决RMQ问题
RMQ问题: RMQ(Range Minimum/Maximum Query),区间最值查询.对于长度为n的数列A,回答若干询问RMQ(A,i,j)(i,j<=n),返回数列A中下标在i,j之间 ...