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                 //测试字符串里是否有字母或数字,若没有,则立刻返回
11         while(    (*p<‘0‘    ||    (*p>‘9‘&&*p<‘A‘)    ||    (*p>‘Z‘&&*p<‘a‘)    ||    *p>‘z‘)&&p!=q){
12             p++;
13         }
14         while(    (*q<‘0‘    ||    (*q>‘9‘&&*q<‘A‘)    ||    (*q>‘Z‘&&*q<‘a‘)    ||    *q>‘z‘)&&p!=q)    //非字母和数字,跳过
15             q--;
16         if(*q>=‘A‘&&*q<=‘Z‘)    //若大写,转为小写
17             *q=*q+32;
18         if(*p>=‘A‘&&*p<=‘Z‘)    //若大写,转为小写
19             *p=*p+32;
20         if(p==q)
21             break;
22         if(*p==*q){
23             p++;
24             if(p==q)
25                 break;
26             q--;
27         }
28         else
29             return false;
30     }
31     return true;
32     }
33 };        

题意:

"A man, a plan, a canal: Panama" is a palindrome.是回文
"race a car" is not a palindrome.非回文

回文:即将字符串倒过来之后和原来仍一样。如:did=did

但是,此题要求过滤掉非数字和字母的其他字符,而且不区分大小写,A和a是一样的。

思路:用两个指针,分别指向字符串的头和尾,每次判断要过滤掉无效的字符。

注意:要考虑空串(即没有字母和数字,可能只有空格,标点什么的),只有1个字符的字符串。还得考虑两个指针指向同一个地址时即已经是回文了。

时间: 2024-10-13 07:07:29

LeetCode Valid Palindrome 有效回文(字符串)的相关文章

[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

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] 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:Palindrome Number - 回文数

1.题目名称 Palindrome Number(回文数) 2.题目地址 https://leetcode.com/problems/palindrome-number 3.题目内容 英文:Determine whether an integer is a palindrome. Do this without extra space. 中文:确认一个整数是否是回文数 4.解题方法1 将数字翻转后判断与原数字是否相等,可以参考LeetCode第7题(Reverse Integer)的解题思路.J

leetcode 9 Palindrome Number 回文数

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

LeetCode (30) 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.

Leetcode——3 Palindrome Number(回文数)

Problem: Determine whether an integer is a palindrome. Do this without extra space. 简单的回文数,大一肯定有要求写过,不过从基础开始尝试吧. Solution: public class Solution { public boolean isPalindrome(int x) { int n=1; int copyx=x; if(x<0)return false; if(x<10)return true; w

LeetCode 9 Palindrome Number 回文数字

题目:Determine whether an integer is a palindrome. Do this without extra space. 翻译:判断一个数字是否是回文数,不要额外空间. 解题思路:因为数字既然传过去了,就不会有越界的问题.每次只需要取最前面和最后面的那一位数字进行比较,相同则继续,不同则返回. 首先要获取数字的位数,假设数字是12344321,一共有8位. 其次是要每次取前后各一位来进行比较,用数字除以1后面7个0得到第一位,用数字对10取余数得到最后一位. 此

[LeetCode]9. Palindrome Number回文数

Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. Example 1: Input: 121 Output: true Example 2: Input: -121 Output: false Explanation: From left to right, it reads -121. From right to