Make writing a habit together! This is the fourth day of my participation in the “Gold Digging Day New Plan · April More text Challenge”. Click here for more details.
📢 preface
🚀 Algorithm 🚀 |
- 🌲 punch in an algorithm every day, which is not only a learning process, but also a sharing process 😜
- 🌲 tip: the programming languages used in this column are C# and Java
- 🌲 to maintain a state of learning every day, let us work together to become a god of algorithms 🧐!
- 🌲 today is the 101st day of continuous clocking of force button algorithm 🎈!
🚀 Algorithm 🚀 |
🌲 Example: Reshape the matrix
In MATLAB, there is a very useful function 0 that remolds an M x N matrix into another new matrix of a different size (R x C), but preserves its original data.
You are given an M x N matrix represented by a two-dimensional array mat, and two positive integers r and C, representing the number of rows and columns of the matrix you want to reconstruct.
The reconstructed matrix requires that all elements of the original matrix be filled in the same row traversal order.
0 0 If the 0 0 operation with the given parameters is 0 0 0 0 0 Otherwise, output the original matrix.
Example 1:
Enter: mat = [[1.2], [3.4]], r = 1, c = 4Output: [[1.2.3.4]]
Copy the code
Example 2:
Enter: mat = [[1.2], [3.4]], r = 2, c = 4Output: [[1.2], [3.4]]
Copy the code
Tip:
- m == mat.length
- n == mat[i].length
- 1 <= m, n <= 100
- -1000 <= mat[i][j] <= 1000
- 1 <= r, c <= 300
🌻C# method: one-dimensional representation of a two-dimensional array
Code:
public class Solution {
public int[][] MatrixReshape(int[][] mat, int r, int c) {
int m = mat.Length, n = mat[0].Length;
if(m * n ! = r * c)return mat;
int[][] ans = new int[r][];
for (int i = 0; i < r; ++i) {
ans[i] = new int[c];
}
for (int x = 0; x < m * n; ++x) {
ans[x / c][x % c] = mat[x / n][x % n];
}
returnans; }}Copy the code
The execution result
By execution time:76Ms, in all C# beat 66.14% of users in submissionMemory consumption:36.9MB, in all CBeat 5.70% of users in # submission
Copy the code
🌻Java method: one-dimensional representation of a two-dimensional array
Thinking analytical Code:
class Solution {
public int[][] matrixReshape(int[][] nums, int r, int c) {
int m = nums.length;
int n = nums[0].length;
if(m * n ! = r * c) {return nums;
}
int[][] ans = new int[r][c];
for (int x = 0; x < m * n; ++x) {
ans[x / c][x % c] = nums[x / n][x % n];
}
returnans; }}Copy the code
The execution result
By execution time:0Ms, beat out all Java commits99.41% user memory consumption:39.4MB, beat out all Java commits38.53% of the userCopy the code
Complexity analysis
Time complexity: O(rc)1)
Copy the code
💬 summary
- Today is the 107th day of buckle algorithm clocking!
- The article USES the
C#
andJava
Two programming languages to solve the problem - Some methods are also written by the god of reference force buckle, and they are also shared while learning, thanks again to the algorithm masters
- That’s the end of today’s algorithm sharing, see you tomorrow!