LeetCode 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.
思路
我的想法是定义一个start指针和end指针,start从头部往尾部移动,end从尾部往头部移动,其中遇到非数字和字母时,要跳过。每次都做一下对比,不同就return 0。否则一直循环下去,如果一路顺利都是匹配的话,那么return 1。 不过有个地方需要注意,匹配是不区分大小写的,所以比较前要先把大写转化为小写。
代码
|
|
476 / 476 test cases passed.
Status: Accepted
Runtime: 4 ms