[Swift]LeetCode566. 重塑矩陣 | Reshape the Matrix

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-rmjarvpm-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new one with different size but keep its original data.git

You're given a matrix represented by a two-dimensional array, and two positive integers r and c representing the row number and column number of the wanted reshaped matrix, respectively.github

The reshaped matrix need to be filled with all the elements of the original matrix in the same row-traversing order as they were.數組

If the 'reshape' operation with given parameters is possible and legal, output the new reshaped matrix; Otherwise, output the original matrix.微信

Example 1:app

Input: 
nums = 
[[1,2],
 [3,4]]
r = 1, c = 4
Output: 
[[1,2,3,4]]
Explanation:
The row-traversing of nums is [1,2,3,4]. The new reshaped matrix is a 1 * 4 matrix, fill it row by row by using the previous list.

 Example 2:ide

Input: 
nums = 
[[1,2],
 [3,4]]
r = 2, c = 4
Output: 
[[1,2],
 [3,4]]
Explanation:
There is no way to reshape a 2 * 2 matrix to a 2 * 4 matrix. So output the original matrix.

 Note:函數

  1. The height and width of the given matrix is in range [1, 100].
  2. The given r and c are all positive.

在MATLAB中,有一個很是有用的函數 reshape,它能夠將一個矩陣重塑爲另外一個大小不一樣的新矩陣,但保留其原始數據。post

給出一個由二維數組表示的矩陣,以及兩個正整數rc,分別表示想要的重構的矩陣的行數和列數。spa

重構後的矩陣須要將原始矩陣的全部元素以相同的行遍歷順序填充。

若是具備給定參數的reshape操做是可行且合理的,則輸出新的重塑矩陣;不然,輸出原始矩陣。

示例 1:

輸入: 
nums = 
[[1,2],
 [3,4]]
r = 1, c = 4
輸出: 
[[1,2,3,4]]
解釋:
行遍歷nums的結果是 [1,2,3,4]。新的矩陣是 1 * 4 矩陣, 用以前的元素值一行一行填充新矩陣。

示例 2:

輸入: 
nums = 
[[1,2],
 [3,4]]
r = 2, c = 4
輸出: 
[[1,2],
 [3,4]]
解釋:
沒有辦法將 2 * 2 矩陣轉化爲 2 * 4 矩陣。 因此輸出原矩陣。

注意:

  1. 給定矩陣的寬和高範圍在 [1, 100]。
  2. 給定的 r 和 c 都是正數。

172ms

 1 class Solution {
 2     func matrixReshape(_ nums: [[Int]], _ r: Int, _ c: Int) -> [[Int]] {
 3         //建立一個二維數組      
 4         var res:[[Int]] = [[Int]](repeating: [Int](repeating: 0, count: c), count: r)
 5         let len:Int = nums.count
 6         if len == 0 || r * c != len * nums[0].count
 7         {
 8             return nums
 9         }
10         var count:Int = 0
11         for i in 0..<len
12         {
13             for j in 0..<nums[0].count
14             {
15                 res[count / c][count % c] = nums[i][j]
16                 count++
17             }    
18         }
19         return res
20     }
21 }
22 
23 /*擴展Int類,實現自增++、自減--運算符*/
24 extension Int{
25     //++前綴:先自增再執行表達示
26     static prefix func ++(num:inout Int) -> Int {
27         //輸入輸出參數num
28         num += 1
29         //返回加1後的數值
30         return num
31     }
32     //後綴++:先執行表達式後再自增
33     static postfix func ++(num:inout Int) -> Int {
34         //輸入輸出參數num
35         let temp = num
36         //num加1
37         num += 1
38         //返回加1前的數值
39         return temp
40     }
41     //--前綴:先自減再執行表達示
42     static prefix func --(num:inout Int) -> Int {
43         //輸入輸出參數num
44         num -= 1
45         //返回減1後的數值
46         return num
47     }
48     //後綴--:先執行表達式後再自減
49     static postfix func --(num:inout Int) -> Int {
50         //輸入輸出參數num
51         let temp = num
52         //num減1
53         num -= 1
54          //返回減1前的數值
55         return temp
56     }
57 }

36ms

 1 class Solution {
 2     func matrixReshape(_ nums: [[Int]], _ r: Int, _ c: Int) -> [[Int]] {
 3         if c * r != nums.count * nums[0].count{
 4             return nums
 5         }
 6         var res = Array(repeating: Array(repeating: 0,count: c) ,count: r)
 7         var count = 0
 8         for i in 0..<nums.count{
 9             for j in 0..<nums[0].count{
10                 res[count / c][count % c] = nums[i][j]
11                 count += 1
12             }
13         }
14         
15         return res
16     }
17 }

36ms

 1 class Solution {
 2     func matrixReshape(_ nums: [[Int]], _ r: Int, _ c: Int) -> [[Int]] {
 3         guard !nums.isEmpty else {
 4             return [[]]
 5         }
 6     
 7         guard nums.count * nums[0].count == r*c else {
 8             return nums
 9         }
10     
11         var result = [[Int]](repeating: [Int](repeating: 0, count: c), count: r)
12         var index = 0
13         let arr = nums.flatMap {$0}
14         for row in 0..<r {
15             for col in 0..<c {
16                 result[row][col] = arr[index]
17                 index += 1
18             }
19         }
20     
21         return result
22     }
23 }

40ms

 1 class Solution {
 2     func matrixReshape(_ nums: [[Int]], _ r: Int, _ c: Int) -> [[Int]] {
 3         let row = nums.count
 4         let column = nums[0].count
 5         
 6         if (row == r && column == c) || row * column != r * c {
 7             return nums
 8         }
 9         
10         var result = [[Int]]()
11         var index = 0
12         for _ in 0..<r {
13             var newRow = [Int]()
14             for _ in 0..<c {
15                 newRow.append(nums[index / column][index % column])
16                 index += 1
17             }
18             result.append(newRow)
19         }
20         return result
21     }
22 }

40ms

 1 class Solution {
 2     func matrixReshape(_ nums: [[Int]], _ r: Int, _ c: Int) -> [[Int]] {
 3         let rows = nums.count
 4         let cols = nums[0].count
 5         if r*c != rows*cols {
 6             return nums
 7         }
 8         var numbers = [Int]()
 9         for row in nums {
10             for item in row {
11                 numbers.append(item)
12             }
13         }
14         var reshaped = [[Int]]()
15         var rIndex = 0
16         var cIndex = 0
17         var row = [Int]()
18         numbers.forEach{ item in 
19             if cIndex == c {
20                 cIndex = 0
21                 rIndex += 1
22                 reshaped.append(row)
23                 row.removeAll()
24             }
25             row.append(item)
26             cIndex += 1
27         }
28         reshaped.append(row)
29         return reshaped
30     }
31 }

44ms

 1 class Solution {
 2     func matrixReshape(_ nums: [[Int]], _ r: Int, _ c: Int) -> [[Int]] {
 3         
 4         guard nums.count * nums[0].count == r * c else {
 5             return nums
 6         }
 7         
 8         var res = [[Int]]()
 9         var tmp = [Int]()   
10         var tmpCount = 0
11         
12         for num in nums {        
13             for n in num {             
14                 tmp.append(n)         
15                 if tmp.count >= c {
16                     res.append(tmp)
17                     tmp.removeAll()
18                 }
19             }
20         }
21         
22         return res
23     }
24 }

44ms

 1 class Solution {
 2     func matrixReshape(_ nums: [[Int]], _ r: Int, _ c: Int) -> [[Int]] {
 3         let numbers = Array(nums.joined())
 4         if numbers.count != r*c {
 5             return nums
 6         }
 7 
 8         var reshaped = Array(repeating:[Int](), count:r)
 9         for count in 0..<numbers.count {
10             reshaped[count/c].append(numbers[count])
11         }
12         return reshaped
13     }
14 }

48ms

 1 class Solution {
 2     func matrixReshape(_ nums: [[Int]], _ r: Int, _ c: Int) -> [[Int]] {
 3         let count = nums.count * nums[0].count;
 4         if count != r * c {
 5             return nums;
 6         }
 7         var result = Array<[Int]>()
 8         var temp = Array<Int>()
 9         
10         for array in nums {
11             for num in array {
12                 temp.append(num)
13                 if temp.count >= c {
14                     result.append(temp);
15                     temp.removeAll()
16                 }
17             }
18         }
19         return result;
20     }
21 }

48ms

 1 class Solution {
 2     func matrixReshape(_ nums: [[Int]], _ r: Int, _ c: Int) -> [[Int]] {
 3         var reshapedMatrix = [[Int]]()
 4         
 5         for _ in stride(from: 0, to: r, by: 1) {
 6             var tempRow = [Int]()
 7             for _ in stride(from: 0, to: c, by: 1) {
 8                 tempRow.append(0)
 9             }
10             reshapedMatrix.append(tempRow)
11         }
12         
13         let matrixSize = nums.count * nums[0].count
14         let reshapedMatrixSize = r * c
15         if matrixSize != reshapedMatrixSize {
16             return nums
17         }
18         for i in stride(from: 0, to: reshapedMatrixSize, by: 1) {
19             reshapedMatrix[i/c][i%c] = nums[i/nums[0].count][i%nums[0].count]
20         }
21         return reshapedMatrix
22     }
23 }

52ms

 1 class Solution {
 2     func matrixReshape(_ nums: [[Int]], _ r: Int, _ c: Int) -> [[Int]] {
 3          if nums.count * nums[0].count != r * c {
 4             return nums
 5         }
 6         var temp = [Int](),result2 = [[Int]]()
 7         for itemArray in nums {
 8             temp += itemArray
 9         }
10         for i in 0..<r {
11             result2.append([Int](temp[i*c..<i*c+c]))
12         }
13         return result2
14     }
15 }

76ms

 1 class Solution {
 2     func matrixReshape(_ nums: [[Int]], _ r: Int, _ c: Int) -> [[Int]] {
 3         guard nums.count * nums.first!.count == r * c else {
 4             return nums
 5         }
 6         
 7         var newA = [[Int]]()
 8         var tmpA = [Int]()
 9         for subA in nums {
10             tmpA += subA
11         }
12         
13         for index in stride(from: 0, to: r * c, by: c) {
14             newA.append(Array(tmpA[index..<index+c]))
15         }
16         
17         return newA
18     }
19 }

84ms

 1 class Solution {
 2     func matrixReshape(_ nums: [[Int]], _ r: Int, _ c: Int) -> [[Int]] {
 3         var new: [Int] = []
 4         for temp: [Int] in nums {
 5             for i: Int in temp {
 6                 new.append(i)
 7             }
 8         }
 9         if r * c > new.count {
10             return nums
11         }
12         var sq: [[Int]] = []
13         for j in 1...r {
14             var tempSq: [Int] = []
15             for a in 0..<c {
16                 tempSq.append(new[(j-1)*c+a])
17             }
18             sq.append(tempSq)
19         }
20         return sq
21     }
22 }
相關文章
相關標籤/搜索