题目链接

  题目要求:

  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.

  程序如下:

 class Solution {
public:
bool isPalindrome(string s) {
if(!s.empty() && s.front() == ' ')
s.erase(, );
if(!s.empty() && s.back() == ' ')
s.pop_back(); bool ret = true;
int sz = s.size();
if(sz == )
return true; int i = ;
int j = sz - ;
while(i < j)
{
while(i < sz && !isalnum(s[i]))
i++;
while(j > - && !isalnum(s[j]))
j--;
if(i < sz && j > - && s[i] != s[j])
{
if(tolower(s[i]) != tolower(s[j]))
{
ret = false;
break;
}
}
i++;
j--;
} return ret;
}
};

LeetCode之“字符串”:Valid Palindrome的更多相关文章

  1. LeetCode 680. 验证回文字符串 Ⅱ(Valid Palindrome II) 1

    680. 验证回文字符串 Ⅱ 680. Valid Palindrome II 题目描述 给定一个非空字符串 s,最多删除一个字符.判断是否能成为回文字符串. 每日一算法2019/5/4Day 1Le ...

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

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

  3. 【LeetCode】125. Valid Palindrome 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 列表生成式 正则表达式 双指针 日期 题目地址:https:/ ...

  4. 【LeetCode OJ】Valid Palindrome

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

  5. 【LeetCode练习题】Valid Palindrome

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

  6. 【LeetCode】125. Valid Palindrome

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

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

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

  8. LeetCode(125)题解--Valid Palindrome

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

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

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

  10. 【LeetCode】680. Valid Palindrome II 验证回文字符串 Ⅱ(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 思路来源 初版方案 进阶方案 日期 题目地址 ...

随机推荐

  1. WmS详解(一)之token到底是什么?基于Android7.0源码

    做Android有些年头了,Framework层三大核心View系统,WmS.AmS最近在研究中,这三大块,每一块都够写一个小册子来介绍,其中View系统的介绍,我之前有一个系列的博客(不过由于时间原 ...

  2. #pragma pack(x) CPU对齐

    编译器会尽量把成员对齐以提高内存的命中率.对齐是可以更改的,使用"#pragma pack(x)" 可以改变编译器的对齐方式. C++固有类型的对界取编译器对齐方式与自身大小中较小 ...

  3. Shell脚本编程入门(一)

    最近在学shell,记录一下. if语句的使用: 1.判断两个参数大小 #!/bin/sh #a test about if statement a=10 b=20 if [ $a -eq $b ]; ...

  4. java虚拟机工具入门

    jps 能显示现在都有那些java程序运行 C:\Users\Administrator>jps 16964 DeadLockJstack 9172 PULSEI~1.JAR 19392 Jps ...

  5. 关于USB驱动的软件测试方法

    在工作中难免会使用一些外部设备挂载到平台进行测试,比如U盘,那么判断一个U盘是否能正常读写的方法如下: 1.在U盘中放入一个二进制文件(xxx.bin) 2.通过U盘在软件上读取该二进制文件,并计算其 ...

  6. 06_NoSQL数据库之Redis数据库:Redis的高级应用之登录授权和主从复制

     Redis高级实用特征 安全性(登录授权和登录后使用auth授权) 设置客户端连接后进行任何其他指定前需要使用的密码. 警告:因为redis速度相当快,所以在一台比较好的服务器下,一个外部的用户 ...

  7. UNIX网络编程——使用select函数编写客户端和服务器

    首先看原先<UNIX网络编程--并发服务器(TCP)>的代码,服务器代码serv.c: #include<stdio.h> #include<sys/types.h> ...

  8. TCP/IP入门(1) --链路层

    /** 本博客由汗青ZJF整理并发布, 转载请注明出处: http://blog.csdn.net/zjf280441589/article/category/1854365 */ TCP/IP体系结 ...

  9. 【一天一道LeetCode】#130. Surrounded Regions

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

  10. android studio2.0出现的gradle问题,instant Run即时运行不了.

    android studio 2.0出现的gradle问题: instant Run即时运行不了.经历了几乎9个preView版本的AS2.0,终于迎来了正式版,然而晴天我的霹雳,好不容易装好的2.0 ...