## 一.题目描述
![](https://box.kancloud.cn/2016-01-05_568bb5ea2190e.jpg)
## 二.解题技巧
这道题考察回文数(palindrome),这一概念起源于在数学中一类数字,这类数字拥有这样的特征:
设n是一任意自然数。若将n的各位数字**反向排列**所得自然数n1与n相等,则称n为一回文数。例如,若n=1234321,则称n为一回文数;但若n=1234567,则n不是回文数。
同理,可以定义英文/中文的回文数,概念和以上的类似,本题就是检测字符串是否为回文数。与数字回文不同的是,题目中给出的英文句子/短语包含空格及标点符号,因此需要在算法设计时加多一层判断,可能会用到以下函数:
~~~
isalpha() // 如果参数是一个字母,返回一个非零数;否则返回为0
isalnum() // 如果参数是一个字母或数字,返回一个非零数;否则返回为0
isdigit() // 如果参数是一个数字(0-9)返回一个非零数;否则返回为0
~~~
若不要求区分字符串的大小写问题,可以加入以下函数:
~~~
transform(string.begin(),string.end(),string.begin(),toupper); // 将字符串中的内容转换为大写字母
transform(string.begin(),string.end(),string.begin(),tolower); // 将字符串中的内容转换为小写字母
~~~
题目说明,空字符串也可判定为回文,除此之外,该题目并没有很复杂的逻辑问题和边界条件。
## 三.示例代码
~~~
#include <string>
#include <iostream>
using std::string;
class Solution
{
public:
bool validPalindrome(string s)
{
if (s == "")
return true;
auto index_start = s.begin(), index_end = prev(s.end());
while (index_start < index_end)
{
if (!isalpha(*index_start))
index_start++;
else if (!isalpha(*index_end))
index_end--;
else if (*index_start == *index_end)
{
index_start++;
index_end--;
}
else return false;
}
return true;
}
};
~~~
## 四.一个示例结果
输入回文字符串:
![](https://box.kancloud.cn/2016-01-05_568bb5ea43cbd.jpg)
输入非回文字符串:
![](https://box.kancloud.cn/2016-01-05_568bb5ea58063.jpg)
- 前言
- 2Sum
- 3Sum
- 4Sum
- 3Sum Closest
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array II
- Search in Rotated Sorted Array
- Remove Element
- Merge Sorted Array
- Add Binary
- Valid Palindrome
- Permutation Sequence
- Single Number
- Single Number II
- Gray Code(2016腾讯软件开发笔试题)
- Valid Sudoku
- Rotate Image
- Power of two
- Plus One
- Gas Station
- Set Matrix Zeroes
- Count and Say
- Climbing Stairs(斐波那契数列问题)
- Remove Nth Node From End of List
- Linked List Cycle
- Linked List Cycle 2
- Integer to Roman
- Roman to Integer
- Valid Parentheses
- Reorder List
- Path Sum
- Simplify Path
- Trapping Rain Water
- Path Sum II
- Factorial Trailing Zeroes
- Sudoku Solver
- Isomorphic Strings
- String to Integer (atoi)
- Largest Rectangle in Histogram
- Binary Tree Preorder Traversal
- Evaluate Reverse Polish Notation(逆波兰式的计算)
- Maximum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Longest Common Prefix
- Recover Binary Search Tree
- Binary Tree Level Order Traversal
- Binary Tree Level Order Traversal II
- Binary Tree Zigzag Level Order Traversal
- Sum Root to Leaf Numbers
- Anagrams
- Unique Paths
- Unique Paths II
- Triangle
- Maximum Subarray(最大子串和问题)
- House Robber
- House Robber II
- Happy Number
- Interlaving String
- Minimum Path Sum
- Edit Distance
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock II
- Best Time to Buy and Sell Stock III
- Best Time to Buy and Sell Stock IV
- Decode Ways
- N-Queens
- N-Queens II
- Restore IP Addresses
- Combination Sum
- Combination Sum II
- Combination Sum III
- Construct Binary Tree from Inorder and Postorder Traversal
- Construct Binary Tree from Preorder and Inorder Traversal
- Longest Consecutive Sequence
- Word Search
- Word Search II
- Word Ladder
- Spiral Matrix
- Jump Game
- Jump Game II
- Longest Substring Without Repeating Characters
- First Missing Positive
- Sort Colors
- Search for a Range
- First Bad Version
- Search Insert Position
- Wildcard Matching