problem:https://leetcode.com/problems/spiral-matrix-iii/
这道题挺简单的,只需要模拟一下题意就可以了。不断地增加步数和改变方向,直到已经读取到矩阵的所有数据。
class Solution { public: vector<int> dx{ 0,1,0,-1 }; vector<int> dy{ 1,0,-1,0 }; vector<vector<int>> spiralMatrixIII(int R, int C, int r0, int c0) { vector<vector<int>> res; int N = R * C; int step = 1; bool bUpdateStep = false; int k = 0; int times = 0; while (res.size() < N) { if(r0 >= 0 && r0 < R && c0 >= 0 && c0 < C) res.push_back({ r0, c0 }); r0 += dx[k]; c0 += dy[k]; times++; if (times == step) { times = 0; k = (k + 1) % 4; if (k % 2 == 0) { step++; } } } return res; } };
原文地址:https://www.cnblogs.com/fish1996/p/11263340.html
时间: 2024-11-06 07:10:35