54. Spiral Matrix
2016-05-10 16:26
351 查看
题目:
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:
You should return
题意:
给定一个m行n列的数组,将该数组按螺旋状输出。
思路:
转载地址:https://leetcode.com/discuss/38974/a-concise-c-implementation-based-on-directions
When traversing the matrix in the spiral order, at any time we follow one out of the following four directions: RIGHT DOWN LEFT UP. Suppose we are working on a 5 x 3 matrix as such:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Imagine a cursor starts off at (0, -1), i.e. the position at '0', then we can achieve the spiral order by doing the following:
Go right 5 times
Go down 2 times
Go left 4 times
Go up 1 times.
Go right 3 times
Go down 0 times -> quit
Notice that the directions we choose always follow the order 'right->down->left->up', and for horizontal movements, the number of shifts follows:{5, 4, 3}, and vertical movements follows {2, 1, 0}.
Thus, we can make use of a direction matrix that records the offset for all directions, then an array of two elements that stores the number of shifts for horizontal and vertical movements, respectively. This way, we really just need one for loop instead of
four.
Another good thing about this implementation is that: If later we decided to do spiral traversal on a different direction (e.g. Counterclockwise), then we only need to change the Direction matrix; the main loop does not need to be touched.
代码:
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].、
题意:
给定一个m行n列的数组,将该数组按螺旋状输出。
思路:
转载地址:https://leetcode.com/discuss/38974/a-concise-c-implementation-based-on-directions
When traversing the matrix in the spiral order, at any time we follow one out of the following four directions: RIGHT DOWN LEFT UP. Suppose we are working on a 5 x 3 matrix as such:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Imagine a cursor starts off at (0, -1), i.e. the position at '0', then we can achieve the spiral order by doing the following:
Go right 5 times
Go down 2 times
Go left 4 times
Go up 1 times.
Go right 3 times
Go down 0 times -> quit
Notice that the directions we choose always follow the order 'right->down->left->up', and for horizontal movements, the number of shifts follows:{5, 4, 3}, and vertical movements follows {2, 1, 0}.
Thus, we can make use of a direction matrix that records the offset for all directions, then an array of two elements that stores the number of shifts for horizontal and vertical movements, respectively. This way, we really just need one for loop instead of
four.
Another good thing about this implementation is that: If later we decided to do spiral traversal on a different direction (e.g. Counterclockwise), then we only need to change the Direction matrix; the main loop does not need to be touched.
代码:
class Solution { public: vector<int> spiralOrder(vector<vector<int>>& matrix) { vector<vector<int> > dirs{{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; vector<int> res; int nr = matrix.size(); if (nr == 0) return res; int nc = matrix[0].size(); if (nc == 0) return res; vector<int> nSteps{nc, nr-1}; int iDir = 0; // index of direction. int ir = 0, ic = -1; // initial position while (nSteps[iDir%2]) { for (int i = 0; i < nSteps[iDir%2]; ++i) { ir += dirs[iDir][0]; ic += dirs[iDir][1]; res.push_back(matrix[ir][ic]); } nSteps[iDir%2]--; iDir = (iDir + 1) % 4; } return res; } };
相关文章推荐
- Mysql操作日志
- leetcode 之 Product of Array Except Self
- PHPAjax请求MySQL数据库
- SDK Manager打不开,闪一下就没了。
- python的import几种形式
- Android高效加载大图、多图解决方案,有效避免程序OOM
- 引用传递——值传递
- 实例讲解使用原生JavaScript处理AJAX请求的方法
- 第十一周阅读程序(补充)----(1)
- 点到平面的距离公式
- 一个用于大规模数据科学的API——DataFrame
- UITableView的两种重用Cell方法的区别
- HDU 百度之星(测试赛)1003 - IP聚合
- 从服务器端获取的图片放在客户端的控件上如何等比例裁切且不失真
- .net更换目标框架时,出现引用错误问题
- 分页方法,类(以后直接调用)
- 一道小博弈
- 注解
- 类的声明、定义、初始化
- 史上最详细的Android Studio系列教程二--基本设置与运行