题目来源


https://leetcode.com/problems/spiral-matrix-ii/

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


题意分析


Input: n:integer

Output:a matrix decribed as list[list[]]

Conditions:输入一个大小,得到一个方形矩阵,然后形成蛇形矩阵


题目思路


本题与54题属于一个类别,都是蛇形遍历,这里直接先生成一个初始化的matrix,然后遍历一遍,利用累加变量来赋值


AC代码(Python)


__author__ = 'YE'

class Solution(object):
def generateMatrix(self, n):
"""
:type n: int
:rtype: List[List[int]]
"""
if n == 0:
return []
matrix = []
for i in range(n):
l = [0 for j in range(n)]
matrix.append(l) up = 0
left = 0
down = len(matrix) - 1
right = len(matrix[0]) - 1 direct = 0 res = [] count = 1 while True:
if direct == 0:
for i in range(left, right + 1):
matrix[up][i] = count
count += 1
up += 1
if direct == 1:
for i in range(up, down + 1):
matrix[i][right] = count
count += 1
right -= 1
if direct == 2:
for i in range(right, left - 1, -1):
matrix[down][i] = count
count += 1
down -= 1
if direct == 3:
for i in range(down, up -1, -1):
matrix[i][left] = count
count += 1
left += 1
if up > down or left > right:
return matrix
direct = (direct + 1) % 4 print(Solution().generateMatrix(3))

[LeetCode]题解(python):059-Spiral Matrix II的更多相关文章

  1. Java for LeetCode 059 Spiral Matrix II

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

  2. 059. Spiral Matrix II

    题目链接:https://leetcode.com/problems/spiral-matrix-ii/description/ Given a positive integer n, generat ...

  3. LeetCode(59)SPiral Matrix II

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

  4. LeetCode 题解 Search a 2D Matrix II。巧妙!

    [ [1, 4, 7, 11, 15], [2, 5, 8, 12, 19], [3, 6, 9, 16, 22], [10, 13, 14, 17, 24], [18, 21, 23, 26, 30 ...

  5. 059 Spiral Matrix II 旋转打印矩阵 II

    给出正整数 n,生成正方形矩阵,矩阵元素为 1 到 n2 ,元素按顺时针顺序螺旋排列.例如,给定正整数 n = 3,应返回如下矩阵:[ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6 ...

  6. LeetCode: Spiral Matrix II 解题报告-三种方法解决旋转矩阵问题

    Spiral Matrix IIGiven an integer n, generate a square matrix filled with elements from 1 to n2 in sp ...

  7. Leetcode 54. Spiral Matrix & 59. Spiral Matrix II

    54. Spiral Matrix [Medium] Description Given a matrix of m x n elements (m rows, n columns), return ...

  8. 【leetcode】Spiral Matrix II

    Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...

  9. leetcode 54. Spiral Matrix 、59. Spiral Matrix II

    54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...

  10. 【leetcode】59.Spiral Matrix II

    Leetcode59 Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 ...

随机推荐

  1. unity textFilde

    #pragma strict private var editUsername:String; private var editPassword:String; private var editSho ...

  2. access-Control-Allow-Origin跨域请求安全隐患

    最新的W3C标准里是这么实现HTTP跨域请求的,Cross-Origin Resource Sharing,就是跨域的目标服务器要返回一系列的Headers,通过这些Headers来控制是否同意跨域. ...

  3. web farm 讨论引出

    关于web farm 有成功的实施的文档没 用它还不如 用nginx,简单易用. Nginx for windows的运行效果咋样 windows  iis无敌 玩nginx就不要用win系统,必须l ...

  4. wp控件

    导航控件 Silverlight的Windows Phone应用程序是基于一种可以让用户在不同页面内容间来回导航的页面模型.这个模型是基于其中的frame控件,而页面间的导航就是靠它. 下面的表格列出 ...

  5. [Unity2D]Box Collider 2D盒子碰撞器

    盒子碰撞器(BoxCollider2D)是Unity2D中常用的碰撞器,所有为碰撞器,顾名思义,就是用于检测物体之间的碰撞情况的,Unity2D里面除了BoxCollider2D碰撞器之外还集成Box ...

  6. C# JS URL 中文传参出现乱码的解决方法

    在传参是先编码在传输,接受时先编码,在接收. string mm=Server.URLEncode(你); Response.Redirect(index.aspx?mm=+mm); 然后在接收页解码 ...

  7. 关于HTML条件注释你可能不知道的一些事儿

    最近经常看到类似这样的HTML代码片段,很多前端开发人员应该都熟悉: 1 <!--[if lt IE 7]>      <html class="ie6"> ...

  8. 外部调用JS文件时出现中文乱码的解决办法

    若测试网页的编码格式为:gb2312,而调用外部JS文件时出现了乱码(前提是JS文件无错误),则将调用的外部JS文件用记事本打开,然后再保存成编码格式为UTF-8的JS文件即可. 若测试网页的编码格式 ...

  9. 使用Objective-C的文档生成工具:appledoc

    使用Objective-C的文档生成工具:appledoc 前言 做项目的人多了,就需要文档了.今天开始尝试写一些项目文档.但是就源代码来说,文档最好和源码在一起,这样更新起来更加方便和顺手.象 Ja ...

  10. 关于 swift 的图片多选问题

    http://stackoverflow.com/questions/20756899/how-to-select-multiple-images-from-uiimagepickercontroll ...