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.

解题思路:

建立两个index,同时从前从后遍历字符串。遇到非字符需要跳过,遇到大写字母将其转为小写字母。比较两个index指向的字母,如果一致则继续遍历下一组,如果不相同则返回false,最后返回true;

注意:

1、非字母不需要比较,要跳过;

2、注意统一大小写;

 class Solution {
public:
bool isPalindrome(string s) {
int len = s.length();
int front = ;
int behind = len - ; if (!len)
return true; while (front <= behind) { if (!isAlphanumeric(&s[front])) {
front++;
continue;
} if (!isAlphanumeric(&s[behind])) {
behind--;
continue;
} if (s[front] != s[behind]) {
return false;
} else {
front ++;
behind --;
} } return true;
} bool isAlphanumeric(char *s) {
if ((*s>='a' && *s<='z') || (*s>='' && *s<=''))
return true;
if (*s>='A' && *s<='Z') {
*s += ;
return true;
}
return false;
} };

另,有些程序直接使用了C++中isalnum()和tolower()函数,思路是一样的:

 class Solution {
public:
bool isPalindrome(string s) {
int len = s.length();
int front = ;
int behind = len - ; if (!len)
return true; while (front <= behind) {
if (!isalnum(s[front])) {
front++;
continue;
} if (!isalnum(s[behind])) {
behind--;
continue;
} if (tolower(s[front]) != tolower(s[behind])) {
return false;
} else {
front ++;
behind --;
}
} return true;
}
};

附录:

常用字符ASCII值

【Leetcode】【Easy】Valid Palindrome的更多相关文章

  1. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  2. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  3. 【LeetCode题意分析&解答】35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  4. 【leetcode刷题笔记】Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  5. ACM金牌选手整理的【LeetCode刷题顺序】

    算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...

  6. 【LeetCode算法题库】Day3:Reverse Integer & String to Integer (atoi) & Palindrome Number

    [Q7]  把数倒过来 Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Outpu ...

  7. 【LeetCode算法题库】Day7:Remove Nth Node From End of List & Valid Parentheses & Merge Two Lists

    [Q19] Given a linked list, remove the n-th node from the end of list and return its head. Example: G ...

  8. 【LeetCode题意分析&解答】36. Valid Sudoku

    Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...

  9. 【LeetCode每天一题】Longest Valid Parentheses(最长有效括弧)

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  10. 【LeetCode每天一题】Valid Parentheses(有效的括弧)

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

随机推荐

  1. P4890 Never·island

    传送门 考虑把总区间长度减去最多能减少的区间长度 把所有区间离散化,对每一小段计算贡献 分类讨论一波,对于边界 $i,i+1$ ,设它们之间距离 $d$,$i$ 属于 $x$ 考察队的边界,$i+1$ ...

  2. 最小生成树----prim算法的堆优化

    题目描述 如题,给出一个无向图,求出最小生成树,如果该图不连通,则输出orz 输入输出格式 输入格式: 第一行包含两个整数N.M,表示该图共有N个结点和M条无向边.(N<=5000,M<= ...

  3. POJ - 3735 循环操作

    构造n+1元组,m次方的矩阵代表循环操作 本题尚有质疑之处(清零操作的正确性还有单位矩阵的必要性),题解可能会改正 #include<iostream> #include<algor ...

  4. 全排列 next_permutation() 函数的使用

    看来看去还是这篇博客比较简洁明了 https://www.cnblogs.com/My-Sunshine/p/4985366.html 顺便给出牛客网的一道题,虽然这道题用dfs写出全排列也能做,题意 ...

  5. ecmall模板编辑中的标题如何自定义读取

    碰见了一个问题,刚上线的ecmall项目.客户说标题不要商城首页这四个字. 我去源码里找,找了半天才找到. 问题描述如下: 找到title的最原始模板themes\mall\tmall\top.htm ...

  6. Python学习 day08

    一.open打开文件 文件操作包含以下三个步骤: 1.文件路径 2.编码方式 3.操作方式:‘’只读‘’.“只写”.“读写” 等 1.只读 r (mode默认值) 例: f = open('d:\py ...

  7. pandas to_excel、to_csv的float_format参数设置

    df.to_excel(outpath,float_format='%.2f')

  8. HashMap的结构算法及代码分析

    HashMap算是日常开发中最长用的类之一了,我们应该了解它的结构跟算法: 参考文章: http://blog.csdn.net/vking_wang/article/details/14166593 ...

  9. 突破Http协议

    突破Http协议 我到不先说什么Http什么的,对于HTTP的彻底理解是http是应用层的一个程序,就像我们写的诸多客户端和服务器模型,我们可能为了可靠,为了方便数据的解析,我们在数据包中其实就是结构 ...

  10. Python项目中如何优雅的import

    Python项目中如何优雅的import 前言 之前有一篇关于Python编码规范的随笔, 但是写的比较杂乱, 因为提到了import语句, 在篇文章中, 我专门来讲Python项目中如何更好的imp ...