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


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 (iif (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 (iwhile (isnumalpha(s[i]) == false && iwhile (isnumalpha(s[j]) == false && iif (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 iwhile isnumalpha(s[i])==False and i1
            while isnumalpha(s[j])==False and i1
            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 iwhile s[i].isalnum()==False and i1
            while s[j].isalnum()==False and i1
            if s[i].upper()!=s[j].upper():
                return False
            i+=1
            j-=1
        return True

你可能感兴趣的:(leetcode,算法,Leetcode)