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. yii2-user

    https://github.com/dektrium/yii2-user 安装 : composer require "dektrium/yii2-user:0.9.*@dev" ...

  2. JavaScript本地对象 内置对象 宿主对象

          在ECMAScript中,所有对象并非同等创建的. 一般来说,可以创建并使用的对象有3种:本地对象.内置对象和宿主对象. 1. 本地对象 ECMA-262把本地对象(native obje ...

  3. Visual Studio 编译项目失败,提示找不到文件

     博客地址:http://blog.csdn.net/FoxDave 今天碰到了一个蠢问题,虽然咱们正常情况下是遇不到的,但这确实是个应该注意的地方,所以简单记录一下. Visual Studio ...

  4. shell多行注释

    :<<BLOCK ....被注释的多行内容 BLOCK :<< 'BLOCK ....被注释的多行内容 BLOCK'

  5. vim编辑下Python2.0自动补全

    Python自动补全有vim编辑下和python交互模式下,下面分别介绍如何在这2种情况下实现Tab键自动补全. 一.vim python自动补全插件:pydiction 可以实现下面python代码 ...

  6. webpack脚手架搭建(简单版)

    运行命令 安装依赖:npm install 运行项目:npm start 大致流程 npm init:新建 package.json 将需要的依赖模块加入 dependencies(生产环境) 和 d ...

  7. 2015GitWebRTC编译实录10

    2015.07.20 rtc_p2p编译通过[879/1600 ] CXX obj /webrtc/p2p/client/rtc_p2p.httpportallocator.o[880/1600 ] ...

  8. 创建Windows截图工具的快捷方式

    日常生活中我们会用到好多截图,一般截图我们用QQ自带截图较多,但许多人都忽视了电脑自带截图功能.在我的电脑-->附件-->截图工具   按照上述方式找截图工具比较繁琐,今天我们可以通过快捷 ...

  9. Linux文件操作 笔记

    fstat stat lstat 原型 #include <unistd.h> #include <sys/stat.h> #include <sys/types.h&g ...

  10. listview分页加载

    package com.baway.test; import java.util.ArrayList; import com.baidu.vo.Mydata;import com.baidu.vo.S ...