-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.java
More file actions
38 lines (35 loc) · 875 Bytes
/
Copy pathSolution.java
File metadata and controls
38 lines (35 loc) · 875 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import java.util.LinkedList;
import java.util.Queue;
public class Solution
{
public int[][] matrixReshape(int[][] nums, int r, int c)
{
if (nums == null || nums.length == 0 || nums[0].length == 0)
{
return nums;
}
int rows = nums.length;
int columns = nums[0].length;
if (rows * columns != r * c)
{
return nums;
}
int[][] matrix = new int[r][c];
Queue<Integer> queue = new LinkedList<>();
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
queue.add(nums[i][j]);
}
}
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
matrix[i][j] = queue.poll();
}
}
return matrix;
}
}