zoukankan      html  css  js  c++  java
  • [Swift]LeetCode542. 01 矩阵 | 01 Matrix

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
    ➤微信公众号:山青咏芝(shanqingyongzhi)
    ➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
    ➤GitHub地址:https://github.com/strengthen/LeetCode
    ➤原文地址:https://www.cnblogs.com/strengthen/p/10409073.html 
    ➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
    ➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

    Given a matrix consists of 0 and 1, find the distance of the nearest 0 for each cell.

    The distance between two adjacent cells is 1.

    Example 1: 
    Input:

    0 0 0
    0 1 0
    0 0 0
    

    Output:

    0 0 0
    0 1 0
    0 0 0 

    Example 2: 
    Input:

    0 0 0
    0 1 0
    1 1 1
    

    Output:

    0 0 0
    0 1 0
    1 2 1 

    Note:

    1. The number of elements of the given matrix will not exceed 10,000.
    2. There are at least one 0 in the given matrix.
    3. The cells are adjacent in only four directions: up, down, left and right.

    给定一个由 0 和 1 组成的矩阵,找出每个元素到最近的 0 的距离。

    两个相邻元素间的距离为 1 。

    示例 1: 
    输入:

    0 0 0
    0 1 0
    0 0 0
    

    输出:

    0 0 0
    0 1 0
    0 0 0
    

    示例 2: 
    输入:

    0 0 0
    0 1 0
    1 1 1
    

    输出:

    0 0 0
    0 1 0
    1 2 1
    

    注意:

    1. 给定矩阵的元素个数不超过 10000。
    2. 给定矩阵中至少有一个元素是 0。
    3. 矩阵中的元素只在四个方向上相邻: 上、下、左、右。

    644ms

     1 class Solution {
     2     func updateMatrix(_ matrix: [[Int]]) -> [[Int]] {
     3         let n = matrix.count
     4         let m = matrix[0].count
     5         let maxDis = n + m
     6         var res: [[Int]] = Array(repeating: Array(repeating: 0, count: m), count: n)
     7             
     8         for i in 0 ..< n {
     9             for j in 0 ..< m {
    10                 if matrix[i][j] == 1 {
    11                     let top = (i > 0) ? res[i - 1][j] : maxDis
    12                     let l = (j > 0) ? res[i][j - 1] : maxDis
    13                     res[i][j] = min(maxDis, min(top, l) + 1)
    14                 }
    15             }
    16         }
    17         
    18         for i in (0 ..< n).reversed() {
    19             for j in (0 ..< m).reversed() {
    20                 if matrix[i][j] == 1 {
    21                     let bottom = (i < n - 1) ? res[i + 1][j] : maxDis 
    22                     let r = (j < m - 1) ? res[i][j + 1] : maxDis
    23                     res[i][j] = min(res[i][j], min(bottom, r) + 1)
    24                 }
    25             }
    26         }
    27         
    28         return res
    29     }
    30 }

    Runtime: 652 ms
    Memory Usage: 20 MB
     1 class Solution {
     2     func updateMatrix(_ matrix: [[Int]]) -> [[Int]] {
     3         var m:Int = matrix.count
     4         var n:Int = matrix[0].count
     5         var res:[[Int]] = [[Int]](repeating:[Int](repeating:Int.max - 1,count:n),count:m)
     6         for i in 0..<m
     7         {
     8             for j in 0..<n
     9             {
    10                 if matrix[i][j] == 0
    11                 {
    12                     res[i][j] = 0
    13                 }
    14                 else
    15                 {
    16                     if i > 0
    17                     {
    18                          res[i][j] = min(res[i][j], res[i - 1][j] + 1)
    19                     }
    20                     if j > 0
    21                     {
    22                         res[i][j] = min(res[i][j], res[i][j - 1] + 1)
    23                     }
    24                 }
    25             }
    26         }
    27         for i in stride(from:m - 1,through:0,by:-1)
    28         {
    29             for j in stride(from:n - 1,through:0,by:-1)
    30             {
    31                 if res[i][j] != 0 && res[i][j] != 1
    32                 {
    33                     if i < m - 1
    34                     {
    35                         res[i][j] = min(res[i][j], res[i + 1][j] + 1)
    36                     }
    37                     if j < n - 1
    38                     {
    39                         res[i][j] = min(res[i][j], res[i][j + 1] + 1)
    40                     }
    41                 }               
    42             }
    43         }
    44         return res
    45     }
    46 }

    Runtime: 676 ms

    Memory Usage: 20.4 MB
     1 class Solution {
     2 func updateMatrix(_ matrix: [[Int]]) -> [[Int]] {
     3 
     4     struct Node {
     5         var value = 0;
     6         var i = 0;
     7         var j = 0;
     8     }
     9 
    10     var tempArr = matrix;
    11 
    12     var nodeArr = [Node]()
    13     var lastNum = 0;
    14     for i in 0..<matrix.count {
    15         for j in 0..<matrix[i].count {
    16             var node: Node = Node.init();
    17             node.i = i;
    18             node.j = j;
    19             if matrix[i][j] == 0 {
    20                 node.value = 0;
    21                 nodeArr.append(node)
    22             } else {
    23                 node.value = -1;
    24                 lastNum = lastNum + 1;
    25                 tempArr[i][j] = -1;
    26             }
    27         }
    28     }
    29 
    30     while lastNum > 0 {
    31 
    32         for k in 0..<nodeArr.count {
    33             let i = nodeArr[k].i;
    34             let j = nodeArr[k].j;
    35             //上下左右,并且不是-1
    36             if i > 0 && tempArr[i-1][j] == -1 {
    37                 tempArr[i-1][j] = nodeArr[k].value + 1;
    38                 let node = Node.init(value: tempArr[i-1][j], i: i-1, j: j);
    39                 nodeArr.append(node);
    40                 lastNum = lastNum - 1;
    41             }
    42             if i + 1 < tempArr.count && tempArr[i+1][j] == -1 {
    43                 tempArr[i+1][j] = nodeArr[k].value + 1;
    44                 let node = Node.init(value: tempArr[i+1][j], i: i+1, j: j);
    45                 nodeArr.append(node);
    46                 lastNum = lastNum - 1;
    47             }
    48             if j > 0 && tempArr[i][j-1] == -1 {
    49                 tempArr[i][j-1] = nodeArr[k].value + 1;
    50                 let node = Node.init(value: tempArr[i][j-1], i: i, j: j-1);
    51                 nodeArr.append(node);
    52                 lastNum = lastNum - 1;
    53             }
    54             if j + 1 < tempArr[0].count && tempArr[i][j+1] == -1 {
    55                 tempArr[i][j+1] = nodeArr[k].value + 1;
    56                 let node = Node.init(value: tempArr[i][j+1], i: i, j: j+1);
    57                 nodeArr.append(node);
    58                 lastNum = lastNum - 1;
    59             }
    60         }
    61     }
    62     return tempArr;
    63   }
    64 }

    796ms

     1 class Solution {
     2     func updateMatrix(_ matrix: [[Int]]) -> [[Int]] {
     3 
     4         var matrix = matrix
     5 
     6         for row in 0..<matrix.count {
     7             for column in 0..<matrix[row].count where matrix[row][column] != 0 {                
     8                 var minDistance = Int.max - 1
     9                 
    10                 //up
    11                 if row > 0 {
    12                     minDistance = min(minDistance, matrix[row-1][column])
    13                 }     
    14                   
    15                 //left
    16                 if column > 0 {
    17                     minDistance = min(minDistance, matrix[row][column-1])
    18                 }
    19                 
    20                 matrix[row][column] = minDistance + 1
    21             }
    22         }   
    23         
    24         
    25         for row in (0..<matrix.count).reversed() {
    26             for column in (0..<matrix[row].count).reversed() where matrix[row][column] != 0 {                
    27                 var minDistance = matrix[row][column]
    28             
    29                 //down                
    30                 if row < matrix.count - 1 {
    31                     minDistance = min(minDistance, (matrix[row+1][column] + 1))    
    32                 }
    33                 
    34                 //right
    35                 if column < matrix[row].count - 1 {
    36                     minDistance = min(minDistance, (matrix[row][column+1] + 1))
    37                 }
    38                 
    39                 matrix[row][column] = minDistance
    40             }
    41         }  
    42                                         
    43         return matrix        
    44     }
    45 }

    976ms

     1 class Solution {
     2     func updateMatrix(_ matrix: [[Int]]) -> [[Int]] {
     3         var res = matrix
     4         if matrix.count == 0 || matrix[0].count == 0 {
     5             return res
     6         }
     7         let dx = [0,0,1,-1]
     8         let dy = [1,-1,0,0]
     9         let n = matrix.count
    10         let m = matrix[0].count
    11         var queue = [[Int]]()
    12         for i in 0 ..< n {
    13             for j in 0 ..< m {
    14                 if res[i][j] != 0 {
    15                     res[i][j] = Int.max
    16                 }else {
    17                     queue.append([i, j])
    18                 }
    19             }
    20         }
    21         
    22         while !queue.isEmpty {
    23             let cur = queue.removeFirst()
    24             for i in 0 ... 3 {
    25                 let nx = dx[i] + cur[0]
    26                 let ny = dy[i] + cur[1]
    27                 if nx < res.count && ny < res[0].count && nx >= 0 && ny >= 0 {
    28                     if res[nx][ny] > res[cur[0]][cur[1]] + 1 {
    29                         res[nx][ny] = res[cur[0]][cur[1]] + 1 
    30                         queue.append([nx, ny])
    31                     }
    32                 }
    33             }
    34         }
    35         
    36         return res
    37     }
    38 }

    1004ms

     1 class Solution {
     2     func updateMatrix(_ matrix: [[Int]]) -> [[Int]] {
     3         var res = matrix
     4         if matrix.count == 0 || matrix[0].count == 0 {
     5             return res
     6         }
     7         let n = matrix.count
     8         let m = matrix[0].count
     9         var visited = [[Bool]](repeating: [Bool](repeating: false, count: m), count: n)
    10         
    11         for i in 0 ..< n {
    12             for j in 0 ..< m {
    13                 if res[i][j] == 0 {
    14                     fill(&res, &visited, i, j , 0)
    15                 }
    16             }
    17         }
    18         return res
    19     }
    20     
    21     
    22     func fill(_ res: inout [[Int]], _ visited: inout [[Bool]], _ x: Int, _ y: Int, _ count :Int) {
    23         visited[x][y] = true
    24         let dx = [0,0,-1,1]
    25         let dy = [1,-1,0,0]
    26         res[x][y] = count
    27         for i in 0 ... 3{
    28             let nx = dx[i] + x
    29             let ny = dy[i] + y
    30             if nx >= 0 && ny >= 0 && nx < res.count && ny < res[0].count && res[nx][ny] != 0{
    31                 if count > 1 && hasNeiborZero(res, [nx, ny]){
    32                     continue
    33                 }
    34                 if !visited[nx][ny] || res[nx][ny] > count + 1 {
    35                     fill(&res, &visited, nx, ny, count + 1)
    36                 }
    37             }
    38         }
    39         
    40     }
    41     func hasNeiborZero(_ maze:[[Int]], _ index:[Int]) -> Bool{
    42         let x = index[0]
    43         let y = index[1]
    44         if x > 0 && maze[x - 1][y] == 0 {
    45             return true
    46         }
    47         if y > 0 && maze[x][y - 1] == 0 {
    48             return true
    49         }
    50         if y < maze[0].count - 1 && maze[x][y + 1] == 0 {
    51             return true
    52         }
    53         if x < maze.count - 1 && maze[x + 1][y] == 0 {
    54             return true
    55         }
    56         return false
    57         
    58     }
    59 }

    1068ms

     1 class Solution {
     2     func updateMatrix(_ matrix: [[Int]]) -> [[Int]] {
     3         let dirs: [(Int, Int)] = [(0, -1), (-1, 0), (1, 0), (0, 1)]
     4         let n = matrix.count
     5         let m = matrix[0].count
     6         var queue: [(Int, Int)] = []
     7         var res: [[Int]] = Array(repeating: Array(repeating: 0, count: m), count: n)
     8         
     9         for i in 0 ..< n {
    10             for j in 0 ..< m {
    11                 if matrix[i][j] == 0 {
    12                     queue.append((i, j))
    13                 } else {
    14                     res[i][j] = Int.max
    15                 }
    16             }
    17         }
    18         
    19         while !queue.isEmpty {
    20             let curr = queue.removeFirst()
    21             for d in dirs {
    22                 let x = curr.0 + d.0
    23                 let y = curr.1 + d.1
    24                 if x < 0 || x >= n || y < 0 || y >= m || res[x][y] <= res[curr.0][curr.1] + 1 {
    25                     continue
    26                 }
    27                 res[x][y] = res[curr.0][curr.1] + 1
    28                 queue.append((x, y))
    29             }
    30         }
    31         return res
    32     }
    33 }

    1124ms

     1 class Solution {
     2     func updateMatrix(_ matrix: [[Int]]) -> [[Int]] {
     3         var matrix = matrix
     4         if matrix == nil || matrix.count == 0 || matrix[0] == nil || matrix[0].count == 0 {
     5             return [[Int]]()
     6         }
     7         
     8         let m = matrix.count
     9         let n = matrix[0].count
    10         
    11         var temp = Array(repeating:0,count:n)
    12         var result = Array(repeating:temp,count:m)
    13         var queue = [Point]()
    14         for i in 0 ..< m{
    15             for j in 0 ..< n{
    16                 
    17                 if matrix[i][j] == 0{
    18                     result[i][j] = 0
    19                     queue.append(Point(i,j,0))
    20                 }else{
    21                     result[i][j] = Int.max
    22                 }
    23             }
    24         }
    25         
    26         
    27         while !queue.isEmpty{
    28             
    29             
    30             let point = queue.removeFirst()
    31             
    32             let deltaX = [0,0,1,-1]
    33             let deltaY = [1,-1,0,0]
    34             
    35             
    36             for i in 0 ..< 4 {
    37                 
    38                 if point.x + deltaX[i] >= 0 && point.x + deltaX[i] < m && point.y + deltaY[i] >= 0 && 
    39                    point.y + deltaY[i] < n && result[point.x+deltaX[i]][point.y+deltaY[i]] == Int.max{
    40                        result[point.x+deltaX[i]][point.y+deltaY[i]] = point.level + 1
    41                        
    42                        queue.append(Point(point.x+deltaX[i],point.y+deltaY[i],point.level+1))
    43                    } 
    44             }
    45         }
    46         
    47         return result
    48     }
    49 }
    50 
    51 class Point{
    52     var x = 0 
    53     var y = 0 
    54     var level = 0 
    55     init(_ x: Int, _ y:Int, _ level:Int){
    56         self.x = x
    57         self.y = y
    58         self.level = level
    59     }
    60 }
  • 相关阅读:
    SourceInsight宏插件3(非常好用,强力推荐)
    SourceInsight宏插件2(非常好用,强力推荐)
    Beyond Compare 3添加右键菜单
    OpenCV图像读取和写入
    TY科技的工程配置(VS2017 & Opencv4.0.0)
    Visual Studio2017 & pcl1.8.1 库的配置
    LeetCode No.198
    LeetCode No.191
    LeetCode No.190
    LeetCode No.179**
  • 原文地址:https://www.cnblogs.com/strengthen/p/10409073.html
Copyright © 2011-2022 走看看