leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

commit ef7cebfb49bc6b4d1a86f14025c9e270a64530b3
parent dd8db0d2592ad603c959758eee9fe52d3f655f1b
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Wed, 25 Jan 2023 12:15:20 +0100

Data Structure I: Day 4

Diffstat:
AProblems/0566.cpp | 20++++++++++++++++++++
MREADME.md | 1+
2 files changed, 21 insertions(+), 0 deletions(-)

diff --git a/Problems/0566.cpp b/Problems/0566.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + vector<vector<int>> matrixReshape(vector<vector<int>> &mat, int r, int c) { + int n = mat.size(), m = mat[0].size(); + if (m * n != r * c) return mat; + vector<vector<int>> nmat(r, vector<int>(c)); + + int x = 0, y = 0; + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + nmat[x][y] = mat[i][j]; + y++; + x += y / c; + y %= c; + } + } + + return nmat; + } +}; diff --git a/README.md b/README.md @@ -186,6 +186,7 @@ for solving problems. | 0559 | Easy | [Maximum Depth of N-ary Tree](Problems/0559.cpp) | | 0561 | Easy | [Array Partition](Problems/0561.cpp) | | 0563 | Easy | [Binary Tree Tilt](Problems/0563.cpp) | +| 0566 | Easy | [Reshape the Matrix](Problems/0566.cpp) | | 0572 | Easy | [Subtree of Another Tree](Problems/0572.cpp) | | 0589 | Easy | [N-ary Tree Preorder Traversal](Problems/0589.cpp) | | 0590 | Easy | [N-ary Tree Postorder Traversal](Problems/0590.cpp) |