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.

分析:题意为 给出一个字符串,检查它是不是一个回文的情况。判断时只考虑字母数字的字符并且忽略大小写。

注意点 1、考虑字符串为空的情况,并将其作为回文的

2、忽略大小写的差别,比较之前先将大小写转换为一致的

3、非字母数字字符要跳过,将它滤除,得到有效字符串

4、对有效字符串前后相应位置进行比较判断

代码如下:

class Solution {
public:
bool isStr(char &ch){
if(ch >= '0' && ch <= '9'){
return true;
} else if(ch >= 'a' && ch <= 'z'){
return true;
} else if(ch >= 'A' && ch <= 'Z'){
ch += 32;
return true;
} return false;
} bool isPalindrome(string s) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int len = s.length();
if(len == 0){
return true;
} string str = "";
for(int i = 0; i < len; i++){ // remove illegal char, such as "?" "/" ...
if(isStr(s[i])){
str += s[i];
}
} len = str.length();
int mid = (len + 1) / 2;
for(int i = 0; i < mid; i++){
if(str[i] != str[len - 1 - i]){ // check front and end char
return false;
}
}
return true;
}
};

其他解法:  

leetcode:Valid Palindrome的更多相关文章

  1. [LeetCode] 680. Valid Palindrome II 验证回文字符串 II

    Given a non-empty string s, you may delete at most one character. Judge whether you can make it a pa ...

  2. LeetCode之“字符串”:Valid Palindrome

    题目链接 题目要求: Given a string, determine if it is a palindrome, considering only alphanumeric characters ...

  3. LeetCode OJ:Valid Palindrome(验证回文)

    Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric char ...

  4. leetcode题解:Valid Palindrome(判断回文)

    题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ig ...

  5. [leetcode] 1. Valid Palindrome

    leetcode的第一题,回文数判断. 原题如下: For example, "A man, a plan, a canal: Panama" is a palindrome. & ...

  6. LeetCode 1216. Valid Palindrome III

    原题链接在这里:https://leetcode.com/problems/valid-palindrome-iii/ 题目: Given a string s and an integer k, f ...

  7. [LeetCode] 125. Valid Palindrome 验证回文字符串

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

  8. 【leetcode】Valid Palindrome

    题目简述: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ...

  9. [Leetcode][JAVA] Valid Palindrome

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

随机推荐

  1. 推荐系统之LFM

    这里我想给大家介绍另外一种推荐系统,这种算法叫做潜在因子(Latent Factor)算法.这种算法是在NetFlix(没错,就是用大数据捧火<纸牌屋>的那家公司)的推荐算法竞赛中获奖的算 ...

  2. ios 调用打印机

    源码 无意中玩一个demo发现调用了打印机  才发现ios有快速调用打印机的功能. if ([UIPrintInteractionController isPrintingAvailable] == ...

  3. discuzx完全自定义设计模板门户首页,栏目,专题模板方法

    第一种:门户首页模板(index.htm,保存于templatedefaultportal) <!--{subtemplate common/header}--> <style id ...

  4. Lessons learned from manually classifying CIFAR-10

    Lessons learned from manually classifying CIFAR-10 Apr 27, 2011 CIFAR-10 Note, this post is from 201 ...

  5. CSS 外边距(margin)重叠及防止方法

    边界重叠是指两个或多个盒子(可能相邻也可能嵌套)的相邻边界(其间没有任何非空内容.补白.边框)重合在一起而形成一个单一边界. 两个或多个块级盒子的垂直相邻边界会重合.结果的边界宽度是相邻边界宽度中最大 ...

  6. 了解Javascript 变量

    javascript语言变量的作用域可以分为局部变量和全局变量 函数内部定义的变量为局部变量,作用范围在整个函数体内,函数外定义的变量为全局变量,如果在函数内部定义变量时没有使用关键字var,那么该变 ...

  7. register_globals

    register_globals参数为On的时候很危险 这里记录一下各版本register_globals的情况 PHP5.2版本register_globals默认为On PHP5.3 PHP5.3 ...

  8. Linuxshell脚本之if条件判断

    IF条件判断 .基本语法: if [ command ]; then 符合该条件执行的语句 fi .扩展语法: if [ command ];then 符合该条件执行的语句 elif [ comman ...

  9. ASP.NET和JSP相似方法总结(持续中。。)

    一.HTTP请求处理 1.获取GET请求数据 ASP.NET:Request.QueryString[name] JSP:request.getParameter(String name); 2.解决 ...

  10. HDU1724 Ellipse(数值积分)

    补一下一些小盲区,譬如simpson这种数值积分的方法虽然一直知道,但是从未实现过,做一道例题存一个模板. #pragma warning(disable:4996) #include<iost ...