【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/ [题意] 给定一个字符串,求最少切割多少下,使得切割后的每个 ...
随机推荐
- Asp.net 解析json
Asp.net Json数据解析的一种思路 http://www.cnblogs.com/scy251147/p/3317366.html http://tools.wx6.org/json2csha ...
- JSON生成c#类代码小工具(转)
原文地址: http://www.cnblogs.com/tianqiq/archive/2015/03/02/4309791.html
- SqlServer中把结果集放到到临时表的方法
一. SELECT INTO 1. 使用select into会自动生成临时表,不需要事先创建 select * into #temp from sysobjects 01. 把存储过程结 ...
- Objective-C:Foundation框架-常用类-NSNull
集合中是不能存储nil值的,因为nil在集合中有特殊含义,但有时确实需要存储一个表示“什么都没有”的值,那么可以使用NSNull,它也是NSObject的一个子类. #import <Found ...
- 线性渐变--linear-gradient
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head> < ...
- Codeforces Round #308 (Div. 2)----C. Vanya and Scales
C. Vanya and Scales time limit per test 1 second memory limit per test 256 megabytes input standard ...
- 319. Bulb Switcher——本质:迭代观察,然后找规律
There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every ...
- Java中的String与常量池[转帖]
string是java中的字符串.String类是不可变的,对String类的任何改变,都是返回一个新的String类对象.下面介绍java中的String与常量池. 1. 首先String不属于8种 ...
- NoSQL分类
NoSQL数据库分类: NoSQL DEFINITION:Next Generation Databases mostly addressing some of the points: beingno ...
- 织梦dedecms分类信息模型上一页下一页失效办法
修改文件/include/arc.archives.class 将一下代码 $next = (is_array($nextR) ? " where arc.id={$nextR['id']} ...