【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 ...
随机推荐
- 解决git 不同branch 下node_moudes不同步的问题
做nodejs开发,或者vue react等开发的同学,在使用git 做版本管理的时候肯定碰到过这个问题. 按常规做法node_modules 肯定是被添加到.gitignore中不需要被提交的 但是 ...
- [转]SOA架构设计经验分享—架构、职责、数据一致性
阅读目录: 1.背景介绍 2.SOA的架构层次 2.1.应用服务(原子服务) 2.2.组合服务 2.3.业务服务(编排服务) 3.SOA化的重构 3.1.保留服务空间,为了将来服务的组合 4.运用DD ...
- dma 测试例子
#include <linux/module.h> #include <linux/slab.h> #include <linux/sched.h> #includ ...
- PDFBOX详解
PDFBOX详解 摘要 自从Adobe公司1993年第一次发布公共PDF参考以来,支持各种语言和平台的PDF工具和类库就如雨后春笋般涌现.然而,Java应用开发中Adobe技术的支持相对滞后了. 自从 ...
- 转载记录一个有效的jetbrains激活码
来自:https://blog.csdn.net/ahun535915415/article/details/80687762 K03CHKJCFT-eyJsaWNlbnNlSWQiOiJLMDNDS ...
- hdoj:2069
Coin Change Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HTTP 05 安全
TCP 打包时加密, 报文传输时 加密 通过证书来判断是否是可以访问的客户端/服务器. 公开秘钥 进行加密, 随便被人知道 私有秘钥 进行解密, 自己保留 http + 加密 + 认证 + 完整性保护 ...
- opencv利用直方图判断人脸光照质量
懒得用中文再写一遍了, 直接传送门过去吧. https://medium.com/@fanzongshaoxing/detect-face-in-bad-lighting-condition-usin ...
- linux中,history命令,显示时间戳?操作人?IP地址?
需求描述: 在linux环境中,有的时候为了审计的需要,要记录谁什么时间从什么IP登录,执行了什么命令,bash的history命令就能够记录这些信息,但是在默认的情况下,是不记录时间的,所以呢,在这 ...
- SQL server 在附加数据库后,数据库总是变成了只读
1. 要把数据库文件的属性改了 右键点击两个文件的属性--安全--添加--立即查找--找everyone这个用户把他的权限都勾上 确定再附加就OK. 2. 在数据库管理器中对数据库点右键属性,然后切 ...