LeetCode(125)题解--Valid Palindrome

https://leetcode.com/problems/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.

思路:

easy.

AC代码:

 1 class Solution {
 2 public:
 3     bool isPalindrome(string s) {
 4         int a=0,b=s.size()-1;
 5         while(a<b){
 6             if(!((s[a]>=‘a‘&&s[a]<=‘z‘)||(s[a]>=‘A‘&&s[a]<=‘Z‘)||(s[a]>=‘0‘&&s[a]<=‘9‘))){
 7                 a++;
 8                 continue;
 9             }
10             if(!((s[b]>=‘a‘&&s[b]<=‘z‘)||(s[b]>=‘A‘&&s[b]<=‘Z‘)||(s[b]>=‘0‘&&s[b]<=‘9‘))){
11                 b--;
12                 continue;
13             }
14             if((s[a]>=‘a‘&&s[a]<=‘z‘)){
15                 if((s[a]!=s[b])&&(s[a]+‘A‘-‘a‘!=s[b])){
16                     return false;
17                 }
18             }
19             else if((s[a]>=‘A‘&&s[a]<=‘Z‘)){
20                 if((s[a]!=s[b])&&(s[a]!=s[b]+‘A‘-‘a‘)){
21                     return false;
22                 }
23             }
24             else{
25                 if(s[a]!=s[b]){
26                     return false;
27                 }
28             }
29             a++;
30             b--;
31         }
32         return true;
33     }
34 };
时间: 2024-10-26 06:40:33

LeetCode(125)题解--Valid Palindrome的相关文章

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 th

LeetCode: Valid Palindrome [125]

[题目] 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

leetcode题解: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 tha

leetCode 125. Valid Palindrome 字符串

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:Hav

[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 th

Java [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 t

【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 th

leetcode Valid Palindrome C++&amp;amp;python 题解

题目描写叙述 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 consid

leetcode 125. Valid Palindrome ----- java

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 th