Question
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
For example,
"A man, a plan, a canal: Panama"
is a palindrome.
"race a car"
is not a palindrome.
Note:
Have you consider that the string might be empty? This is a good question to ask during an interview.
For the purpose of this problem, we define empty string as valid palindrome.
本题难度Easy。
双指针法
【复杂度】
时间 O(N) 空间 O(1)
【思路】
字串中有字母、数字和其他字符,我们利用两个指针从两端向中间靠拢,碰到字母或数字就停下然后对比两端字符是否相等,否则跳过。这里要先对字串中的大小写字母进行转换。
【代码】