【LeetCode OJ】Valid Palindrome
Problem Link:
http://oj.leetcode.com/problems/valid-palindrome/
The following two conditions would simplify the problem:
- only alphanumerci characters considered
- ignoring cases
Given a string, we check if it is a valid palindrome by following rules:
- An empty string is a valid palindrome;
- Letter case should be ignored;
- Blank space should be ignored;
- The string is a valid palindrome only if s[x] == s[n-x] for x = 0,1,..., n/2
Therefore, we design the algorithm that scan the string from both the beginning and the end. Each iteration, we compare them to check if the string is a valid palindrome so far.
The algorithm will terminate if the two characters are not same; when all the characters in the string are compared, the algorithm will return True.
The following code is the python code accepted by oj.leetcode.com.
class Solution:
# @param s, a string
# @return a boolean
n0 = ord('0')
n9 = ord('9')
A = ord('A')
Z = ord('Z')
def isAlphanumeric(self, c):
x = ord(c)
return ( self.n0 <= x <= self.n9 or \
self.A <= x <= self.Z ) def isPalindrome(self, s):
"""
Scan the string from two ends of the string
"""
low = 0
high = len(s)-1
s = s.upper()
while low <= high:
if not self.isAlphanumeric(s[low]):
low += 1
elif not self.isAlphanumeric(s[high]):
high -= 1
elif s[low] == s[high]:
low += 1
high -= 1
else:
return False
return True
【LeetCode OJ】Valid Palindrome的更多相关文章
- 【LeetCode练习题】Valid Palindrome
Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric char ...
- 【LeetCode OJ】Interleaving String
Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...
- 【LeetCode OJ】Reverse Words in a String
Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...
- LeetCode OJ:Valid Palindrome(验证回文)
Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric char ...
- 【LeetCode OJ】Palindrome Partitioning
Problem Link: http://oj.leetcode.com/problems/palindrome-partitioning/ We solve this problem using D ...
- 【LeetCode OJ】Palindrome Partitioning II
Problem Link: http://oj.leetcode.com/problems/palindrome-partitioning-ii/ We solve this problem by u ...
- 【LeetCode OJ】Gas Station
Problem link: http://oj.leetcode.com/problems/gas-station/ We can solve this problem by following al ...
- 【LeetCode OJ】Word Break II
Problem link: http://oj.leetcode.com/problems/word-break-ii/ This problem is some extension of the w ...
- 【leetcode dp】132. Palindrome Partitioning II
https://leetcode.com/problems/palindrome-partitioning-ii/description/ [题意] 给定一个字符串,求最少切割多少下,使得切割后的每个 ...
随机推荐
- 20150206读书笔记<深入理解计算机系统>
●第一章 C是系统级编程的首选.C++显示支持抽象,属于应用级程序设计语言. 简单例子: 一个典型系统的硬件组成: 存储器的层次结构: 注:存储器层次结构的设计思想是,该层存储器作为下一层存储器的高速 ...
- Search for a Range [LeetCode]
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- springMvc配置编码过滤器
在web.xml中配置 <!-- 编码过滤器 --> <filter> <filter-name>characterEncodingFilter</filte ...
- Oracle函数大全之转换函数
chartorowid(c1) [功能]转换varchar2类型为rowid值 [参数]c1,字符串,长度为18的字符串,字符串必须符合rowid格式 [返回]返回rowid值 [示例] SELECT ...
- hdu 4622 Reincarnation
http://acm.hdu.edu.cn/showproblem.php?pid=4622 用字典树把每一个字符串对应成一个整数 相同的字符串对应到相同的整数上 把所用的串对应的整数放在一个数组里 ...
- Exif的Orientation信息说明
EXIF Orientation 参数让你随便照像但都可以看到正确方向的照片而无需手动旋转(前提要图片浏览器支持,Windows 自带的不支持) 这个参数在佳能.尼康相机照的照片是自带的,但我的奥林巴 ...
- [开发笔记]-获取歌曲ID3信息
ID3介绍: ID3,一般是位于一个mp3文件的开头或末尾的若干字节内,附加了关于该mp3的歌手,标题,专辑名称,年代,风格等信息,该信息就被称为ID3信息,ID3信息分为两个版本,v1和v2版. 获 ...
- Linux-如何查看登陆shell的类型
输入一个系统不认识的命令(如#ig)获得系统提示 aix/#ig ksh ig not found #echo $ (适用sh/ksh) aix/#echo $ ksh #echo $SHELL(用户 ...
- POJ 2992 求组合数的因子个数
求C(n,k)的因子个数 C(n,k) = (n*(n-1)*...*(n-k+1))/(1*2*...*k) = p1^k1 * p2^k2 * ... * pt^kt 这里只要计算出分子中素数因子 ...
- 中国广核集团:BPM与ERP紧密结合
全球能源消耗不断增长,电能已经达到了无可替代的位置.同时,传统的电力供应模式正在受新模式的影响,营造更具价值的生态系统.面对挑战,核电企业在提高能效并降低成本的同时,也迫切需要进行转型.面对公众对可再 ...