【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/ [题意] 给定一个字符串,求最少切割多少下,使得切割后的每个 ...
随机推荐
- sina 行情api
http://blog.csdn.net/simon803/article/details/7784682
- Qt之可重入与线程安全
简述 本篇文章中,术语"可重入性"和"线程安全"被用来标记类与函数,以表明它们如何被应用在多线程应用程序中. 一个线程安全的函数可以同时被多个线程调用,甚至调用 ...
- hdu-----2491Priest John's Busiest Day(2008 北京现场赛G)
Priest John's Busiest Day Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Jav ...
- IE5,IE6,IE7,IE8的css兼容性列表[转自MSDN]
CSS 2.1: IE 5.0 IE 5.5 IE 6.0 IE 7.0 IE8 Beta 1 IE8 Beta 2 IE 8.0 @charset No Yes Yes Yes Yes Yes ...
- PowerDesigner中导出设计说明文档
点击下图的新建按钮,新建一个导出内容的模板 模板设计界面分为2栏,左边是可选的模板内容,右侧是模板,双击左侧条目会添加到右侧,最后生成的文件中就有此项内容. 已经添加到右侧的内容可以编辑,双击右侧的条 ...
- JavaScript prototype 属性
prototype 属性使开发人员有能力向对象添加属性和方法. 语法 object.prototype.name=value 实例 在本例中,我们将展示如何使用 prototype 属性来向对象添加属 ...
- bzoj 2049: [Sdoi2008]Cave 洞穴勘测
#include<cstdio> #include<iostream> using namespace std; ][],n,m,fa[],st[]; ]; bool isro ...
- POJ 1077 && HDU 1043 Eight A*算法,bfs,康托展开,hash 难度:3
http://poj.org/problem?id=1077 http://acm.hdu.edu.cn/showproblem.php?pid=1043 X=a[n]*(n-1)!+a[n-1]*( ...
- 长理ACM 14-星期几(谌海军)
题目描述:编一个程序,已知今天是星期几,计算出n天后是星期几.要求使用枚举变量. 输入描述:输入为两个正整数,第一个数n(n<=6)表示今天是星期几,第二个数m(m<=1000),表示求m ...
- 【STL】-pair的用法
初始化: std::pair<int, float> p; //initialize p.first and p.second with zero std::pair<int, co ...