59. Spiral Matrix II ***
生活随笔
收集整理的這篇文章主要介紹了
59. Spiral Matrix II ***
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
description:
螺旋型填充矩陣
Note:
Example:
Example:Input: 3 Output: [[ 1, 2, 3 ],[ 8, 9, 4 ],[ 7, 6, 5 ] ]answer:
class Solution { public:vector<vector<int>> generateMatrix(int n) {vector<vector<int>> res(n, vector<int>(n, 0));int up = 0, down = n - 1, right = n - 1, left = 0, val = 1;while(true) {for (int j = left; j <= right; ++j) res[up][j] = val++;if (++up > down) break;for (int i = up; i <= down; ++i) res[i][right] = val++;if (--right < left) break;for (int j = right; j >= left; --j) res[down][j] = val++;if (--down < up) break;for (int i = down; i >= up; --i) res[i][left] = val++;if (++left > right) break;}return res;} };relative point get√:
hint :
轉(zhuǎn)載于:https://www.cnblogs.com/forPrometheus-jun/p/11296416.html
總結(jié)
以上是生活随笔為你收集整理的59. Spiral Matrix II ***的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 介绍求解AX=b:可解性与解的结构
- 下一篇: CSS transition 的默认值