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]
leetcode Valid Palindrome C++&python 题解的更多相关文章
- [leetcode]Valid Palindrome @ Python
原题地址:https://oj.leetcode.com/problems/valid-palindrome/ 题意: Given a string, determine if it is a pal ...
- LeetCode Valid Palindrome II
原题链接在这里:https://leetcode.com/problems/valid-palindrome-ii/description/ 题目: Given a non-empty string ...
- [LeetCode] Valid Palindrome 验证回文字符串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- LeetCode——Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- [LeetCode] Valid Palindrome II 验证回文字符串之二
Given a non-empty string s, you may delete at most one character. Judge whether you can make it a pa ...
- Leetcode Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- LeetCode: Valid Palindrome [125]
[题目] Given a string, determine if it is a palindrome, considering only alphanumeric characters and i ...
- LeetCode: Valid Palindrome 解题报告
Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric char ...
- [Leetcode] valid palindrome 验证回文
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
随机推荐
- Django初学及mvt模型理解
Django是python语言用来做web项目的一个模板,创建Django项目之后会自动生成models,views和templates文件夹,又叫mvt框架 文件结构如下: Models:其中mod ...
- 图论:POJ2186-Popular Cows (求强连通分量)
Popular Cows Description Every cow's dream is to become the most popular cow in the herd. In a herd ...
- MySql update inner join!MySql跨表更新 多表update sql语句?如何将select出来的部分数据update到另一个表里面?
项目中,评论数,关注数等数据,是实时更新的.+1,-1 这种. 有的时候,可能统计不准确. 需要写一个统计工具,更新校准下. 用Java写SQL和函数,代码很清晰,方便扩展,但是太慢了. 为了简单起见 ...
- Hive中文注释乱码解决方案(2)
本文来自网易云社区 作者:王潘安 执行阶段 launchTask 回到Driver类的runInternal方法,看以下执行过程.在runInternal方法中,执行过程调用了execute方法 ...
- 【LeetCode】Valid Parentheses(有效的括号)
这道题是LeetCode里的第20道题. 题目要求: 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭 ...
- 【LeetCode】Maximize Sum Of Array After K Negations(K 次取反后最大化的数组和)
这道题是LeetCode里的第1005道题. 题目描述: 给定一个整数数组 A,我们只能用以下方法修改该数组:我们选择某个个索引 i 并将 A[i] 替换为 -A[i],然后总共重复这个过程 K 次. ...
- ASP.NET(一):Reques对象和Response对象的区别,以及IsPostBack属性的用法
导读:在ASP.NET的学习中,初步认识了其6大对象(严格说来只能算是属性):Request,Response,Application,Session,Server,OjectContext.这些对象 ...
- Android自制rom,为update.zip签名
确认已经安装好openssl openssl genrsa -out key.pem openssl req -new -key key.pem -out request.pem openssl x5 ...
- 【Luogu】P2530化工厂装箱员(DP)
题目链接 不知道做出这道题是我能力的一个提升还是能力的回归. DP.设f[i][j][k][l]是已经取了i个产品,现在手里还拿着j件A,k件B,l件C,最小的操作数. 然后状转方程乱搞啊 #incl ...
- BZOJ3122 [Sdoi2013]随机数生成器 【BSGS】
题目 输入格式 输入含有多组数据,第一行一个正整数T,表示这个测试点内的数据组数. 接下来T行,每行有五个整数p,a,b,X1,t,表示一组数据.保证X1和t都是合法的页码. 注意:P一定为质数 输出 ...