2 Star 2 Fork 1

Justin Yuan/LeetCode-Swift

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
ValidPalindrome.swift 912 Bytes
Copy Edit Raw Blame History
/**
* Question Link: https://leetcode.com/problems/valid-palindrome/
* Primary idea: For every index in the first half of the String, compare two values at mirroring indices.
*
* Time Complexity: O(n), Space Complexity: O(n)
*
*/
class ValidPalindrome {
func isPalindrome(_ s: String) -> Bool {
var i = 0, j = s.count - 1
let sChars = Array(s.lowercased())
while i < j {
while !sChars[i].isAlphanumeric && i < j {
i += 1
}
while !sChars[j].isAlphanumeric && i < j {
j -= 1
}
if sChars[i] != sChars[j] {
return false
} else {
i += 1
j -= 1
}
}
return true
}
}
extension Character {
var isValid: Bool {
return isLetter || isNumber
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/BiggerMax/LeetCode-Swift.git
git@gitee.com:BiggerMax/LeetCode-Swift.git
BiggerMax
LeetCode-Swift
LeetCode-Swift
master

Search