Skip to content

Commit 8e8ef00

Browse files
committed
Reshape the matrix.
1 parent e81e547 commit 8e8ef00

File tree

4 files changed

+62
-0
lines changed

4 files changed

+62
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
In MATLAB, there is a handy function called reshape which can reshape an m x n matrix into a new one with a different size r x c keeping its original data.
3+
4+
You are given an m x n matrix mat and two integers r and c representing the number of rows and the number of columns of the wanted reshaped matrix.
5+
6+
The reshaped matrix should be filled with all the elements of the original matrix in the same row-traversing order as they were.
7+
8+
If the reshape operation with given parameters is possible and legal, output the new reshaped matrix; Otherwise, output the original matrix.
9+
10+
 
11+
12+
Example 1:
13+
Input: mat = [[1,2],[3,4]], r = 1, c = 4
14+
Output: [[1,2,3,4]]
15+
16+
Example 2:
17+
Input: mat = [[1,2],[3,4]], r = 2, c = 4
18+
Output: [[1,2],[3,4]]
19+
 
20+
21+
Constraints:
22+
- m == mat.length
23+
- n == mat[i].length
24+
- 1 <= m, n <= 100
25+
- -1000 <= mat[i][j] <= 1000
26+
- 1 <= r, c <= 300
27+
*/
28+
class Solution {
29+
func matrixReshape(_ mat: [[Int]], _ r: Int, _ c: Int) -> [[Int]] {
30+
var total = [Int]()
31+
for arr in mat {
32+
for e in arr {
33+
total.append(e)
34+
}
35+
}
36+
if r * c != total.count { return mat }
37+
var result = [[Int]]()
38+
for (i, e) in total.enumerated() {
39+
var arr = i % c == 0 ? [Int]() : result.popLast()!
40+
arr.append(e)
41+
result.append(arr)
42+
}
43+
return result
44+
}
45+
}
46+
47+
let s = Solution()
48+
let r = s.matrixReshape([[1,2],[3,4]], 2, 4)
49+
print(r)
50+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

Easy/566.Reshape the Matrix.playground/playground.xcworkspace/contents.xcworkspacedata

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@
111111
107. [Reverse Words in a String III](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/557.Reverse%20Words%20in%20a%20String%20III.playground/Contents.swift)
112112
108. [Maximum Depth of N-ary Tree](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/559.Maximum%20Depth%20of%20N-ary%20Tree.playground/Contents.swift)
113113
109. [Array Partition](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/561.Array%20Partition.playground/Contents.swift)
114+
110. [Reshape the Matrix](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/566.Reshape%20the%20Matrix.playground/Contents.swift)
114115

115116
#### Medium
116117

0 commit comments

Comments
 (0)