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++程序设计语言(第4部分:标准库)P146
问题一:啥叫回文palindrome:输入一个字符串,然后判定它是否为一个Palindrome,所谓Palindrome是指其 从正反两个方向看都是一样的(不分大小写),如"Madam"。
问题二:C++ transform
-
(1) unary operation
-
Applies
op to each of the elements in the range
[first1,last1)
and stores the value returned by each operation in the range that begins at result.
(2) binary operation
-
Calls
binary_op using each of the elements in the range
[first1,last1)
as first argument, and the respective argument in the range that begins at first2 as second argument. The value returned by each call is stored in the range that begins at result.
The behavior of this function template is equivalent to:
| |
The function allows for the destination range to be the same as one of the input ranges to make transformations in place.
Example:
// 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;
}
问题三:
x.begin() ---为指向X的第一个字符的iterator
x.end() ---为指向X末尾之后位置的iterator
x.size()----为x所含的字符数量
问题四: C++ ::tolower
Convert uppercase letter to lowercase 把大写的改成小写
问题5:C++ auto
C++11中引入的auto主要有两种用途:自动类型推断和返回值占位。
1. 自动类型推断
auto自动类型推断,用于从初始化表达式中推断出变量的数据类型。通过auto的自动类型推断,可以大大简化我们的编程工作。
问题6: C++ prev
迭代器是STL中重要的一支,prev和distance是其基本方法。distance方法十分简单,就不在此赘述,现主要对prev方法以及其相关方法——advance方法作简要介绍与使用说明,并在文末附上代码示例。
解析
prev在VC6.0之中不能实现,在VS2010之中可以实现。实际实现的操作是将迭代器递减一个单位长度而已,并未见所谓的递增操作或者根据是否作为随机迭代器时的一次或者n次操作!
问题7:C++ isalnum
isalnum
int isalnum ( int c );
解答及测试代码:
#include <string>
#include <algorithm>
#include <iostream>
using namespace std;
class solution{
public:
bool ispalindrome(string s){
if (s == "") return true; //如果是空字符串,则判别为true
else{
//transform(s.begin(), s.end(), s.begin(), tolower); //tolower大写搞成小写
for (auto temp = s.begin(); temp != s.end(); temp++){
if (isupper(*temp)) *temp = tolower(*temp);
}
auto left = s.begin(), right = prev(s.end()); //指向有用的prev(s.end()),迭代器减去1而已, auto自动判别类型
while (left < right){
if (!isalnum(*left)) ++left; //判断是否是数字或字母
else if (!isalnum(*right)) --right;
else if (*left != *right) return false; //只要找到不相等就输出false
else {
left++;
right--;
}
}
return true;
}
}
};
//测试代码
int main(){
//初始化一个字符串
string a{ "a man, a plan, a canal: panama" };
string b{"race a car"};
string c; //空字符串
//transform(c.begin(), c.end(), c.begin(), tolower);
solution mysolution;
//cout <<(c.begin() < c.end())<< endl;
//cout << (2 <= 1) << endl;
cout <<mysolution.ispalindrome(c)<< endl;
return 0;
}