diff --git a/lcci/01.06.Compress String/README.md b/lcci/01.06.Compress String/README.md index c6bed3d949bac..6c7ad202b717b 100644 --- a/lcci/01.06.Compress String/README.md +++ b/lcci/01.06.Compress String/README.md @@ -167,6 +167,28 @@ var compressString = function (S) { }; ``` +```swift +class Solution { + func compressString(_ S: String) -> String { + let n = S.count + var compressed = "" + var i = 0 + + while i < n { + var j = i + let currentChar = S[S.index(S.startIndex, offsetBy: i)] + while j < n && S[S.index(S.startIndex, offsetBy: j)] == currentChar { + j += 1 + } + compressed += "\(currentChar)\(j - i)" + i = j + } + + return compressed.count < n ? compressed : S + } +} +``` + diff --git a/lcci/01.06.Compress String/README_EN.md b/lcci/01.06.Compress String/README_EN.md index 4b2863013fc94..d82ed0642749d 100644 --- a/lcci/01.06.Compress String/README_EN.md +++ b/lcci/01.06.Compress String/README_EN.md @@ -173,6 +173,28 @@ var compressString = function (S) { }; ``` +```swift +class Solution { + func compressString(_ S: String) -> String { + let n = S.count + var compressed = "" + var i = 0 + + while i < n { + var j = i + let currentChar = S[S.index(S.startIndex, offsetBy: i)] + while j < n && S[S.index(S.startIndex, offsetBy: j)] == currentChar { + j += 1 + } + compressed += "\(currentChar)\(j - i)" + i = j + } + + return compressed.count < n ? compressed : S + } +} +``` + diff --git a/lcci/01.06.Compress String/Solution.swift b/lcci/01.06.Compress String/Solution.swift new file mode 100644 index 0000000000000..b8ed14611966f --- /dev/null +++ b/lcci/01.06.Compress String/Solution.swift @@ -0,0 +1,19 @@ +class Solution { + func compressString(_ S: String) -> String { + let n = S.count + var compressed = "" + var i = 0 + + while i < n { + var j = i + let currentChar = S[S.index(S.startIndex, offsetBy: i)] + while j < n && S[S.index(S.startIndex, offsetBy: j)] == currentChar { + j += 1 + } + compressed += "\(currentChar)\(j - i)" + i = j + } + + return compressed.count < n ? compressed : S + } +} diff --git a/lcci/01.07.Rotate Matrix/README.md b/lcci/01.07.Rotate Matrix/README.md index 5a12b7d78deb4..f12e2efe8cd1e 100644 --- a/lcci/01.07.Rotate Matrix/README.md +++ b/lcci/01.07.Rotate Matrix/README.md @@ -203,6 +203,30 @@ public class Solution { } ``` +```swift +class Solution { + func rotate(_ matrix: inout [[Int]]) { + let n = matrix.count + + for i in 0..<(n >> 1) { + for j in 0.. diff --git a/lcci/01.07.Rotate Matrix/README_EN.md b/lcci/01.07.Rotate Matrix/README_EN.md index 9b09740ef5e96..773574d8ba555 100644 --- a/lcci/01.07.Rotate Matrix/README_EN.md +++ b/lcci/01.07.Rotate Matrix/README_EN.md @@ -232,6 +232,30 @@ public class Solution { } ``` +```swift +class Solution { + func rotate(_ matrix: inout [[Int]]) { + let n = matrix.count + + for i in 0..<(n >> 1) { + for j in 0.. diff --git a/lcci/01.07.Rotate Matrix/Solution.swift b/lcci/01.07.Rotate Matrix/Solution.swift new file mode 100644 index 0000000000000..d16c8d6291c00 --- /dev/null +++ b/lcci/01.07.Rotate Matrix/Solution.swift @@ -0,0 +1,21 @@ +class Solution { + func rotate(_ matrix: inout [[Int]]) { + let n = matrix.count + + for i in 0..<(n >> 1) { + for j in 0..