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 ...
随机推荐
- V2EX社区
无论你是在大学进行人生最重要阶段的学习,或者是在中国的某座城市工作,或者是在外太空的某个天体如 Sputnik 1 上享受人生,在注册进入 V2EX 之后,你都可以为自己设置一个所在地,从而找到更多和 ...
- C# WinForm设置TreeView选中节点
这里假定只有两级节点,多级方法类似.遍历节点,根据选中节点文本找到要选中的节点.treeView.SelectedNode = selectNode; /// <summary> /// ...
- js----DOM的三大节点及部分用法
DOM有三种节点:元素节点.属性节点.文本节点. 一.用nodeType可以检测节点的类型 节点类型 nodeType属性值 元素节点 1 属性节点 2 文本节点 3 这样方便在js中对各个节点进行操 ...
- 从一个URL下载原始数据,基于byte字节,得到byte数组
public static byte[] loadRawDataFromURL(String u) throws Exception { URL url = new URL(u); HttpURLCo ...
- Python脚本控制的WebDriver 常用操作 <十六> 处理对话框
下面将使用webdriver来处理一些页面跳出的对话框事件 测试用例场景 页面上弹出的对话框是自动化测试经常会遇到的一个问题.前端框架的对话框经常是div形式的,下面是一些常见的对话框操作事件: 打开 ...
- django post报403问题
第一个问题是: 我使用jquery的ajax向后台传值, 当使用GET方法时没问题 $.ajax({ type:"GET" url: data: success: }) 但是由于基 ...
- OpenNMS架构介绍
一.OpenNMS简介 OpenNMS的开发基于TMN及FCAPS这两个模型. 电信管理网络(TMN)是由 ITU-T 推荐 M.3000于1985年提出作为一种应用于电信服务供应商所持有的运营支持系 ...
- Uploadify 3.2 上传图片
uploadify version: uploadify 3.2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional// ...
- c++构造函数详解
c++构造函数的知识在各种c++教材上已有介绍,不过初学者往往不太注意观察和总结其中各种构造函数的特点和用法,故在此我根据自己的c++编程经验总结了一下c++中各种构造函数的特点,并附上例子,希望对初 ...
- Javascript Array.prototype.some()
当我们使用数组时,查找数组中包含某个特殊的项是非常常见的动作.下面例子是一个简单的实现: 01 planets = [ 02 "mercury", 03 " ...