Given a matrix of m x n elements (m rows, ncolumns), return all elements of the matrix in spiral order.
Example 1:
Input: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] Output: [1,2,3,6,9,8,7,4,5]
Example 2:
Input: [ [1, 2, 3, 4], [5, 6, 7, 8], [9,10,11,12] ] Output: [1,2,3,4,8,12,11,10,9,5,6,7]
這道題讓我們搓一個螺旋丸,將一個矩陣按照螺旋順序打印出來,只能一條邊一條邊的打印,首先要從給定的 mxn 的矩陣中算出按螺旋順序有幾個環,注意最中間的環可以是一個數字,也可以是一行或者一列。環數的計算公式是 min(m, n) / 2,知道了環數,就可以對每個環的邊按順序打印,比如對於題目中給的那個例子,個邊生成的順序是(用顏色標記了數字,Github 上可能無法顯示顏色,請參見博客園上的帖子) Red -> Green -> Blue -> Yellow -> Black
1 2 3
4 5 6
7 8 9
定義 p,q 為當前環的高度和寬度,當p或者q為1時,表示最后一個環只有一行或者一列,可以跳出循環。此題的難點在於下標的轉換,如何正確的轉換下標是解此題的關鍵,可以對照着上面的 3x3 的例子來完成下標的填寫,代碼如下:
解法一:
class Solution { public: vector<int> spiralOrder(vector<vector<int> > &matrix) { if (matrix.empty() || matrix[0].empty()) return {}; int m = matrix.size(), n = matrix[0].size(); vector<int> res; int c = m > n ? (n + 1) / 2 : (m + 1) / 2; int p = m, q = n; for (int i = 0; i < c; ++i, p -= 2, q -= 2) { for (int col = i; col < i + q; ++col) res.push_back(matrix[i][col]); for (int row = i + 1; row < i + p; ++row) res.push_back(matrix[row][i + q - 1]); if (p == 1 || q == 1) break; for (int col = i + q - 2; col >= i; --col) res.push_back(matrix[i + p - 1][col]); for (int row = i + p - 2; row > i; --row) res.push_back(matrix[row][i]); } return res; } };
如果覺得上面解法中的下標的轉換比較難弄的話,也可以使用下面這種坐標稍稍簡潔一些的方法。對於這種螺旋遍歷的方法,重要的是要確定上下左右四條邊的位置,那么初始化的時候,上邊 up 就是0,下邊 down 就是 m-1,左邊 left 是0,右邊 right 是 n-1。然后進行 while 循環,先遍歷上邊,將所有元素加入結果 res,然后上邊下移一位,如果此時上邊大於下邊,說明此時已經遍歷完成了,直接 break。同理對於下邊,左邊,右邊,依次進行相對應的操作,這樣就會使得坐標很有規律,並且不易出錯,參見代碼如下:
解法二:
class Solution { public: vector<int> spiralOrder(vector<vector<int>>& matrix) { if (matrix.empty() || matrix[0].empty()) return {}; int m = matrix.size(), n = matrix[0].size(); vector<int> res; int up = 0, down = m - 1, left = 0, right = n - 1; while (true) { for (int j = left; j <= right; ++j) res.push_back(matrix[up][j]); if (++up > down) break; for (int i = up; i <= down; ++i) res.push_back(matrix[i][right]); if (--right < left) break; for (int j = right; j >= left; --j) res.push_back(matrix[down][j]); if (--down < up) break; for (int i = down; i >= up; --i) res.push_back(matrix[i][left]); if (++left > right) break; } return res; } };
若對上面解法中的多個變量還是暈的話,也可以使用類似迷宮遍歷的方法,這里只要設定正確的遍歷策略,還是可以按螺旋的方式走完整個矩陣的,起點就是(0,0)位置,但是方向數組一定要注意,不能隨便寫,開始時是要往右走,到了邊界或者訪問過的位置后,就往下,然后往左,再往上,所以 dirs 數組的順序是 右->下->左->上,由於原數組中不會有0,所以就可以將訪問過的位置標記為0,這樣再判斷新位置的時候,只要其越界了,或者是遇到0了,就表明此時需要轉彎了,到 dirs 數組中去取轉向的 offset,得到新位置,注意這里的 dirs 數組中取是按循環數組的方式來操作,加1然后對4取余,按照這種類似迷宮遍歷的方法也可以螺旋遍歷矩陣,參見代碼如下:
解法三:
class Solution { public: vector<int> spiralOrder(vector<vector<int>>& matrix) { if (matrix.empty() || matrix[0].empty()) return {}; int m = matrix.size(), n = matrix[0].size(), idx = 0, i = 0, j = 0; vector<int> res; vector<vector<int>> dirs{{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; for (int k = 0; k < m * n; ++k) { res.push_back(matrix[i][j]); matrix[i][j] = 0; int x = i + dirs[idx][0], y = j + dirs[idx][1]; if (x < 0 || x >= m || y < 0 || y >= n || matrix[x][y] == 0) { idx = (idx + 1) % 4; x = i + dirs[idx][0]; y = j + dirs[idx][1]; } i = x; j = y; } return res; } };
Github 同步地址:
https://github.com/grandyang/leetcode/issues/54
類似題目:
參考資料:
https://leetcode.com/problems/spiral-matrix/
https://leetcode.com/problems/spiral-matrix/discuss/20719/0ms-Clear-C%2B%2B-Solution