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 ...
随机推荐
- 小白从零开始阿里云部署react项目+node服务接口(一:阿里云服务器)
准备阿里云服务器,并安装系统 如果没用自己服务器可以购买一个 https://www.aliyun.com/minisite/goods?userCode=x7i5glgc 初级购买一个1核2G的主机 ...
- java大数据最全课程学习笔记(3)--HDFS 简介及操作
目前CSDN,博客园,简书同步发表中,更多精彩欢迎访问我的gitee pages 目录 HDFS 简介及操作 HDFS概述 HDFS产出背景及定义 HDFS优缺点 HDFS组成架构 HDFS文件块大小 ...
- two types of friend
两类朋友 第一类,普通朋友,并不能分享一些隐私的感情,只能说一些事情,有一些只是认识的人或者虽然认识很多年但是也只能是这样的! 第二类,关心你,可以交流感清,明显更加亲密一点. 不要对第一类朋友说第二 ...
- Presto性能调优的五大技巧
概述 Presto架构 Presto是一个分布式的查询引擎,本身并不存储数据,但是可以接入多种数据源,并且支持跨数据源的级联查询. Presto的架构分为: Coodinator:解析SQL语句,生成 ...
- JMS微服务开发示例(一)Hello world
网关部署 1.在网关服务器上,安装.net core 3.1运行环境: 2.到 https://www.cnblogs.com/IWings/p/13354541.html 下载Gateway.zip ...
- spring学习(三)属性注入
用的是IDEA的maven工程,pom.xml文件导包依赖省略 本文主要写set方式注入 (一).一般类型注入 一.写两个实体类Car.User public class Car { private ...
- BUUCTF-web web1 (无列名注入)
注册并登录后发现,sql注入,注入点在广告申请的界面.加单引号发现报错 先通过insert插入数据,然后再通过id查询相应的数据,所以是二次注入. 常见报错函数updatexml,floor以及ext ...
- 曹工说Spring Boot源码(30)-- ConfigurationClassPostProcessor 实在太硬核了,为了了解它,我可能debug了快一天
写在前面的话 相关背景及资源: 曹工说Spring Boot源码(1)-- Bean Definition到底是什么,附spring思维导图分享 曹工说Spring Boot源码(2)-- Bean ...
- python基础--函数全解析(2)
函数的重点知识补充 (1)补充的两个小知识点(global,nonlocal) 1.global的使用 我们在补充这两个知识点之前,我们先看一下下面这个例子: a = 1 def func(): pr ...
- Centos 7下编译安装Apache
(1)下载apr.apr-util.httpd源码包 百度云下载地址:https://pan.baidu.com/s/1HyW_9XTLhhhf5j_IuNCQsQ 提取码:pg4d (2)安装编译工 ...