博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Leetcode 38] 125 Valid Palindrome
阅读量:4604 次
发布时间:2019-06-09

本文共 1804 字,大约阅读时间需要 6 分钟。

Problem:

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.

 

Analysis:

Get two pointer pi, pj, pi points to the head of the string, pj points to the end of the string. 

Then compare the two pointed characters, if they are the same, then pi++, pj--, repeat the comparision.

If they are not the same, return false;

 

Code:

1 class Solution { 2 public: 3     bool isPalindrome(string s) { 4         // Start typing your C/C++ solution below 5         // DO NOT write int main() function 6         if (s == "") return true; 7          8         int i = 0, j = s.size()-1; 9         while (true) {10             while (!((s[i]>='0'&&s[i]<='9') || (s[i]>='a' && s[i]<='z')11                     || (s[i]>='A' && s[i]<='Z')))12                     if ((i++) >= s.size()) break;13                     14             while (!((s[j]>='0'&&s[j]<='9') || (s[j]>='a' && s[j]<='z')15                     || (s[j]>='A' && s[j]<='Z')))16                     if ((j--) < 0) break;17                     18             if (i >= j) break;19             20             if (toLower(s[i]) != toLower(s[j]))21                 return false;22             23             i++;24             j--;25         }26         27         return true;28     }29     30     char toLower(char a) {31         if (a>='A' && a<= 'Z')32             a += 32;33             34         return a;35     }36     37     38 };
View Code

 

Attention:

转载于:https://www.cnblogs.com/freeneng/archive/2013/05/24/3096220.html

你可能感兴趣的文章
C2. Power Transmission (Hard Edition)(线段相交)
查看>>
STM32F0使用LL库实现SHT70通讯
查看>>
Atitit. Xss 漏洞的原理and应用xss木马
查看>>
MySQL源码 数据结构array
查看>>
(文件过多时)删除目录下全部文件
查看>>
T-SQL函数总结
查看>>
python 序列:列表
查看>>
web移动端
查看>>
pythonchallenge闯关 第13题
查看>>
linux上很方便的上传下载文件工具rz和sz使用介绍
查看>>
React之特点及常见用法
查看>>
【WEB前端经验之谈】时间一年半,或沉淀、或从零开始。
查看>>
优云软件助阵GOPS·2017全球运维大会北京站
查看>>
linux 装mysql的方法和步骤
查看>>
poj3667(线段树区间合并&区间查询)
查看>>
51nod1241(连续上升子序列)
查看>>
SqlSerch 查找不到数据
查看>>
集合相关概念
查看>>
Memcache 统计分析!
查看>>
(Python第四天)字符串
查看>>