Valid Palindrome

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.

思想:简单的从两端来逐个读字符,判断是否相等。(36ms)

inline char getLowerChar(string &s, int id) {
if((s[id] >= 'a' && s[id] <= 'z') || (s[id] >= '0' && s[id] <= '9')) return s[id];
else if(s[id] >= 'A' && s[id] <= 'Z') return s[id] + 32;
else return 0;
}
inline char getFirstChar(string &s, int& l) {
while(l < s.size()) {
char ch = getLowerChar(s, l);
if(ch) return ch;
++l;
}
return '*';
}
inline char getLastChar(string &s, int& h) {
while(h >= 0) {
char ch = getLowerChar(s, h);
if(ch) return ch;
--h;
}
return '#';
}
class Solution {
public:
bool isPalindrome(string s) {
int l = 0, h = s.size()-1;
while(l <= h) {
if(getFirstChar(s, l) != getLastChar(s, h))
return false;
++l, --h;
}
return true; }
};

25. Valid Palindrome的更多相关文章

  1. LeetCode 之 Valid Palindrome(字符串)

    [问题描写叙述] Given a string, determine if it is a palindrome, considering only alphanumeric characters a ...

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

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

  3. 【leetcode】Valid Palindrome

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

  4. Leetcode Valid Palindrome

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

  5. [LintCode] Valid Palindrome 验证回文字符串

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

  6. [LeetCode]题解(python):125 Valid Palindrome

    题目来源 https://leetcode.com/problems/valid-palindrome/ Given a string, determine if it is a palindrome ...

  7. [Leetcode][JAVA] Valid Palindrome

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

  8. Valid Palindrome [LeetCode]

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

  9. 【LeetCode OJ】Valid Palindrome

    Problem Link: http://oj.leetcode.com/problems/valid-palindrome/ The following two conditions would s ...

随机推荐

  1. java web图片上传和文件上传

    图片上传和文件上传本质上是一样的,图片本身也是文件.文件上传就是将图片上传到服务器,方式虽然有很多,但底层的实现都是文件的读写操作. 注意事项 1.form表单一定要写属性enctype=" ...

  2. 谈谈UIView的几个layout方法

    谈谈UIView的几个layout方法-layoutSubviews.layoutIfNeeded.setNeedsLayout...   最近在学习swift做动画,用到constraint的动画, ...

  3. [安卓]Android窗口、视图、布局

    1.窗口全屏的两种方法: 转自:http://blog.sina.com.cn/s/blog_4c451e0e010133ab.html 设置全屏包括两个部分: 窗口全屏和Activity全屏.窗口全 ...

  4. (转)jQuery Mobile 移动开发中的日期插件Mobiscroll 2.3 使用说明

    (原)http://www.cnblogs.com/hxling/archive/2012/12/12/2814207.html jQuery Mobile 移动开发中的日期插件Mobiscroll ...

  5. Warning: Attempt to dismiss from view controller <UIViewController: 0x17d71c10> while a presentation or dismiss is in progress!

    昨天 调试程序 已经快要上线了 突然有个BUG 找了半天 才找到是因为这个警告 但是 解决这个警告又花了一天的时间 试了各种消除控制器的方法 都不可用 其中 并且 有这个bug  手机真机测试完全没问 ...

  6. decimalFormat(小数格式)

    这个格式是用来形容小数的,所以只对小数部分起作用 0 一个数字 # 一个数字,不包括 0 (0和#就是一个占位符,有几个就意味着要显示多少位,区别是0 匹配任意数字,#匹配不包括0的任意数字(最后的0 ...

  7. GCC中文手册

    GCC 1 NAME gcc,g++-GNU工程的C和C++编译器(egcs-1.1.2) 总览(SYNOPSIS) gcc[option|filename ]... g++[option|filen ...

  8. VPS折腾

    kali  源: deb http://ppa.launchpad.net/wagungs/kali-linux2/ubuntu raring main deb-src http://ppa.laun ...

  9. android的ArrayMap类

    运行的时候出现: java.lang.NoClassDefFoundError: android.util.ArrayMap http://stackoverflow.com/questions/24 ...

  10. IE中Keep-Alive机制引起的错误

    我们知道Http协议是基于TCP/IP连接的,也就是说客户端浏览器向服务器发出一个Http请求并得到响应是要建立一条TCP/IP连接的,但是如果每发出一个Http请求客户端就要向服务器端建立一条TCP ...