【LeetCode每天一题】Spiral Matrix II(螺旋数组II)
Given a positive integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.
Example:
Input: 3
Output:
[
[ 1, 2, 3 ],
[ 8, 9, 4 ],
[ 7, 6, 5 ]
]
思路
之前做过一个旋转数组的题,当时是旋转打印矩阵,这次只是构造一个旋转数组。方案还是一样的,条件变量也一样。时间复杂度为O(n*n), 空间复杂度为O(n)。
解决代码
def generateMatrix(self, n):
"""
:type n: int
:rtype: List[List[int]]
"""
if n == 0 or n == 1:
return 0 if n == 0 else [[1]]
nums = 1
up, down, left, right = 0, n-1, 0, n-1 # 上下左右变量
res = []
for i in range(n): # 结果矩阵
res.append([0]*n) while left < right: # 循环条件,也可以是 up< down。 因为是n*n矩阵,所以可以这样。
for i in range(left, right):
res[up][i] = nums
nums+= 1 for i in range(up, down):
res[i][right] = nums
nums += 1 for i in range(right, left, -1):
res[down][i] = nums
nums += 1 for i in range(down, up, -1):
res[i][left] = nums
nums += 1
up, down, left, right = up+1, down-1, left+1, right-1 if n % 2: # 如果是奇数的话,中心还有一个元素。
res[left][right] = nums
return res
【LeetCode每天一题】Spiral Matrix II(螺旋数组II)的更多相关文章
- [LeetCode每日一题]81. 搜索旋转排序数组 II
[LeetCode每日一题]81. 搜索旋转排序数组 II 问题 已知存在一个按非降序排列的整数数组 nums ,数组中的值不必互不相同. 在传递给函数之前,nums 在预先未知的某个下标 k(0 & ...
- LeetCode(59)SPiral Matrix II
题目 Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. F ...
- 【js】Leetcode每日一题-解码异或后数组
[js]Leetcode每日一题-解码异或后数组 [题目描述] 未知 整数数组 arr 由 n 个非负整数组成. 经编码后变为长度为 n - 1 的另一个整数数组 encoded ,其中 encode ...
- [LeetCode每日一题]153.寻找旋转排序数组中的最小值
[LeetCode每日一题]153.寻找旋转排序数组中的最小值 问题 已知一个长度为 n 的数组,预先按照升序排列,经由 1 到 n 次 旋转 后,得到输入数组.例如,原数组 nums = [0,1, ...
- 【python】【补】Leetcode每日一题-合并两个有序数组
[python]Leetcode每日一题-合并两个有序数组 [题目描述] 给你两个有序整数数组 nums1 和 nums2,请你将 nums2 合并到 nums1 中,使 nums1 成为一个有序数组 ...
- [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] 885. Spiral Matrix III 螺旋矩阵之三
On a 2 dimensional grid with R rows and C columns, we start at (r0, c0) facing east. Here, the north ...
- [leetcode]54. Spiral Matrix2生成螺旋数组
import java.util.Arrays; /** * Created by lvhao on 2017/7/6. * Given an integer n, generate a square ...
- PAT 1105 Spiral Matrix[模拟][螺旋矩阵][难]
1105 Spiral Matrix(25 分) This time your job is to fill a sequence of N positive integers into a spir ...
随机推荐
- 使用ffmpeg搭建HLS直播系统
[时间:2018-04] [状态:Open] [关键词:流媒体,stream,HLS, ffmpeg,live,直播,点播, nginx, ssegment] 0 引言 本文作为HLS综述的后续文章. ...
- Spark 广播变量BroadCast
一. 广播变量 广播变量允许程序员将一个只读的变量缓存在每台机器上,而不用在任务之间传递变量.广播变量可被用于有效地给每个节点一个大输入数据集的副本.Spark还尝试使用高效地广播算法来分发变量,进而 ...
- Codeforces Round #313 (Div. 2) C. Gerald's Hexagon(补大三角形)
C. Gerald's Hexagon time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 消息中间件系列四:RabbitMQ与Spring集成
一.RabbitMQ与Spring集成 准备工作: 分别新建名为RabbitMQSpringProducer和RabbitMQSpringConsumer的maven web工程 在pom.xml文 ...
- Dart
Dart异步与阻塞 import 'dart:async'; import 'dart:io'; void main() async { for(int i = 0;i<10;i++) { as ...
- 获取mssqlserver数据库表的字段名称,字段说明,数据类型,主键等表的信息
sql脚本: SELECT TableName then d.name else '' end,---表名 TableShowsThat then isnull(f.value,'') else '' ...
- 关于ping地址的几个意义
1.ping 127.0.0.1 判断电脑的tcp/ip协议栈有没有问题 2.ping 本机ip 判断电脑的网卡驱动有没有问题 3.ping 网关 判断数据是否可以到达路由 4.ping www.ba ...
- linux signal
1) SIGHUP 本信号在用户终端连接(正常或非正常)结束时发出, 通常是在终端的控制进程结束时, 通知同一session内的各个作业, 这时它们与控制终端不再关联. 登录Linux时,系统会分配给 ...
- System.InvalidOperationException:“线程间操作无效: 从不是创建控件“txtPortName02”的线程访问它。”
“System.InvalidOperationException”类型的未经处理的异常在 System.Windows.Forms.dll 中发生 其他信息: 线程间操作无效: 从不是创建控件“tx ...
- docker-compose & docker 镜像/加速
docker-compose: http://sanwen.net/a/nuwruoo.html docker加速: http://guide.daocloud.io/dcs/daocloud-915 ...