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.

C++

class Solution {
bool isAlphaNum(char &ch) {
if (ch >= 'a' && ch <= 'z') return true;
if (ch >= 'A' && ch <= 'Z') return true;
if (ch >= '0' && ch <= '9') return true;
return false;
}
public:
bool isPalindrome(string s) {
int left = 0, right = s.size() - 1 ;
while (left < right) {
if (!isAlphaNum(s[left])) ++left;
else if (!isAlphaNum(s[right])) --right;
else if ((s[left] + 32 - 'a') %32 != (s[right] + 32 - 'a') % 32) return false;
else {
++left; --right;
}
}
return true;
}
bool isPalindrome2(string s){
if("" == s) return true;
int left = 0;
int right = s.size() - 1;
while (left<right){
if(!isAlphaNum(s[left])) left++;
else if (!isAlphaNum(s[right])) right--;
else if ((s[left] + 32 - 'a') %32 != (s[right] + 32 - 'a') % 32) return false;
else { left++; right--;}
}
return true;
}
};

valid-palindrome leetcode C++的更多相关文章

  1. Valid Palindrome [LeetCode]

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

  2. Valid Palindrome leetcode java

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

  3. Valid Palindrome ---- LeetCode 125

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

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

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

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

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

  6. [Leetcode][JAVA] Valid Palindrome

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

  7. 【LeetCode OJ】Valid Palindrome

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

  8. LeetCode——Valid Palindrome

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

  9. [LeetCode] Valid Palindrome II 验证回文字符串之二

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

  10. 【一天一道LeetCode】#125. Valid Palindrome

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

随机推荐

  1. Dockerfile 自动制作 Docker 镜像(三)—— 镜像的分层与 Dockerfile 的优化

    Dockerfile 自动制作 Docker 镜像(三)-- 镜像的分层与 Dockerfile 的优化 前言 a. 本文主要为 Docker的视频教程 笔记. b. 环境为 CentOS 7.0 云 ...

  2. MySQL中的查询事务问题

    之前帮同学做个app的后台,使用了MySQL+MyBatis,遇到了一个查询提交的问题,卡了很久,现在有时间了来复盘下 环境情况 假设有学生表: USE test; CREATE TABLE `stu ...

  3. Vmware 15 安装 win7 虚拟机 (初学者操作与详解教程)

    @ 目录 一.镜像下载 1.什么是镜像 2.常见的系统镜像文件格式 3.下载win7旗舰版镜像 二.VMware Workstation 下载 1.什么是虚拟机 2.VMware 主要功能 3.VMw ...

  4. 利用 uber-go/dig 库管理依赖

    利用 uber-go/dig 库管理依赖 github 地址 官方文档 介绍 dig 库是一个为 go 提供依赖注入 (dependency injection) 的工具包,基于 reflection ...

  5. 关于PHP数组Key的强制类型转换

    PHP是弱类型语言,就像JavaScript一样,在定义变量时,不需要强制指定变量的类型.同时,PHP又有着强大的数组功能,数组的Key即可以是普通的数字类型下标,也可以是字符串类型的Hash键值,那 ...

  6. javascript 面向对象 模块

    * module 完成函数 createModule,调用之后满足如下要求:1.返回一个对象2.对象的 greeting 属性值等于 str1, name 属性值等于 str23.对象存在一个 say ...

  7. python with 线程锁

    import threading import time num = 0 # 全局变量多个线程可以读写,传递数据 mutex = threading.RLock() # 创建一个锁 class Myt ...

  8. Navicat连接数据库成功,新建查询时提示错误“Cannot create file ……”

    Navicat连接数据库成功,新建查询时提示错误"Cannot create file --" 原因:编辑连接{高级}<设置位置>被修改,该oci.dll不正确 解决方 ...

  9. CF1322B-Present【双指针】

    正题 题目链接:https://www.luogu.com.cn/problem/CF1322B 题目大意 给出\(n\)个数字\(a_i\)求 \[\bigoplus _{i=1}^n\bigopl ...

  10. P4542-[ZJOI2011]营救皮卡丘【费用流,Floyd】

    正题 题目链接:https://www.luogu.com.cn/problem/P4542 题目大意 给出\(n+1\)个点\(m\)条边的无向图,\(k\)个人开始在\(0\)号点,一个人进入\( ...