125. Valid Palindrome
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.
题意:
给定一个字符串,确定它是否是回文,只考虑字母数字字符和忽略空格。
思路:
利用前后两个指针,回文串肯定是关于中间对称的,所以利用前后指针遍历比较,遍历的过程中忽略空格、忽略大小写,当前指针大于后指针的时候,说明遍历结束是回文串,如果遍历过程中有一个字符不等则肯定不是回文串。
1 | class Solution { |