From f793de00c271cb81437728c8f9113aa7ade9627e Mon Sep 17 00:00:00 2001 From: Lanre Adedara Date: Tue, 8 Oct 2024 08:03:33 +0100 Subject: [PATCH 1/2] feat: add swift implementation to lcof2 problem: No.107 --- .../README.md" | 39 +++++++++++++++++++ .../Solution.swift" | 33 ++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 "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" 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..7118230802883 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,45 @@ 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 + } +} From ff58a4210d7e1680443bbf648f21a6f486851749 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Tue, 8 Oct 2024 17:39:21 +0800 Subject: [PATCH 2/2] Update README.md --- .../README.md" | 1 - 1 file changed, 1 deletion(-) 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 7118230802883..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" @@ -236,7 +236,6 @@ class Solution { return ans } } - ```