[LeetCode]题解(python):059-Spiral Matrix II
题目来源
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的更多相关文章
- 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 ...
- 059. Spiral Matrix II
题目链接:https://leetcode.com/problems/spiral-matrix-ii/description/ Given a positive integer n, generat ...
- LeetCode(59)SPiral Matrix II
题目 Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. F ...
- 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 ...
- 059 Spiral Matrix II 旋转打印矩阵 II
给出正整数 n,生成正方形矩阵,矩阵元素为 1 到 n2 ,元素按顺时针顺序螺旋排列.例如,给定正整数 n = 3,应返回如下矩阵:[ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6 ...
- LeetCode: Spiral Matrix II 解题报告-三种方法解决旋转矩阵问题
Spiral Matrix IIGiven an integer n, generate a square matrix filled with elements from 1 to n2 in sp ...
- 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 ...
- 【leetcode】Spiral Matrix II
Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...
- leetcode 54. Spiral Matrix 、59. Spiral Matrix II
54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...
- 【leetcode】59.Spiral Matrix II
Leetcode59 Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 ...
随机推荐
- CSS创建三角形(小三角)的几种方法
你可以在很多地方看到三角形(小三角):tooltips提示框.下拉菜单.甚至在loading载入动画里.不管你喜欢还是不喜欢,这些小元素对各UI元素之间的联系关系式很重要的. 有一些不同的方法来设计并 ...
- HTML的快速写法:Emmet和Haml
HTML代码写起来很费事,因为它的标签多. 一种解决方法是采用模板, 在别人写好的骨架内,填入自己的内容.还有一种就是我今天想要介绍的方法—-简写法. 常用的简写法,目前主要是Emmet和Haml两种 ...
- BZOJ2738: 矩阵乘法
Description 给你一个N*N的矩阵,不用算矩阵乘法,但是每次询问一个子矩形的第K小数. Input 第一行两个数N,Q,表示矩阵大小和询问组数: 接下来N行N列一共N*N个数,表示这个矩阵: ...
- Google Code Jam 2010 Round 1A Problem A. Rotate
https://code.google.com/codejam/contest/544101/dashboard#s=p0 Problem In the exciting game of Jo ...
- 【转】delphi程序只允许运行一个实例的三种方法:
一. 创建互斥对象 在工程project1.dpr中创建互斥对象 Program project1 Uses Windows,Form, FrmMain in 'FrmMain.pas' ...
- Xshell选中的同时把内容复制到剪贴板(还可以设置设置文本分隔符)
1.设置对话框 工具 -> 选项 -> 键盘和鼠标 -> 将选定的文本自动复制到剪贴板 2.贴图如下 2.1.打开设置对话框 2.2.设置键盘鼠标,左键复制 2.3.右键粘贴 作者: ...
- zabbix企业级监控概述和部署
官方网站:http://www.zabbix.com/ zabbix是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案 zabbix能监视各种网络参数,保证服务器系统的安全 ...
- 又一种Mysql报错注入
from:https://rdot.org/forum/showthread.php?t=3167 原文是俄文,所以只能大概的翻译一下 这个报错注入主要基于Mysql的数据类型溢出(不适用于老版本的M ...
- MUI - 预加载
预加载都是在mui.init({)}中执行的 方式一:preload一次仅能预加载一个页面(除非循环) var subWebview = mui.preload({ url: 'examples/ac ...
- [APAC]手动截取当前活动窗口,并且按规则命名(1/2)
Function Take-ScreenShot { <# .SYNOPSIS Used to take a screenshot of the desktop or the active wi ...