Re: [閒聊] 每日leetcode
※ 引述《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
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 674 之 1548 篇):