LeetCode OJ:Valid Palindrome(验证回文)
Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
For example,"A man, a plan, a canal: Panama"
is a palindrome."race a car"
is not a palindrome.
验证回文与否,这个估计大家学c语言的时候就都写过:
class Solution {
public:
bool isPalindrome(string s) {
int len = s.length();
for(int i = , j = len - ; i < j ;++i, --j){
while(i < j && (!isalpha(s[i]) && !isdigit(s[i])))
i++;
while(j > i && (!isalpha(s[j]) && !isdigit(s[j])))
j--;
if(isalpha(s[i]) && isalpha(s[j])){
if(toupper(s[i]) == toupper(s[j]))
continue;
else return false;
}else{
if(s[i] == s[j])
continue;
return false;
}
}
return true;
}
};
java版本的代码如下所示,算法没有变化:
public class Solution {
public boolean isPalindrome(String s) {
int sz = s.length();
int beg = 0, end = sz - 1;
while(beg < end){
while(!Character.isLetter(s.charAt(beg)) && !Character.isDigit(s.charAt(beg))){
if(beg < end) beg++;
else return true;
}
while(!Character.isLetter(s.charAt(end)) && !Character.isDigit(s.charAt(end))){
if(beg < end) end--;
else return true;
}
if(Character.toUpperCase(s.charAt(beg)) == Character.toUpperCase(s.charAt(end))){
beg++;
end--;
}else{
return false;
}
}
return true;
}
}
LeetCode OJ:Valid Palindrome(验证回文)的更多相关文章
- [LeetCode] 125. 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] valid palindrome 验证回文
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- [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 ...
- 125 Valid Palindrome 验证回文字符串
给定一个字符串,确定它是否是回文,只考虑字母数字字符和忽略大小写.例如:"A man, a plan, a canal: Panama" 是回文字符串."race a c ...
- LeetCode 125. Valid Palindorme (验证回文字符串)
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- LeetCode Valid Palindrome 有效回文(字符串)
class Solution { public: bool isPalindrome(string s) { if(s=="") return true; ) return tru ...
- [LeetCode] 214. Shortest Palindrome 最短回文串
Given a string s, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- lintcode :Valid Palindrome 有效回文串
题目: 有效回文串 给定一个字符串,判断其是否为一个回文串.只包含字母和数字,忽略大小写. 样例 "A man, a plan, a canal: Panama" 是一个回文. & ...
随机推荐
- C#超级方便的ExpandoObject类别
这东西是.NET Framework 4.5 的新东西..发现这个,大概就跟发现新大陆一样的兴奋,让我再次赞叹Anders Hejlsberg 之神.. 这边有MSDN : http://msdn.m ...
- qt下通过socket传送中文
zz 1.在main函数里我之前就加了一句QTextCodec::setCodecForTr( QTextCodec::codecForLocale() ); 现在再加一句QTextCodec::se ...
- python16_day04【编码、函数、装饰器、包】
一.编码总结 """python2 文件存储默认是ascii方式,启动加#coding:utf8就是文件以utf8方式打开.否则就是以ascii.变量则是str. 例子: ...
- mysql binlog日志的三种模式
1.statement level模式 每一条会修改数据的sql都会记录到master的bin-log中.slave在复制的时候sql进程会解析成和原来master端执行过的相同的sql来再次执行.优 ...
- Linux Shell编程第3章——正则表达式
目录 正则表达式基础 正则表达式的扩展 通配 grep命令 正则表达式基础 Linux Shell以字符串作为表达式向系统传达意思.元字符(Metacharacters)是用来阐述字符表达式意义的字符 ...
- spring和spirngmvc整合
<!-- 需要进行 Spring 整合 SpringMVC 吗 ? 还是否需要再加入 Spring 的 IOC 容器 ? 是否需要再 web.xml 文件中配置启动 Spring IOC 容器的 ...
- HDOJ 1501 Zipper 【DP】【DFS+剪枝】
HDOJ 1501 Zipper [DP][DFS+剪枝] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...
- html 基础--一般标签
<html> --开始标签 <head> 网页上的控制信息 <title>页面标题</title> </head> <body& ...
- 正则表达式和python的re模块
0 正则表达式 0.1 常见的元字符 .: 匹配除\r\n之外的任何单个字符 *: 匹配前面的子表达式任意次,例如Zz*可以匹配Z,可以匹配Zz,也可以匹配Zzzzzzzzzz +: ...
- ASP.NET MVC 处理404与500错误页面的方法
第一步创建ErrorPageController 第二步添加Oops页面 @{ ViewBag.Title = "Oops"; Layout = "~/Areas/Adm ...