54. Spiral Matrix
生活随笔
收集整理的這篇文章主要介紹了
54. Spiral Matrix
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
description:
螺旋輸出一個矩陣.
Note:
Example:
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]answer:
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;} };relative point get√:
hint :
轉載于:https://www.cnblogs.com/forPrometheus-jun/p/11262696.html
總結
以上是生活随笔為你收集整理的54. Spiral Matrix的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 学习Knowledge Graph Em
- 下一篇: 第三方控件DevExpress的Tree