【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 ...
随机推荐
- ios调试-查看日志
连上mac,然后打开“console.app”, 左侧设备里选这个设备.
- DTD约束简介
DTD约束简介 文档类型声明 文档类型声明就是DOCTYPE,它告诉解析器,XML文档必须遵循DTD定义.同时,他也告诉解析器,到哪里找到文档定义的其余内容.在前边的例子里DOCTYPE很简单: &l ...
- Android多种格式的异步解压/压缩解决方案
前言 最近由于项目需要,需要我谅解一下关于在移动平台的解压功能,在移动平台解压,我个人感觉是没有太大必要的,毕竟手机的性能有限.但是,不口否认,移动端的解压功能又是必备的,因为如果对于一些资源管理器类 ...
- 移动网页广告引入mraid.js使用指南
在网上找mraid相关资料,相对比较少,大多都是API介绍,概念介绍等,没有一份详细的移动端网页广告使用教程,经过自己两天的摸索,完成了开发的移动端网页版的广告加入mraid功能. 背景: 我开发了移 ...
- CSS外边距属性,深入理解margin
margin See the Pen margin by wmui (@wmui) on CodePen. 该属性用于设置元素的外边距,外边距是透明的,默认值0.这是一个简写属性,属性值最多为4个,例 ...
- My google script which based on Google Sheet and Form
My google script which based on Google Sheet and Form // get sheet data function getSpreadsheetData( ...
- 【CF446D】DZY Loves Games 高斯消元+矩阵乘法
[CF446D]DZY Loves Games 题意:一张n个点m条边的无向图,其中某些点是黑点,1号点一定不是黑点,n号点一定是黑点.问从1开始走,每次随机选择一个相邻的点走过去,经过恰好k个黑点到 ...
- 算法提高 金属采集_树形dp
算法提高 金属采集 时间限制:1.0s 内存限制:256.0MB 问题描述 人类在火星上发现了一种新的金属!这些金属分布在一些奇怪的地方,不妨叫它节点好了.一些节点之间有道路相连 ...
- centos7 与 archlinux用户 安装 python3模块 pytaglib
对于 centos7用户: yum group install "Development Tools" yum install taglib-devel yum install p ...
- 29、sass
SASS 一.SASS的作用: 方便编写CSS. 二.SASS依赖的环境 : Ruby 三.如何安装SASS? gem install sass gem update sass (更新sass) ge ...