diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 107. \347\237\251\351\230\265\344\270\255\347\232\204\350\267\235\347\246\273/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 107. \347\237\251\351\230\265\344\270\255\347\232\204\350\267\235\347\246\273/README.md" index 7a1840e9e959b..d54780acac9b6 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 107. \347\237\251\351\230\265\344\270\255\347\232\204\350\267\235\347\246\273/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 107. \347\237\251\351\230\265\344\270\255\347\232\204\350\267\235\347\246\273/README.md" @@ -200,6 +200,44 @@ func updateMatrix(mat [][]int) [][]int { } ``` +#### Swift + +```swift +class Solution { + func updateMatrix(_ mat: [[Int]]) -> [[Int]] { + let m = mat.count + let n = mat[0].count + var ans = Array(repeating: Array(repeating: -1, count: n), count: m) + var queue = [(Int, Int)]() + + for i in 0..= 0 && x < m && y >= 0 && y < n && ans[x][y] == -1 { + ans[x][y] = ans[i][j] + 1 + queue.append((x, y)) + } + } + } + + return ans + } +} +``` + diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 107. \347\237\251\351\230\265\344\270\255\347\232\204\350\267\235\347\246\273/Solution.swift" "b/lcof2/\345\211\221\346\214\207 Offer II 107. \347\237\251\351\230\265\344\270\255\347\232\204\350\267\235\347\246\273/Solution.swift" new file mode 100644 index 0000000000000..be98341a4a6cf --- /dev/null +++ "b/lcof2/\345\211\221\346\214\207 Offer II 107. \347\237\251\351\230\265\344\270\255\347\232\204\350\267\235\347\246\273/Solution.swift" @@ -0,0 +1,33 @@ +class Solution { + func updateMatrix(_ mat: [[Int]]) -> [[Int]] { + let m = mat.count + let n = mat[0].count + var ans = Array(repeating: Array(repeating: -1, count: n), count: m) + var queue = [(Int, Int)]() + + for i in 0..= 0 && x < m && y >= 0 && y < n && ans[x][y] == -1 { + ans[x][y] = ans[i][j] + 1 + queue.append((x, y)) + } + } + } + + return ans + } +}