精華區beta Marginalman 關於我們 聯絡資訊
※ 引述《JIWP (神楽めあ的錢包)》之銘言: : 885. Spiral Matrix III 昨天的 醒來發現才7. 就先寫掉 三層迴圈 腳踏實地 看起來超笨== 應該是可以把很多外面的路都直接pass掉 懶 腿了 class Solution { public: vector<vector<int>> spiralMatrixIII(int rows, int cols, int rs, int cs) { int num = rows * cols; vector<vector<int>> res; res.push_back({rs, cs}); num--; int step = 0; vector<int> dir{1, 0, -1, 0, 1}; //{0, -1, 0 1} // row[i] : 1 0 -1 0 // col[i+1]: 0 -1 0 1 //cout << "num = " << num << '\n'; while(num > 0){ step += 2; rs--; cs++; //cout << rs << " " << cs << '\n'; for(int i = 0; i < 4; i++){ // 4 dir for(int j = 0; j < step; j++){ rs += dir[i]; cs += dir[i+1]; //cout << rs << ' ' << cs << "; "; if((rs >= 0) and (rs < rows) and (cs >= 0) and (cs < cols)){ res.push_back({rs, cs}); //cout << "PPPPPPP\n"; num--; if(num == 0) break; } } //cout << '\n'; if(num == 0) break; } } return res; } }; -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 123.205.121.194 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1723163624.A.E62.html
DJYOMIYAHINA: 大濕 08/09 09:20
oin1104: 大師 08/09 10:27