看板 Marginalman
※ 引述 《involution》 之銘言: :   : 885. Spiral Matrix III :   : 無聊的模擬題 只是想分享一下繞圈圈的四個方向有幾種寫法 :   :   : 2. :   : dx = [0, 1, 0, -1] : dy = [1, 0, -1, 0] :   題目: 讓你在一個矩陣裡面從指定的地方開始繞圈圈 超過矩陣就繼續走 然後問你經過哪裡了 思路: 模擬 我讓jiwp去當d x或y 因為我想要jiwp的 x或y染色體 你懂我意思嗎 ```cpp class Solution { public: vector<vector<int>> spiralMatrixIII(int rows, int cols, int rStart, int cSta rt) { int n = 1 ; int len = 1; int all = rows*cols; vector<vector<int>> paper; int r = rStart; int c = cStart; vector<int> ji = {0,1,0,-1}; vector<int> wp = {1,0,-1,0}; paper.push_back({r,c}); while(n<all) { for(int k = 0 ; k < 4 ; k ++) { for(int i = 0 ; i < len ; i ++) { r+=ji[k]; c+=wp[k]; if(r>=0&&r<rows&&c>=0&&c<cols) { paper.push_back({r,c}); n++; } } if(k &1)len++; } } return paper; } }; ``` -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 61.230.129.159 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1723087800.A.15B.html
SydLrio: 你有什麼用 08/08 11:31
JIWP: 你有什麼用 08/08 11:42