zoukankan      html  css  js  c++  java
  • [Swift]LeetCode566. 重塑矩阵 | Reshape the Matrix

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
    ➤微信公众号:山青咏芝(shanqingyongzhi)
    ➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
    ➤GitHub地址:https://github.com/strengthen/LeetCode
    ➤原文地址:https://www.cnblogs.com/strengthen/p/9843320.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.

    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.

    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:

    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:

    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,它可以将一个矩阵重塑为另一个大小不同的新矩阵,但保留其原始数据。

    给出一个由二维数组表示的矩阵,以及两个正整数rc,分别表示想要的重构的矩阵的行数和列数。

    重构后的矩阵需要将原始矩阵的所有元素以相同的行遍历顺序填充。

    如果具有给定参数的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 }
  • 相关阅读:
    第一章 第二节逻辑代数基础
    第一章 第一节数制与编码
    Altium Designer多原理图、PCB更新处理
    AD添加LOGO的方法
    XML中<beans>属性
    程序员值得学习的技术博客
    设计模式
    js分页实例
    Java构造和解析Json数据的方法
    H5+ 移动app学习之三 App离线存储
  • 原文地址:https://www.cnblogs.com/strengthen/p/9843320.html
Copyright © 2011-2022 走看看