leetcode Valid Palindrome C++&python 题解

题目描述

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.

给定一个 字符串,判断是否是一个回文串,注:这道题中默认空串是回文串

思路分析

首先给出c++代码的思路,因为存在大小写的问题,所以需要统一把所有字母统一转为小写或者大写,所以这里使用到了STL中的transform()方法

下面简要记录一下transform()的用法,该算法用于容器元素的变换操作,有如下两个使用原型,一个将迭代器区间[first,last)中元素,执行一元函数对象op操作,交换后的结果放在[result,result+(last-first))区间中。另一个将迭代器区间[first1,last1)的元素*i,依次与[first2,first2+(last-first))的元素*j,执行二元函数操作binary_op(*i,*j)

函数原型

template < class InputIterator, class OutputIterator, class UnaryOperator >
  OutputIterator transform ( InputIterator first1, InputIterator last1,
                             OutputIterator result, UnaryOperator op );

template < class InputIterator1, class InputIterator2,
           class OutputIterator, class BinaryOperator >
  OutputIterator transform ( InputIterator1 first1, InputIterator1 last1,
                             InputIterator2 first2, OutputIterator result,
                             BinaryOperator binary_op );

参数解释:

irst1, last1

指出要进行元素变换的第一个迭代器区间 [first1,last1)。

first2

指出要进行元素变换的第二个迭代器区间的首个元素的迭代器位置,该区间的元素个数和第一个区间相等。

result

指出变换后的结果存放的迭代器区间的首个元素的迭代器位置

op

用一元函数对象op作为参数,执行其后返回一个结果值。它可以是一个函数或对象内的类重载operator()。

binary_op

用二元函数对象binary_op作为参数,执行其后返回一个结果值。它可以是一个函数或对象内的类重载operator()。

给出一段示例代码

// transform algorithm example
#include <iostream>     // std::cout
#include <algorithm>    // std::transform
#include <vector>       // std::vector
#include <functional>   // std::plus

int op_increase (int i) { return ++i; }

int main () {
  std::vector<int> foo;
  std::vector<int> bar;

  // set some values:
  for (int i=1; i<6; i++)
    foo.push_back (i*10);                         // foo: 10 20 30 40 50

  bar.resize(foo.size());                         // allocate space

  std::transform (foo.begin(), foo.end(), bar.begin(), op_increase);
                                                  // bar: 11 21 31 41 51

  // std::plus adds together its two arguments:
  std::transform (foo.begin(), foo.end(), bar.begin(), foo.begin(), std::plus<int>());
                                                  // foo: 21 41 61 81 101

  std::cout << "foo contains:";
  for (std::vector<int>::iterator it=foo.begin(); it!=foo.end(); ++it)
    std::cout << ‘ ‘ << *it;
  std::cout << ‘\n‘;

  return 0;
}

output:

foo contains: 21 41 61 81 101

下面描述思路

统一转为大小写后,利用两个迭代器,一个指向开头,一个指向结尾,每次循环判断,如果不是数字或字母就跳过,如果两个迭代器指向的内容不一致则直接返回,如果相等则两个迭代器同时向中间走一步,直到两个指针相遇则判断是回文串

给出代码实现:

class Solution
{
public:
    bool isPalindrome(string s)
    {
        transform(s.begin(),s.end(),s.begin(),::tolower);
        string::iterator left=s.begin(),right=s.end();
        while(left<right)
        {
            if (!::isalnum(*left))
                left++;
            else if(!::isalnum(*right))
                right--;
            else if((*left)!=(*right))
                return false;
            else
                {left++,right--;}

        }
        return true;
    }
};

注意这里域运算符::的用法,涉及到c++命名空间的问题,如果不加域运算符,会报找不到函数或者变量的错误。

python 解法

这道题用python的列表生成器和列表操作可以很简洁的解决,思路是先用列表生成器去掉除数字和字母以外的字符得到一个新的字符串,然后直接判断该串和该串的逆序是否相等即可。

代码实现如下

class Solution:
    def isPalindrome(self, s):
        newS=[i.lower() for i in s if i.isalnum()]
        return newS==newS[::-1]

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-09 21:14:14

leetcode Valid Palindrome C++&python 题解的相关文章

leetcode Valid Palindrome C++&amp;amp;python 题解

题目描写叙述 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 consid

LeetCode: Valid Palindrome [125]

[题目] 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

[LeetCode] Valid Palindrome [10]

题目 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 t

LeetCode Valid Palindrome Python

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

[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

LeetCode:Valid Palindrome(need update)

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 解题报告

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

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

LeetCode——Valid Palindrome

Description: 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. public class So