题目来源


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. 【wikioi】1227 方格取数 2(费用流)

    http://www.wikioi.com/problem/1227 裸题,拆点,容量为1,费用为点权的负数(代表只能取一次).再在拆好的两个点连边,容量为oo,费用为0.(代表能取0) 然后向右和下 ...

  2. Json 数组拼接

    var str1 = {"name": "apple", "sex": "21"};                 / ...

  3. JS按位非(~)运算符与~~运算符的理解分析

    按位非运算符,简单的理解就是改变运算数的符号并减去1,当然,这是只是简单的理解能转换成number类型的数据. 那么,对于typeof var!==”number”的类型来说,进行运算时,会尝试转化成 ...

  4. winform学习之----打开文件对话框并将文件内容放入文本框

    OpenFileDialog ofg = new OpenFileDialog(); ofg.Title = "ddd";//设置对话框标题 ofg.Multiselect = t ...

  5. 如何获取checkboxlist的多个选中项

    string[] array = dt.Rows[0]["s_type"].ToString().Split('|');                foreach (ListI ...

  6. 使用mybatis执行oracle存储过程

    存储过程在小公司用的不多,但是如果业务比较复杂或者性能要求比较苛刻的时候存储过程就派上用场了,ibatis的前期的一些版本貌似不支持存储过程因此我选择了mybatis来做实验. 1.无输入和输出参数的 ...

  7. iOS5中UIViewController的新方法

    iOS5中UIViewController的新方法 前言 在苹果的 WWDC2011 大会视频的<Session 101 - What's New in Cocoa> 和<Sessi ...

  8. 在Excel中实现查询功能

    $sn = Read-Host -Prompt "请输入员工号|序列号|资产号" $xl = New-Object -ComObject "Excel.Applicati ...

  9. 《GK101任意波发生器》升级固件发布(版本:1.0.2.build126)

    一.固件说明: 硬件版本:0,logic.3 固件版本:1.0.2.build126 编译日期:2014-08-23 ====================================== 二. ...

  10. Spring框架中的定时器 如何使用和配置

    目标类 <bean id="myTimer" class="com.timer.MyTimer"></bean> 配置你的定时器详情 & ...