leetcode第十周解题总结
来源:互联网 发布:联通ssr免流端口2017 编辑:程序博客网 时间:2024/05/29 17:32
54. 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].
题意解析:
对一个矩阵进行螺旋式的访问
解题思路:
将螺旋式的访问顺序变成四个部分进行操作。
class Solution {public: vector<int> spiralOrder(vector<vector<int>>& matrix) { vector<int> result; int m = matrix.size(); if (m == 0) return result; int n = matrix[0].size(); int row = 0; int col = -1; while (true) { for(int i = 0; i < n; i ++) { col++; result.push_back(matrix[row][col]); } m--; if(m == 0) break; for(int i = 0; i < m; i ++) { row++; result.push_back(matrix[row][col]); } n--; if(n == 0) break; for(int i = 0; i < n; i ++) { col--; result.push_back(matrix[row][col]); } m--; if(m == 0) break; for(int i = 0; i < m; i ++) { row--; result.push_back(matrix[row][col]); } n--; if(n == 0) break; } return result; }};
0 0
- leetcode第十周解题总结
- leetcode第十四周解题总结--二分查找
- leetcode第十一周解题总结(155,150,20)--栈
- leetcode第七周解题总结
- leetcode第八周解题总结
- leetcode第十七周解题总结
- 算法第十周解题
- leetcode第六周解题总结(98)
- leetcode第十二周解题总结--动态规划
- leetcode第十六周解题总结--图
- leetcode第十八周解题总结-贪心算法
- LeetCode解题思路总结
- Leetcode解题思路总结(Easy)
- Leetcode解题思路总结(Medium)
- Leetcode解题思路总结(Easy)
- leetcode第二周解题总结(3,8,65)
- leetcode第三周解题总结(5,7,66)
- leetcode第五周解题总结(138, 23)
- 欢迎使用CSDN-markdown编辑器
- 【LeetCode】Container with Most Water
- Mapper XML配置
- UICollectionView入门--使用系统UICollectionViewFlowLayout布局类
- Java后端书籍推荐2
- leetcode第十周解题总结
- Python-字典
- Django之路
- Ehcache(03)——Ehcache中储存缓存的方式
- MPICH2服务器集群简单的安装配置总结
- 接口篇(5.6) 01. 内网接口的拆分 ❀ 飞塔 (Fortinet) 防火墙
- properties元素
- 阿里百川HotFix 接入问题记录
- 第二行代码学习笔记之第一章