diff options
author | Jack Sangdahl <[email protected]> | 2025-03-10 00:36:13 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2025-03-10 00:36:13 -0600 |
commit | 7c60dee83b436e8e8a667bf9f191beced8de4fc3 (patch) | |
tree | eb7a24ee2067f92245c57bb118e85a1baf15df50 /other/01_matrix.cpp | |
parent | 5d7cdc215e4ba1ec4cbb24c084cccb5e5e641468 (diff) | |
download | leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.tar.gz leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.zip |
chore: move cpp and rust code to directory
Diffstat (limited to 'other/01_matrix.cpp')
-rw-r--r-- | other/01_matrix.cpp | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/other/01_matrix.cpp b/other/01_matrix.cpp new file mode 100644 index 0000000..f6d9c2d --- /dev/null +++ b/other/01_matrix.cpp @@ -0,0 +1,34 @@ +#include "leetcode.h" + +class Solution { +public: + vector<vector<int>> updateMatrix(vector<vector<int>> &mat) { + int m = mat.size(), n = mat[0].size(); + vector<vector<int>> ans(m, vector<int>(n, INT_MAX)); + queue<pair<int, int>> qp; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (mat[i][j] == 0) { + ans[i][j] = 0; + qp.push({i, j}); + } + } + } + vector<pair<int, int>> vp{{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; + while (!qp.empty()) { + auto curr = qp.front(); + qp.pop(); + int currX = curr.first, currY = curr.second; + for (auto dirr : vp) { + int x = currX + dirr.first, y = currY + dirr.second; + if (x >= 0 && x < m && y >= 0 && y < n) { + if (ans[x][y] > ans[currX][currY] + 1) { + ans[x][y] = ans[currX][currY] + 1; + qp.push({x, y}); + } + } + } + } + return ans; + } +}; |