【leetcode】Spiral Matrix
题目概要:
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.
For example,
Given the following matrix:
[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ]
You should return [1,2,3,6,9,8,7,4,5].
解题思路:
这个题的关键在于指针的计算,一定要细心,我们用四个量分别表示行和列的起始位置和结束位置,每次进行四个循环,将得到的数字加入返回列表,同时修改相应的指针值,同时我们要注意到这样得到的列表是会有冗余的,我们只要截取最前面的就可以。
具体的修改过程:
1.从左到右,结束后因为对应的最开始一行已经遍历完成,所以标记行的开始的要自增
2.从上到下,结束后最后一列已经完成,故标记列的结束的要自减
3.从右到左,结束后最下一行已经完成,故标记行的结束的自减
4.从下到上,结束后第一列已经完成,故标记列的自增
这里也可以看出,会有重复的计算的,但是重复小于一圈,故可以直接采用截断的方式,当然在循环中加入判断亦可以达到这个目的。
class Solution:
# @param matrix, a list of lists of integers
# @return a list of integers
def spiralOrder(self, matrix):
if len(matrix) == 0:
return []
r, c = len(matrix), len(matrix[0])
if c == 0:
return []
roundr = 0
roundc = 0
res = []
while roundr < r and roundc < c:
for i in range(roundc, c):
res.append(matrix[roundr][i])
roundr += 1
for i in range(roundr, r):
res.append(matrix[i][c-1])
c -= 1
for i in range(c-1,roundc-1,-1):
res.append(matrix[r-1][i])
r -= 1
for i in range(r-1,roundr-1,-1):
res.append(matrix[i][roundc])
roundc += 1
return res
#return res[0:len(matrix)*len(matrix[0])];
s = Solution()
print s.spiralOrder([
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
])
print s.spiralOrder([[7],[9],[6]])
print s.spiralOrder([[7,9,6]])
【leetcode】Spiral Matrix的更多相关文章
- 【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】Spiral Matrix II (middle)
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- 【leetcode】 Spiral Matrix
Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...
- 【leetcode】Spiral Matrix(middle)
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- 【LeetCode】Spiral Matrix(螺旋矩阵)
这是LeetCode里的第54道题. 题目要求: 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
- 【LeetCode】Set Matrix Zeroes 解题报告
今天看到CSDN博客的勋章换了图表,同一时候也添加显示了博客等级,看起来都听清新的,感觉不错! [题目] Given a m x n matrix, if an element is 0, set i ...
- 【数组】Spiral Matrix II
题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. ...
- 【数组】Spiral Matrix
题目: Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spira ...
随机推荐
- c#线程间操作UI-Invoke和BeginInvoke
利用Invoke和或BeginInvoke实现线程间操作UI的简单例子. /* 窗体包含一个button和一个richtextbox控件 * 注:必须在子线程中执行Invoke和或BeginInvok ...
- Ubuntu(基于Ubuntu)中常用的apt和dpkt命令
apt-get sudo apt-get install package 安装包 sudo apt-get -f install 修复安装”-f = ——fix-missing” sudo a ...
- java高新技术-可变参数与OverLoad相关面试题分析
可变参数 可变参数的特点: 只能出现在参数列表的最后: ...位于变量类型和变量名之间,前后有无空格都可以: 调用可变参数的方法时,编译器为该可变参数隐含创建一个数组,在方法中以数组的形式访问可变参数 ...
- eclipse安装和配置
一.下载eclipse eclipse下载页 (选择"Eclipse IDE for Java EE Developers",适用于web和android开发) 我用的是luna的 ...
- 【CityHunter】服务器端设计思路
设计服务端程序首先我考虑到的是通讯传输方式的设计,按照CityHunter的特殊性,其具有两种使用场景: 仅用于查看当前信息状态.搜索周边环境.对信息的实时性要求不高的一些场景: 用于攻略藏宝图或Ch ...
- HTML5触摸事件(touchstart、touchmove和touchend) (转)
HTML5中新添加了很多事件,但是由于他们的兼容问题不是很理想,应用实战性不是太强,所以在这里基本省略,咱们只分享应用广泛兼容不错的事件,日后随着兼容情况提升以后再陆续添加分享.今天为大家介绍的事件主 ...
- boost之lexical_cast
第一次翻译,虽然是个很简单的函数介绍... 文件boost/lexical_cast.hpp中定义了此函数: namespace boost { class bad_lexical_cast; tem ...
- jQuery知识点一 each()和toggleClass()
jQuery的一些东东比较容易忘,所以在这里整理一下... ... 1. each (1) $(selector).each(function(index,element)) inde ...
- 【转载】使用pandas进行数据清洗
使用pandas进行数据清洗 本文转载自:蓝鲸的网站分析笔记 原文链接:使用python进行数据清洗 目录: 数据表中的重复值 duplicated() drop_duplicated() 数据表中的 ...
- hadoop源码编译——2.5.0版本
强迫症必治: WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using b ...