lintcode :Valid Palindrome 有效回文串
题目:
给定一个字符串,判断其是否为一个回文串。只包含字母和数字,忽略大小写。
"A man, a plan, a canal: Panama" 是一个回文。
"race a car" 不是一个回文。
你是否考虑过,字符串有可能是空字符串?这是面试过程中,面试官常常会问的问题。
在这个题目中,我们将空字符串判定为有效回文。
O(n) 时间复杂度,且不占用额外空间。
解题:
去除非有效字符后,整体是个回文串,两边查找,利用快速排序的思想,通过while找到有效的字符串
Java程序:
public class Solution {
/**
* @param s A string
* @return Whether the string is a valid palindrome
*/
public boolean isPalindrome(String s) {
// Write your code here
if(s.equals("")) return true;
int len = s.length();
int left = 0;
int right = len - 1;
s = s.toLowerCase();
while(left < right){
char leftchar = s.charAt( left );
char rightchar = s.charAt( right );
while(!isValid(leftchar)){
left ++;
leftchar = s.charAt( left );
if(left>=right) return true;
}
while(!isValid(rightchar)){
right --;
rightchar = s.charAt( right );
if(right<=left) return true;
}
if(leftchar != rightchar)
return false;
left ++;
right --;
}
return true;
}
public boolean isValid(char ch){
if(ch>='a' && ch <= 'z')
return true;
if(ch >='0' && ch <= '9')
return true;
return false;
}
}
总耗时: 14651 ms
Python程序:
class Solution:
# @param {string} s A string
# @return {boolean} Whether the string is a valid palindrome
def isPalindrome(self, s):
# Write your code here
if s.isspace() or s=="":
return True
s = s.lower()
left = 0
right = len(s) - 1
while left< right:
while (self.isValid(s[left]))==False:
left += 1
if left>= right:
return True
while (self.isValid(s[right])) ==False:
right -=1
if left>=right:
return True
if s[left]!= s[right]:
return False
left += 1
right -= 1 return True def isValid(self,ch): # isalnum()
if ch.isalpha():
return True
if ch.isdigit():
return True
return False
总耗时: 825 ms
lintcode :Valid Palindrome 有效回文串的更多相关文章
- [leetcode]125. Valid Palindrome判断回文串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- [LintCode] Valid Palindrome 验证回文字符串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- [LeetCode] Valid Palindrome 验证回文字符串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- [LeetCode] Shortest Palindrome 最短回文串
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- [LeetCode] 214. Shortest Palindrome 最短回文串
Given a string s, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- poj 3280 Cheapest Palindrome ---(DP 回文串)
题目链接:http://poj.org/problem?id=3280 思路: dp[i][j] :=第i个字符到第j个字符之间形成回文串的最小费用. dp[i][j]=min(dp[i+1][j]+ ...
- poj3280 Cheapest Palindrome(回文串区间dp)
https://vjudge.net/problem/POJ-3280 猛刷简单dp第一天第三题. 这个据说是[求字符串通过增减操作变成回文串的最小改动次数]的变体. 首先增减操作的实质是一样的,所以 ...
- bzoj 3768: spoj 4660 Binary palindrome二进制回文串
Description 给定k个长度不超过L的01串,求有多少长度为n的01串S满足: 1.该串是回文串 2.该串不存在两个不重叠的子串,在给定的k个串中. 即不存在a<=b<c<= ...
- LeetCode Valid Palindrome 有效回文(字符串)
class Solution { public: bool isPalindrome(string s) { if(s=="") return true; ) return tru ...
随机推荐
- easyUI中treegrid组件构造树形表格(简单数据类型)+ssm后台
这几天做的项目要求用树形表格的形式展示一部分数据,于是就想到了使用easyUI的treegrid组件,但几经翻查各种资料,发现数据类型大多采取标准数据类型,即包含children元素的数据类型,小编查 ...
- Ubuntu下设置Tomcat成为服务(开机启动)
1.将tomcat安装目录下bin文件夹中的catalina.sh拷贝到/etc/init.d下并修改名称为tomcat cp /path/to/tomcat/bin/catalina.sh /et ...
- Discuz X3.2 SEO设置 title 不支持空格的解决方法
很多使用 Discuz X3.2 的同学都发现这么一个问题:在后台SEO设置-title设定的时候,即使你在连字符两侧输入了空格,在前台也显示不出来,很多同学纠结这个问题,今天终于找到了解决方法,在此 ...
- Word 使用技巧
文档的写作,例来分为latex与word两大阵营.一个是论文界的宠儿,一个是平民的所见即所得.看起来好像前者更加牛一些. 本来我也是觉得latex比word好.但是使用latex时苦于找不到一个好的编 ...
- linux 线程笔记
线程与进程关键字对比 创建新流 fork/pthread_create 退出控制流 exit/pthread_exit 获取退出状态 waitpid/pthread_join 在退出时的清理工作 at ...
- 为什么我们使用192.168.0.1作为内网ip
私有IP地址是一段保留的IP地址.只是使用在局域网中,在Internet上是不使用的. 私有IP地址的范围有: 10.0.0.0-10.255.255.255 172.16.0.0—172.31.25 ...
- PHP dirname() 函数 __FILE__ __DIR__
__DIR__返回文件所处的目录,除非是根目录,否则末尾不带\ __FILE__ 返回文件路径 dirname(__DIR__) 文件所处目录的上级目录,末尾也不带斜杠
- js 截取某个字符前面或者后面的字符串
/* string 字符串; str 指定字符; split(),用于把一个字符串分割成字符串数组; split(str)[0],读取数组中索引为0的值(第一个值),所有数组索引默认从0开始; */ ...
- F1
----------------------------------------------------------------------------Welcome to the MASM32 SD ...
- 从地址栏输入url到显示页面都发生了什么?
作为一个软件开发者,你一定会对网络应用如何工作有一个完整的层次化的认知,同样这里也包括这些应用所用到的技术:像浏览器,HTTP,HTML,网络服务器,需求处理等等. 本文将更深入的研究当你输入一个网址 ...