54. Spiral Matrix
来源:互联网 发布:2017游戏鼠标推荐 知乎 编辑:程序博客网 时间:2024/06/05 04:22
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 ]]
class Solution(object): def spiralOrder(self, matrix): result = [] m = len(matrix) if m == 0: return result else: n = len(matrix[0]) #print('m=',m,'n=',n) #print('int((min(m,n)+1)/2=',int((min(m,n)+1)/2)) for i in range(int((min(m,n)+1)/2)): #print('i=',i) temp_i = i temp_j = i #print('line1------------------------------------------') while(temp_j>=0 and temp_j<n-i): #print(matrix[temp_i][temp_j]) result.append(matrix[temp_i][temp_j]) temp_j+=1 temp_j-=1 temp_i+=1 #print('line2------------------------------------------') while(temp_i>=0 and temp_i<m-i): #print(matrix[temp_i][temp_j]) result.append(matrix[temp_i][temp_j]) temp_i+=1 temp_i-=1 temp_j-=1 #print('line3-----------temp_i=------------temp_j=-------------------',temp_i,temp_j) while(temp_j>=i+1 and temp_i>i): #print(matrix[temp_i][temp_j]) result.append(matrix[temp_i][temp_j]) temp_j-=1 #print('line4-----------temp_i=------------temp_j=-------------------',temp_i,temp_j) while(temp_i>i and temp_j >=i): #print(matrix[temp_i][temp_j]) result.append(matrix[temp_i][temp_j]) temp_i-=1 return result
阅读全文
0 0
- 54. Spiral Matrix && 59. Spiral Matrix II
- 59. Spiral Matrix II && 54. Spiral Matrix
- LeetCode 54. Spiral Matrix
- [LeetCode]54.Spiral Matrix
- LeetCode --- 54. Spiral Matrix
- [Leetcode] 54. Spiral Matrix
- [leetcode] 54.Spiral Matrix
- 54. Spiral Matrix
- Leetcode-54.Spiral Matrix
- **LeetCode 54. Spiral Matrix
- Leetcode 54. Spiral Matrix
- [leetcode] 54. Spiral Matrix
- 54. Spiral Matrix
- leetcode 54. Spiral Matrix
- 54. Spiral Matrix LeetCode
- 54. Spiral Matrix
- 54. Spiral Matrix
- leetcode 54. Spiral Matrix
- 一个复杂系统的拆分改造实践
- HDU 4548 美素数 (素数打表+前缀和)
- 资料 url
- 关于mybatis批量更新操作的相关注意事项
- 移动端input呼出的键盘,换行(enter)变为搜索
- 54. Spiral Matrix
- windows下的定时计划
- vue2:一个简单实例
- web端get和post请求支持跨域请求
- Codeforces 835E E. The penguin's game 交互题 多猪试毒 (写的挺好的)
- cocos2d实现2D地图A*广度路径算法
- 自适应网页设计
- 莫队+树状数组 AHOI 作业
- 使用cucumber-jvm自动化测试中遇到问题及解决