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)的更多相关文章

  1. [LeetCode每日一题]81. 搜索旋转排序数组 II

    [LeetCode每日一题]81. 搜索旋转排序数组 II 问题 已知存在一个按非降序排列的整数数组 nums ,数组中的值不必互不相同. 在传递给函数之前,nums 在预先未知的某个下标 k(0 & ...

  2. LeetCode(59)SPiral Matrix II

    题目 Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. F ...

  3. 【js】Leetcode每日一题-解码异或后数组

    [js]Leetcode每日一题-解码异或后数组 [题目描述] 未知 整数数组 arr 由 n 个非负整数组成. 经编码后变为长度为 n - 1 的另一个整数数组 encoded ,其中 encode ...

  4. [LeetCode每日一题]153.寻找旋转排序数组中的最小值

    [LeetCode每日一题]153.寻找旋转排序数组中的最小值 问题 已知一个长度为 n 的数组,预先按照升序排列,经由 1 到 n 次 旋转 后,得到输入数组.例如,原数组 nums = [0,1, ...

  5. 【python】【补】Leetcode每日一题-合并两个有序数组

    [python]Leetcode每日一题-合并两个有序数组 [题目描述] 给你两个有序整数数组 nums1 和 nums2,请你将 nums2 合并到 nums1 中,使 nums1 成为一个有序数组 ...

  6. [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 ...

  7. [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 ...

  8. [leetcode]54. Spiral Matrix2生成螺旋数组

    import java.util.Arrays; /** * Created by lvhao on 2017/7/6. * Given an integer n, generate a square ...

  9. PAT 1105 Spiral Matrix[模拟][螺旋矩阵][难]

    1105 Spiral Matrix(25 分) This time your job is to fill a sequence of N positive integers into a spir ...

随机推荐

  1. python模块import具体用法

    同级目录 import 文件名 form 文件名 import * 子目录 import 目录名.文件名 form 目录名.文件名 import * 不同目录 先导入sys包,然后把对应的目录加入pa ...

  2. 【iCore4 双核心板_ARM】例程三十一:HTTP_IAP_FPGA实验——更新升级FPGA

    实验现象: 核心代码: int main(void) { GPIO_InitTypeDef GPIO_InitStruct; __HAL_RCC_GPIOI_CLK_ENABLE(); __HAL_R ...

  3. Vue:$set和$delete

    一.$set 在开始讲解$set之前先看下面的一段代码,实现的功能:当点击“添加”按钮时,动态的给data里面的对象添加属性和值,代码示例如下: <!DOCTYPE html> <h ...

  4. 第四百零四节,python网站第三方登录,social-auth-app-django模块,

    第四百零四节,python网站第三方登录,social-auth-app-django模块, social-auth-app-django模块是专门用于Django的第三方登录OAuth2协议模块 目 ...

  5. CentOS安装和配置Mysql

    1. Centos 默认的yum 是没有Mysql5.7 所以需要配置下,从官网获取最新的RPM包 在MySQL官网中下载YUM源rpm安装包:https://dev.mysql.com/downlo ...

  6. KafkaManager对offset的两种管理方式

    OffsetManager主要提供对offset的保存和读取,每个broker都有一个OffsetManager实例,kafka管理topic的偏移量有2种方式: 1.ZookeeperOffsetM ...

  7. kafka---->kafka connect的使用(一)

    这里面介绍一下kafka connect的一些使用. kafka connect的使用 一.在config目录下面复制一个file-srouce.properties并且修改内容 huhx@gohuh ...

  8. pycharm 2018.1 激活

    pycharm 2018.1 License server 填入 https://jetlicense.nss.im/ 激活没有问题 测试时间 2018.4.18

  9. windows上,任务管理器中,进程命令行太长怎么办

    一.前言 在windows上,有时候需要查看进程命令行,但是有的进程的命令行太长了,很难看全 此时,可以使用下面的方法解决(红框改为自己要查看的进程即可): C:\Users\Gaoyu>wmi ...

  10. Solve Error: 'NSInvalidArgumentException', reason: '-[UITableView mas_makeConstraints:]: unrecognized selector sent to instance 0x7fa5c402fa00'

    下面是iOS开发用第三方库可能出现的错误,及其解决方法: 1. 'NSInvalidArgumentException', reason: '-[UITableView mas_makeConstra ...