【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/ [题意] 给定一个字符串,求最少切割多少下,使得切割后的每个 ...
随机推荐
- java 三大框架 介绍
三大框架:Struts+Hibernate+Spring Java三大框架主要用来做WEN应用. Struts主要负责表示层的显示 Spring利用它的IOC和AOP来处理控制业务(负责对数据库的操作 ...
- PHP程序员面试技巧之口试题分享
网络上流传很广的一部分php工程师面试题目,有些phper们认为这些很形式,天下面试题目一大把,不能考核一个人的真实水平,其实细细研究起来,无论怎样,能存在就表明其有存在的价值.下面小编整理了12条P ...
- Mybatis 实用
1.<delete id="removeZtreeS" parameterType="String"> DELETE FROM sys_fun WH ...
- MVC 与传统的 webform 的比较
代码架构方式 ASP 脚本语言和代码同置,每个请求页面对应一个物理文件 WebForm 代码后置 ,每个请求页面对应dll和一个.asp物理文件 MVC 代码分离,每个请求对应一个Action和一个V ...
- 转:Struts2<s:iterator value="" var="lst">中var的使用和一些标签的使用体会
比如<s:iterator value="pmOperateList" var="lst"> <!-- iterator加上var 等价于重新 ...
- 【转载】如何在德州仪器网站查找和下载PCB封装
德州仪器的网站做得相当不错,查找IC资料和下载IC封装样样给力.那么如何在TI网站上能够快速查找到自已需要的PCB封装呢?下面我来告诉你. 1. 在浏览器中输入网址http://weben ...
- 使用DD_belatedPNG让IE6支持PNG透明图片
使用DD_belatedPNG让IE6支持PNG透明图片 众所周知IE6不支持透明的PNG图片,而PNG图片在Web设计方面表现力上,具有其它图形格式所达不到的效果,IE6这一致命缺陷极大地限制了We ...
- [转]JDK6和JDK7中的substring()方法
substring(int beginIndex, int endIndex)在JDK6与JDK7中的实现方式不一样,理解他们的差异有助于更好的使用它们.为了简单起见,下面所说的substring() ...
- hdu 4616 Game
http://acm.hdu.edu.cn/showproblem.php?pid=4616 要记录各种状态的段 a[2][4] a[0][j]表示以trap为起点一共有j个trap的最优值 a[1 ...
- 二模 (5) day2
第一题: 有 N 个人顺时针围在一圆桌上开会,他们对身高很敏感. 因此决定想使得任意相邻的两人的身高差距最大值最小. 如果答案不唯一,输出字典序最小的排列,指的是身高的排列.N<=50 解题过程 ...