Re: [閒聊] 每日leetcode

看板Marginalman作者 (6B)時間1年前 (2024/08/09 08:33), 編輯推噓1(101)
留言2則, 2人參與, 1年前最新討論串674/1548 (看更多)
※ 引述《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

08/09 09:20, 1年前 , 1F
大濕
08/09 09:20, 1F

08/09 10:27, 1年前 , 2F
大師
08/09 10:27, 2F
文章代碼(AID): #1cjMFevY (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1cjMFevY (Marginalman)