【leetcode】125. Valid Palindrome(Python & C++)

本文介绍了一种方法来判断一个给定的字符串是否为回文,重点在于只考虑字母和数字字符,并忽略大小写差异。提供了三种不同的C++实现思路及其代码示例,同时也给出了两种Python实现方式。

125. Valid Palindrome

题目链接

125.1 题目描述:

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.

125.2 解题思路:

  1. 思路一:分两步:第一步判断字符是否是数字或者字母,第二步判断是否回文。

    • 判断字符是否是数字或者字母:如果字符在65-90(A-Z)或者97-122(a-z)或者48-57(0-9)之间,则返回true,否则false。
    • 判断是否回文:设置两个指针i=0和j=s.lenght()-1,各自从两头遍历,且i小于j,如果s[i]判断为不是数字或字母,i一直++。如果s[j]判断为不是数字或字母,j一直–。如果s[i]==s[j],或者两者都是字母,但是一个大写一个小写(两者相差32),则i++,j–;否则,返回false。遍历结束,返回true。
  2. 思路二:等同于思路一,写法优化。

  3. 思路三:等同于思路一。两处不同:一是判断是否是字母或数字的函数使用自带函数;二是如果两者同为字母,但一个大写一个小写,这里统一用自带函数upper或者lower或者toupper处理。

125.3 C++代码:

1、思路一代码(9ms):

class Solution130 {
public:
    bool isnumalpha(char c)
    {
        if ((c > 64 && c < 91) || (c>96 && c < 123) || (c>47 && c < 58))
            return true;
        else
            return false;
    }
    bool isPalindrome(string s) {
        if (s.length() == 0)
            return true;
        int i = 0;
        int j = s.length() - 1;
        while (i<j)
        {
            if (isnumalpha(s[i]) && isnumalpha(s[j]))
            {
                if (s[i] == s[j] || (abs(s[i] - s[j]) == 32 && s[i]>57 && s[j]>57))
                {
                    i++;
                    j--;
                }
                else
                    return false;
            }
            if (isnumalpha(s[i]) == false)
                i++;
            if (isnumalpha(s[j]) == false)
                j--;
        }
        return true;
    }
};

2、思路二代码(9ms)

class Solution130_1 {
public:
    bool isnumalpha(char c)
    {
        if ((c > 64 && c < 91) || (c>96 && c < 123) || (c>47 && c < 58))
            return true;
        else
            return false;
    }
    bool isPalindrome(string s) {
        if (s.length() == 0)
            return true;
        int i = 0;
        int j = s.length() - 1;
        while (i<j)
        {
            while (isnumalpha(s[i]) == false && i<j)
                i++;
            while (isnumalpha(s[j]) == false && i<j)
                j--;
            if (s[i] == s[j] || (abs(s[i] - s[j]) == 32 && s[i] > 57 && s[j] > 57))
            {
                i++;
                j--;
            }
            else
                return false;
        }
        return true;
    }
};

3、思路三代码(9ms)

class Solution130_2 {
public:
    bool isPalindrome(string s) {
        if (s.length() == 0)
            return true;
        int i = 0;
        int j = s.length() - 1;
        while (i < j)
        {
            while (isalnum(s[i]) == false && i < j)
                i++;
            while (isalnum(s[j]) == false && i < j)
                j--;
            if (toupper(s[i])!=toupper(s[j]))
                return false;
            i++;
            j--;
        }
        return true;
    }
};

125.4 Python代码:

2、思路二代码(122ms)

class Solution(object):
    def isPalindrome(self, s):
        """
        :type s: str
        :rtype: bool
        """
        if len(s)==0:
            return True
        def isnumalpha(c):
            if (c>='a' and c<='z') or (c>='A' and c<='Z') or (c>='0' and c<='9'):
                return True
            else:
                return False
        i=0
        j=len(s)-1
        while i<j:
            while isnumalpha(s[i])==False and i<j:
                i+=1
            while isnumalpha(s[j])==False and i<j:
                j-=1
            if s[i]==s[j] or (s[i]>'9' and abs(ord(s[i])-ord(s[j]))==32):
                i+=1
                j-=1
            else:
                return False
        return True

3、思路三代码(89ms):

class Solution1(object):
    def isPalindrome(self, s):
        """
        :type s: str
        :rtype: bool
        """
        if len(s)==0:
            return True
        i=0
        j=len(s)-1   
        while i<j:
            while s[i].isalnum()==False and i<j:
                i+=1
            while s[j].isalnum()==False and i<j:
                j-=1
            if s[i].upper()!=s[j].upper():
                return False
            i+=1
            j-=1
        return True

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值