LeetCode 125 Valid Palindrome(有效回文)(*)

简介: 版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/50623165 翻译给定一个字符串,确定它是否是回文的,仅仅考虑其中的数字和字符并忽略其他。
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/50623165

翻译

给定一个字符串,确定它是否是回文的,仅仅考虑其中的数字和字符并忽略其他。

例如,
“A man, a plan, a canal: Panama” 是回文的。
“race a car” 不是回文的

批注:
你是否考虑了字符串可能为空?这种面试的时候是一个好问题。

对于这问题的意图,我们定义空字符串是回文的。

原文

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.

分析

题目解出来并不是很难,不过需要细心不过还是会出错。

第一步就是要从原字符串中的干扰去掉,也就是只保留字母,这可以通过ascii来判断。

然后构造出新的字符串,再判断这个新字符串是否是回文的就好了。

然而我也还是错了,首先我没想清楚题目的例子,”aA”也是回文的。

再一次修改之后我还是错了,因为题目的alphanumeric是字母数字均包含了,我一开始翻译的就是字母,后来上文的翻译也修改了。

于是我确定彻底的改革,将功能独立出来,然而代码好长了,但是很清晰:

代码

class Solution {
public:
    bool isAlpha(char c) {
        int ascii = (int)c;
        if ((ascii >= 65 && ascii <= 90) || (ascii >= 97 && ascii <= 122))
            return true;
        else return false;
    }    
    bool isNumber(char c) {
        int ascii = (int)c;
        if (ascii >= 48 && ascii <= 57)
            return true;
        else return false;
    }       
    bool isAlphaAndNumber(char c1, char c2) {
        if ((isAlpha(c1) && isNumber(c2)) || (isAlpha(c2) && isNumber(c1)))
            return true;
        else return false;
    }       
    bool isPalindrome(string s) {
        if (s.size() == 0) return true;
        string newStr = "";
        for (int i = 0; i < s.size(); ++i) {
            // 只取出其中的字母和数字
            if (isAlpha(s[i]) || isNumber(s[i]))
                newStr += s[i];
        }
        for (int i = 0; i < newStr.size() / 2; ++i) {
            // 两者一个是字母、一个是数字
            if (isAlphaAndNumber(newStr[i], newStr[newStr.size() - i - 1]))
                return false;
            // 两者均为数字
            else if (isNumber(newStr[i]) && isNumber(newStr[newStr.size() - i - 1])) {
                // 判断是否是同一个数字
                if (newStr[i] != newStr[newStr.size() - i - 1])
                    return false;
            }
            // 两者均为字母
            else {
                // 前面判断是否是同一个字母,后面判断是否是互为大小写
                if (newStr[i] != newStr[newStr.size() - i - 1] && abs((int)newStr[i] - (int)newStr[newStr.size() - i - 1]) != 32)
                    return false;
            }
        }
        return true;
    }                           
};

进阶

一想到ascii就忘了还有toupper这些函数了,别人写的……

class Solution {
public:
    bool isPalindrome(string s) {
        int l = 0, r = s.size() - 1;
        while(l <= r){
            while(!isalnum(s[l]) && l < r) l++;
            while(!isalnum(s[r]) && l < r) r--;
            if(toupper(s[l]) != toupper(s[r])) return false;
            l++, r--;
        }
        return true;
    }
};
目录
相关文章
|
3月前
|
Go
golang力扣leetcode 479.最大回文数乘积
golang力扣leetcode 479.最大回文数乘积
21 0
LeetCode | 234. 回文链表
LeetCode | 234. 回文链表
|
6月前
|
算法
【Leetcode -405.数字转换为十六进制数 - 409.最长回文串】
【Leetcode -405.数字转换为十六进制数 - 409.最长回文串】
23 0
|
11天前
【力扣】409.最长回文串
【力扣】409.最长回文串
|
3月前
|
Go
golang力扣leetcode 1332. 删除回文子序列
golang力扣leetcode 1332. 删除回文子序列
12 0
|
3月前
|
Go
golang力扣leetcode 234.回文链表
golang力扣leetcode 234.回文链表
10 0
|
3月前
|
机器学习/深度学习
leetcode-234:回文链表
leetcode-234:回文链表
18 0
|
4月前
|
算法 vr&ar 图形学
☆打卡算法☆LeetCode 214. 最短回文串 算法解析
☆打卡算法☆LeetCode 214. 最短回文串 算法解析
|
4月前
|
存储 Java
1457. 二叉树中的伪回文路径 --力扣 --JAVA
给你一棵二叉树,每个节点的值为 1 到 9 。我们称二叉树中的一条路径是 「伪回文」的,当它满足:路径经过的所有节点值的排列中,存在一个回文序列。 请你返回从根到叶子节点的所有路径中 伪回文 路径的数目。
59 3
|
5月前
|
计算机视觉
234.回文链表(LeetCode)
234.回文链表(LeetCode)
234.回文链表(LeetCode)