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:

  1. An empty string is a valid palindrome;
  2. Letter case should be ignored;
  3. Blank space should be ignored;
  4. 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的更多相关文章

  1. 【LeetCode练习题】Valid Palindrome

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

  2. 【LeetCode OJ】Interleaving String

    Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...

  3. 【LeetCode OJ】Reverse Words in a String

    Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...

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

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

  5. 【LeetCode OJ】Palindrome Partitioning

    Problem Link: http://oj.leetcode.com/problems/palindrome-partitioning/ We solve this problem using D ...

  6. 【LeetCode OJ】Palindrome Partitioning II

    Problem Link: http://oj.leetcode.com/problems/palindrome-partitioning-ii/ We solve this problem by u ...

  7. 【LeetCode OJ】Gas Station

    Problem link: http://oj.leetcode.com/problems/gas-station/ We can solve this problem by following al ...

  8. 【LeetCode OJ】Word Break II

    Problem link: http://oj.leetcode.com/problems/word-break-ii/ This problem is some extension of the w ...

  9. 【leetcode dp】132. Palindrome Partitioning II

    https://leetcode.com/problems/palindrome-partitioning-ii/description/ [题意] 给定一个字符串,求最少切割多少下,使得切割后的每个 ...

随机推荐

  1. Spring 框架整理

    在web.xml中配置以下内容 <!-- 配置Spring MVC DispatcherServlet --> <servlet> <servlet-name>MV ...

  2. 自定义jQuery插件Step by Step

    1.1.1 摘要 随着前端和后端技术的分离,各大互联网公司对于 Mobile First理念都是趋之若鹜的,为了解决网页在不同移动设备上的显示效果,其中一个解决方案就是Responsive Desig ...

  3. SQL Server 自定义函数(1)把某一列多行的值拼接成一个字符串

    ) SET @resultstr='' SELECT @resultstr=@resultstr+'|'+[BaseImage] FROM dbo.life_fc PRINT @resultstr 示 ...

  4. Objective-C:@property参数详解

    格式:@property(param1, param2) 类型 属性名 参数可有可无 三类参数: 1.读写属性:readwrite/readonly readwrite:产生setter\getter ...

  5. 一个关于qml插件的文章-转

    制作Qt Quick 2 Extension Plugin的几个问题-Qt 经过几天的google和爬帖,加上自己的摸索,终于把新版的Qt Quick 2制作插件的问题给弄了个明白,工作流可以建立了. ...

  6. js 数组去除空值

    for(var i = 0 ;i<wordarr.length;i++)                {                    if(wordarr[i] == "& ...

  7. [css]邮件的写法

    <style type="text/css">        /* Client-specific Styles */        #outlook a{paddin ...

  8. PowerMock使用遇到的一些问题

    首先使用PowerMock  Mock对象如果不成功的话首先要检查在测试类上是否有这两个声明@RunWith(PowerMockRunner.class)                        ...

  9. 判断comboBox是否选对了绑定的数据库中的项

    实现: comboBox1下拉列表已绑定数据库,将选中的项保存到数据库时,判断是否已选中下拉列表里的项 如果没选中,或者输入了其他的值,和已绑定的数据不匹配,出现提示框 按钮的点击事件中: strin ...

  10. ZOJ3675:Trim the Nails

    Robert is clipping his fingernails. But the nail clipper is old and the edge of the nail clipper is ...