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 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)注意两点:a、判断满足条件的字符  b、大写变小写(本题认为不区分大小写)

2)字符串为空则true

实现:

 1 class Solution {
 2 public:
 3         bool isPalindrome(string s) {
 4             if(s.empty()) return true;
 5             int len=strlen(s.c_str());
 6             int i=0;
 7             int j=0;
 8             for (;i<len;i++)
 9             {
10                 if (isChar(s[i]))//去其他符合,若有大写则大写变小写
11                 {
12                     s[i] = tolower(s[i]);//库函数
13                     s[j++]=s[i];
14                 }
15             }
16             s[j]=‘\0‘;
17             if (s[0]==‘\0‘) return true;
18             int len2=strlen(s.c_str());
19             i=0;
20             while(i<=len2-1-i) //判断是否回文
21             {
22                 if(s[i]!=s[len2-1-i]) return false;
23                 i++;
24             }
25             return true;
26     }
27     private:
28         bool isChar(char t)//判断是否满足条件字符
29         {
30             if ((‘a‘ <= t&&t<=‘z‘)||(‘A‘ <= t&&t<=‘Z‘)||(‘0‘ <= t&&t<=‘9‘))
31            {
32                return true;
33            }
34            else
35                return false;
36         }
37 };

leetcode题解:Valid Palindrome(判断回文)

时间: 2024-08-05 06:52:35

leetcode题解: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 有效回文(字符串)

1 class Solution { 2 public: 3 bool isPalindrome(string s) { 4 if(s=="") return true; 5 if(s.length()==1) return true; //单个字符,对称 6 char *p,*q; 7 p=&s[0]; //p指向开头 8 q=&s[s.length()-1]; //q指向末尾 9 while(p!=q){ 10 //测试字符串里是否有字母或数字,若没有,则立刻返回

LeetCode Problem 9:Palindrome Number回文数

描述:Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using extra space. You could a

[LintCode] Valid Palindrome 验证回文字符串

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. Notice 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

[LeetCode]40. Valid Palinadrome有效回文串

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

Validate Palindrome 验证回文字符串

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example, "race a car" is not a palindrome. Note:Have you consider that the string might be empty? This is a good question to ask du

leetcode -day13 Valid Palindrome &amp; Triangle &amp; Pascal&#39;s Triangle I II

1.  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:

[leetcode] 1. Valid Palindrome

leetcode的第一题,回文数判断. 原题如下: 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

HDOJ/HDU 2163 Palindromes(判断回文串~)

Problem Description Write a program to determine whether a word is a palindrome. A palindrome is a sequence of characters that is identical to the string when the characters are placed in reverse order. For example, the following strings are palindro