From fe1f4906c3d78b0c6e0f20b2434bc868c55379c0 Mon Sep 17 00:00:00 2001 From: Lanre Adedara Date: Mon, 3 Jun 2024 07:36:41 +0100 Subject: [PATCH 1/3] Swift implementation for LCOF2 018 --- .../README.md" | 26 +++++++++++++++++++ .../Solution.swift" | 21 +++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 "lcof2/\345\211\221\346\214\207 Offer II 018. \346\234\211\346\225\210\347\232\204\345\233\236\346\226\207/Solution.swift" diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 018. \346\234\211\346\225\210\347\232\204\345\233\236\346\226\207/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 018. \346\234\211\346\225\210\347\232\204\345\233\236\346\226\207/README.md" index bb3de48e35f4e..0e4621b249ff8 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 018. \346\234\211\346\225\210\347\232\204\345\233\236\346\226\207/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 018. \346\234\211\346\225\210\347\232\204\345\233\236\346\226\207/README.md" @@ -206,6 +206,32 @@ impl Solution { } ``` +#### Swift + +```swift +class Solution { + func isPalindrome(_ s: String) -> Bool { + var i = s.startIndex + var j = s.index(before: s.endIndex) + + while i < j { + while i < j && !s[i].isLetter && !s[i].isNumber { + i = s.index(after: i) + } + while i < j && !s[j].isLetter && !s[j].isNumber { + j = s.index(before: j) + } + if s[i].lowercased() != s[j].lowercased() { + return false + } + i = s.index(after: i) + j = s.index(before: j) + } + return true + } +} +``` + diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 018. \346\234\211\346\225\210\347\232\204\345\233\236\346\226\207/Solution.swift" "b/lcof2/\345\211\221\346\214\207 Offer II 018. \346\234\211\346\225\210\347\232\204\345\233\236\346\226\207/Solution.swift" new file mode 100644 index 0000000000000..8caa3ceb70fdb --- /dev/null +++ "b/lcof2/\345\211\221\346\214\207 Offer II 018. \346\234\211\346\225\210\347\232\204\345\233\236\346\226\207/Solution.swift" @@ -0,0 +1,21 @@ +class Solution { + func isPalindrome(_ s: String) -> Bool { + var i = s.startIndex + var j = s.index(before: s.endIndex) + + while i < j { + while i < j && !s[i].isLetter && !s[i].isNumber { + i = s.index(after: i) + } + while i < j && !s[j].isLetter && !s[j].isNumber { + j = s.index(before: j) + } + if s[i].lowercased() != s[j].lowercased() { + return false + } + i = s.index(after: i) + j = s.index(before: j) + } + return true + } +} \ No newline at end of file From 33df2ef2e29c84ab1650f744b10261dc91f0a4e5 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Mon, 3 Jun 2024 16:17:20 +0800 Subject: [PATCH 2/3] Update README.md --- .../README.md" | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 018. \346\234\211\346\225\210\347\232\204\345\233\236\346\226\207/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 018. \346\234\211\346\225\210\347\232\204\345\233\236\346\226\207/README.md" index 0e4621b249ff8..6f059d073bca2 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 018. \346\234\211\346\225\210\347\232\204\345\233\236\346\226\207/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 018. \346\234\211\346\225\210\347\232\204\345\233\236\346\226\207/README.md" @@ -213,7 +213,7 @@ class Solution { func isPalindrome(_ s: String) -> Bool { var i = s.startIndex var j = s.index(before: s.endIndex) - + while i < j { while i < j && !s[i].isLetter && !s[i].isNumber { i = s.index(after: i) @@ -221,6 +221,9 @@ class Solution { while i < j && !s[j].isLetter && !s[j].isNumber { j = s.index(before: j) } + if i >= j { + break + } if s[i].lowercased() != s[j].lowercased() { return false } From cb70552e7b9285c0d75aca6c7503befbdbda49a7 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Mon, 3 Jun 2024 16:17:30 +0800 Subject: [PATCH 3/3] Update Solution.swift --- .../Solution.swift" | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 018. \346\234\211\346\225\210\347\232\204\345\233\236\346\226\207/Solution.swift" "b/lcof2/\345\211\221\346\214\207 Offer II 018. \346\234\211\346\225\210\347\232\204\345\233\236\346\226\207/Solution.swift" index 8caa3ceb70fdb..4f7e82123e45f 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 018. \346\234\211\346\225\210\347\232\204\345\233\236\346\226\207/Solution.swift" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 018. \346\234\211\346\225\210\347\232\204\345\233\236\346\226\207/Solution.swift" @@ -2,7 +2,7 @@ class Solution { func isPalindrome(_ s: String) -> Bool { var i = s.startIndex var j = s.index(before: s.endIndex) - + while i < j { while i < j && !s[i].isLetter && !s[i].isNumber { i = s.index(after: i) @@ -10,6 +10,9 @@ class Solution { while i < j && !s[j].isLetter && !s[j].isNumber { j = s.index(before: j) } + if i >= j { + break + } if s[i].lowercased() != s[j].lowercased() { return false } @@ -18,4 +21,4 @@ class Solution { } return true } -} \ No newline at end of file +}