From 0b0e490b6a15a039f85e7a3362a24fdd126b3032 Mon Sep 17 00:00:00 2001 From: Lanre Adedara Date: Mon, 15 Apr 2024 09:48:16 +0100 Subject: [PATCH 1/2] Swift implementation for 01.06 & 01.07 Signed-off-by: Lanre Adedara --- lcci/01.06.Compress String/README.md | 22 +++++++++++++++++++++ lcci/01.06.Compress String/README_EN.md | 22 +++++++++++++++++++++ lcci/01.06.Compress String/Solution.swift | 19 ++++++++++++++++++ lcci/01.07.Rotate Matrix/README.md | 24 +++++++++++++++++++++++ lcci/01.07.Rotate Matrix/README_EN.md | 24 +++++++++++++++++++++++ lcci/01.07.Rotate Matrix/Solution.swift | 21 ++++++++++++++++++++ 6 files changed, 132 insertions(+) create mode 100644 lcci/01.06.Compress String/Solution.swift create mode 100644 lcci/01.07.Rotate Matrix/Solution.swift diff --git a/lcci/01.06.Compress String/README.md b/lcci/01.06.Compress String/README.md index c6bed3d949bac..8c2df7cb69090 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..ad77a44650d27 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.. Date: Mon, 15 Apr 2024 08:52:05 +0000 Subject: [PATCH 2/2] style: format code and docs with prettier --- lcci/01.06.Compress String/README.md | 4 ++-- lcci/01.06.Compress String/README_EN.md | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lcci/01.06.Compress String/README.md b/lcci/01.06.Compress String/README.md index 8c2df7cb69090..6c7ad202b717b 100644 --- a/lcci/01.06.Compress String/README.md +++ b/lcci/01.06.Compress String/README.md @@ -173,7 +173,7 @@ class Solution { let n = S.count var compressed = "" var i = 0 - + while i < n { var j = i let currentChar = S[S.index(S.startIndex, offsetBy: i)] @@ -183,7 +183,7 @@ class Solution { 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 ad77a44650d27..d82ed0642749d 100644 --- a/lcci/01.06.Compress String/README_EN.md +++ b/lcci/01.06.Compress String/README_EN.md @@ -179,7 +179,7 @@ class Solution { let n = S.count var compressed = "" var i = 0 - + while i < n { var j = i let currentChar = S[S.index(S.startIndex, offsetBy: i)] @@ -189,7 +189,7 @@ class Solution { compressed += "\(currentChar)\(j - i)" i = j } - + return compressed.count < n ? compressed : S } }