leetCode(51):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.
Note:
Have you consider that the string might be empty? This is a good question to ask during an interview.
For the purpose of this problem, we define empty string as valid palindrome.
//用两个指针实现。前提是要忽略其它符号
class Solution {
public:
bool isPalindrome(string s) {
if(s.empty())
return true;
int i=0;
int j=s.size()-1;
for(;i<j;i++)
{//i后移
if((s[i]>='a' && s[i]<='z') || (s[i]>='A' && s[i]<='Z') || (s[i]>='0' && s[i]<='9'))
{//找到第一个为字符或数字的字符
for(;j>i;--j)
{//j前移
if((s[j]>='a' && s[j]<='z') || (s[j]>='A' && s[j]<='Z') || (s[j]>='0' && s[j]<='9'))
{//找到第一个为字符或数字的字符
if(s[i]==s[j] || s[i]+'A'-'a'==s[j] || s[i]+'a'-'A'==s[j])
{//找到后时行推断
j--;//前进一步
break;
}
else
return false;
}
}
}
}
return true;
}
};
leetCode(51):Valid Palindrome的更多相关文章
- [LeetCode] 680. Valid Palindrome II 验证回文字符串 II
Given a non-empty string s, you may delete at most one character. Judge whether you can make it a pa ...
- [Leetcode][JAVA] Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- [leetcode] 1. Valid Palindrome
leetcode的第一题,回文数判断. 原题如下: For example, "A man, a plan, a canal: Panama" is a palindrome. & ...
- LeetCode 1216. Valid Palindrome III
原题链接在这里:https://leetcode.com/problems/valid-palindrome-iii/ 题目: Given a string s and an integer k, f ...
- [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 ...
- 【题解】【字符串】【Leetcode】Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- leetcode 125. Valid Palindrome ----- java
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 a ...
随机推荐
- 算法复习——状压dp
状压dp的核心在于,当我们不能通过表现单一的对象的状态来达到dp的最优子结构和无后效性原则时,我们可能保存多个元素的有关信息··这时候利用2进制的01来表示每个元素相关状态并将其压缩成2进制数就可以达 ...
- codeforces 900D 数论+组合+容斥原理
问有多少个这样的数字序列 所有数的GCD等于x 并且 所有数的和等于y 题解: 非常难有思路啊 看题解后过的. 考虑序列GCD为x的倍数 即GCD = n*x 和当然都为y 这个条件不要忘了 这样我们 ...
- POJ3671 Dining Cows
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8126 Accepted: 3441 Description The c ...
- POJ3625 Building Roads
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10803 Accepted: 3062 Description Fa ...
- 【BZOJ1901】Dynamic Rankings(树套树,树状数组,主席树)
题意:给定一个N个数的序列,要求维护一个数据结构支持以下两种操作: 1:将第X个数改成Y 2:查询第X到第Y个数里第K小的数是多少 n,m<=10000,a[i]<=10^9 思路:单点修 ...
- J2ME开发基本语法及小实例专题
原文发布时间为:2008-07-31 -- 来源于本人的百度文章 [由搬家工具导入] http://www.wcplym.com/sbjtClassArtl.asp?id=4 ·Canva专题:漂亮的 ...
- [leetcode] permutations 排列
写了两个,一个是直接的递归实现: class Solution { public: void swap(vector<int> &num,int left,int right) { ...
- 标准C程序设计七---111
Linux应用 编程深入 语言编程 标准C程序设计七---经典C11程序设计 以下内容为阅读: <标准C程序设计>(第7版) 作者 ...
- Scrollview总结:滑动问题、监听Scrollview实现头部局改变
ScrollView就是一个可以滚动的View,这个滚动的方向是垂直方向的,而HorizontalScrollView则是一个水平方向的可以滚动的View. ScrollView的简单介绍 Scrol ...
- Unity3d Inspector面板实现set/get访问器
简单说一下属性和字段的区别:字段就是成员变量,而属性确实提供给外部访问内部成员变量的接口.之所以会有属性的出现,就是为了避免外部对类的成员的直接访问,通俗的说就是OOP中的封装思想. using Un ...