leetcode :Spiral Matrix
题目很简单,就是花式拐弯遍历一个矩阵。
既然题目花式拐弯遍历矩阵,那我们就来一个花式漂移完成拐弯瞬间爆炸的解法:
1,定义四个移动操作,goLeft,goRight,goUp,goDown
2,定义四个边界,up,down,left, right,当goLeft走到头的时候,将down边界往上移一行,以此类推
4,使用一个自动机定义某个方向走到边界时,下一步应该往哪走,可以用一个function数组 (即 vector<function<bool(int&,int&)>> func) 来保存操作,
一个nextStatus数组 (int nextStatus = {1, 2, 3, 0};) 来保存下一步的操作
AC代码:
class Solution { public: vector<int> spiralOrder(vector<vector<int>>& matrix) { vector<int> ret; if (matrix.size() == 0) { return ret; } vector<function<bool(int&, int&)>> func; int left = -1, right = matrix[0].size(), up = -1, down = matrix.size(); func.push_back(bind(&Solution::goRight, this, ref(ret), ref(matrix), ref(right), ref(up), placeholders::_1, placeholders::_2)); func.push_back(bind(&Solution::goDown, this, ref(ret), ref(matrix), ref(down), ref(right), placeholders::_1, placeholders::_2)); func.push_back(bind(&Solution::goLeft, this, ref(ret), ref(matrix), ref(left), ref(down), placeholders::_1, placeholders::_2)); func.push_back(bind(&Solution::goUp, this, ref(ret), ref(matrix), ref(up), ref(left), placeholders::_1, placeholders::_2)); int nextStatus[4] = {1, 2, 3, 0}; int status = 0; int x = 0, y = -1; while (ret.size() != matrix.size() * matrix[0].size()) { if (!func[status](x, y)) { status = nextStatus[status]; } } return ret; } bool goLeft(vector<int>& ret, vector<vector<int>>& matrix, int& left, int& down, int& x, int& y) { if (y - 1 >left) { y -= 1; if (y - 1 == left) { --down; } ret.push_back(matrix[x][y]); return true; } return false; } bool goRight(vector<int>& ret, vector<vector<int>>& matrix, int& right, int& up, int& x, int& y) { if (y + 1 < right) { y += 1; if (y + 1 == right) { ++up; } ret.push_back(matrix[x][y]); return true; } return false; } bool goUp(vector<int>& ret, vector<vector<int>>& matrix, int &up, int &left, int& x, int& y) { if (x - 1 > up) { x -= 1; if (x - 1 == up) { ++left; } ret.push_back(matrix[x][y]); return true; } return false; } bool goDown(vector<int>& ret, vector<vector<int>>& matrix, int &down, int &right, int& x, int& y) { if (x + 1 < down) { x += 1; if (x + 1 == down) { --right; } ret.push_back(matrix[x][y]); return true; } return false; } };
浙公网安备 33010602011771号