25. Valid Palindrome
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的更多相关文章
- LeetCode 之 Valid Palindrome(字符串)
		[问题描写叙述] Given a string, determine if it is a palindrome, considering only alphanumeric characters a ... 
- [LeetCode] Valid Palindrome 验证回文字符串
		Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ... 
- 【leetcode】Valid Palindrome
		题目简述: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ... 
- Leetcode Valid Palindrome
		Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ... 
- [LintCode] Valid Palindrome 验证回文字符串
		Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ... 
- [LeetCode]题解(python):125 Valid Palindrome
		题目来源 https://leetcode.com/problems/valid-palindrome/ Given a string, determine if it is a palindrome ... 
- [Leetcode][JAVA] Valid Palindrome
		Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ... 
- Valid Palindrome [LeetCode]
		Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ... 
- 【LeetCode OJ】Valid Palindrome
		Problem Link: http://oj.leetcode.com/problems/valid-palindrome/ The following two conditions would s ... 
随机推荐
- 搭建web服务器环境
			一. 安装apache 安装好之后测试:浏览器地址栏输入:localhost,若弹出"It works!"表明已成功安装. 管理方式:1.通过Apache自带的镜像管理器:2.wi ... 
- Android中Handler的消息处理
			关于Handler机制,能找到无数的文章.http://blog.csdn.net/jiangshitian/article/details/12525313这篇博客写得比较好,关键是图很清晰,结合H ... 
- Annotation注解(有源代码)
			注解(annotation)概述: ·从JDK5.0 开始,java增加了对元数据(MetaData)的支持,也就是Annotation(注解) ·Annotation其实就是代码里的特殊标记,这些标 ... 
- Android四大组件之Activity(活动)及其布局的创建与加载布局
			Android四大组件之Activity(活动)及其布局的创建与加载布局 什么是Activity ? 活动(Activity)是包含用户界面的组件,主要用于和用户进行交互的,一个应用程序中可以包含零个 ... 
- 2014年5月份第2周51Aspx源码发布详情
			Reapter手写分页控件源码 2014-5-12 [VS2010]源码描述:实现repeater控件分页,方便好用,界面设计也很漂亮.数据库是Access,可直接运行.入口是RepeaterTes ... 
- 初用Ubuntu常见问题及解决方案之一
			1.我的Vaio Fit15e的无线网卡是BCM43142,装了Ubuntu后为了装驱动折腾了好久,因为这款网卡在Broadcom官网都找不到驱动,google了一大圈,一个最简单的命令可以解决这个问 ... 
- Ztree使用笔记
			在项目中需要用到树,使用了Ztree.(官网地址:http://www.treejs.cn/v3/main.php#_zTreeInfo,介绍很详细,有API,有demo) 1.初始化树: $.f ... 
- Winform基础知识
			1.关于登陆部分 this.DialogResult = DialogResult.OK; this.Close(); FrmLogin login = new FrmLogin(m_CurUser) ... 
- UIkit框架之UIwebview
			1.继承链:UIview:UIResponder:NSObject 2.使用loadHTMLString:baseURL:方法来加载本地的HTML文件,或者使用 loadRequest:方法来加载网络 ... 
- 监听调试web service的好工具TCPMon
			监听调试web service的好工具TCPMonhttp://ws.apache.org/commons/tcpmon/download.cgi TCPMon Tutorial Content In ... 
