Leetcode---Valid Palindrome
来源:互联网 发布:阿里云主机80端口打开 编辑:程序博客网 时间:2024/06/08 06:46
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.
这道题有很多种解法,比如先去掉冗余字符,然后判断一个干净的纯小写字符串;或者两个指针一前一后向中间遍历,遇到冗余字符跳过。。
判断一个纯小写字符串是否是回文也有不同的办法,比如前后两个指针往中间扫;或者找到中点往两边扩展;或者
可以利用回文的子结构性质,DP。公式就是
f(i,j)=f(i+1,j-1)&&s[i]==s[j], 0<=i<j<=n-1
f(i,j)=true, i>=j
我们采用最简单的第一种方法,注意字符串长度为奇偶数时都可以用这段code来做:
- for(int j=0;j<t.length()/2;j++){
- if(t[j]!=t[t.length()-1-j])
- return false;
- }
- return true;
代码如下:
- bool isPalindrome(string s) {
- string t;
- for(int i=0;i<s.length();i++){
- if(s[i]>='a' && s[i]<='z'){
- t+=s[i];
- }
- else if(s[i]>='A' && s[i]<='Z'){
- t+=s[i]-'A'+'a';
- }
- else if(s[i]>='0' && s[i]<='9'){
- t+=s[i];
- }
- }
- for(int j=0;j<t.length()/2;j++){
- if(t[j]!=t[t.length()-1-j])
- return false;
- }
- return true;
- }
0
上一篇:Leetcode---Unique Binary Search Trees II
下一篇:Leetcode---Palindrome Number
相关热门文章
- test123
- 编写安全代码——小心有符号数...
- 使用openssl api进行加密解密...
- 一段自己打印自己的c程序...
- sql relay的c++接口
- 谁能够帮我解决LINUX 2.6 10...
- 现在的博客积分不会更新了吗?...
- shell怎么读取网页内容...
- ssh等待连接的超时问题...
- curl: (56) Recv failure: Con...
给主人留下些什么吧!~~
评论热议
0 0
- [Leetcode] Valid Palindrome
- Leetcode: Valid Palindrome
- leetcode 55: Valid Palindrome
- [LeetCode] Valid Palindrome
- 【leetcode】Valid Palindrome
- LeetCode - Valid Palindrome
- [leetcode]Valid Palindrome
- [LeetCode]Valid Palindrome
- [Leetcode]Valid Palindrome
- LeetCode-Valid Palindrome
- [leetcode] Valid Palindrome
- LeetCode 125: Valid Palindrome
- LeetCode - Valid Palindrome
- leetcode之Valid Palindrome
- LeetCode | Valid Palindrome
- [leetcode]Valid Palindrome
- 【leetcode】Valid Palindrome
- LeetCode题解:Valid Palindrome
- Leetcode---Triangle
- Leetcode---Decode Ways
- Leetcode---Interleaving String
- Summarize 2014 Look Ahead 2015
- Leetcode---Unique Binary Search Trees II
- Leetcode---Valid Palindrome
- Xcode文档使用
- Leetcode---Palindrome Number
- Leetcode---Palindrome Partitioning
- Leetcode---Sudoku Solver
- Leetcode---Surrounded Regions
- 在腾讯实习的那段日子:不要在难受的时候选择 '逃避/离开'
- Leetcode---Word Break II
- Leetcode---Word Break
原创粉丝点击
热门IT博客
热门问题
老师的惩罚
人脸识别
我在镇武司摸鱼那些年
重生之率土为王
我在大康的咸鱼生活
盘龙之生命进化
天生仙种
凡人之先天五行
春回大明朝
姑娘不必设防,我是瞎子
苏州旧货回收
废旧汽车回收公司
回收废锡公司
哪里回收金盐
回收废旧机械设备
回收闲置物品
二手衣物回收
闲置电脑回收
废旧物质回收公司
镀金废料回收
废旧硬盘回收
回收公司电脑
上门回收旧电脑
二手旧电脑回收
回收黄金电话
黄金高价回收
福州回收二手电脑
旧金回收价格
废旧电脑回收公司
废电脑回收公司
废旧玻璃回收
废铁回收多少钱一吨
专业回收废锡
回收二手电脑公司
闲置物品回收
废旧设备回收公司
回收有限公司
上海电脑回收
收购废品回收
废旧物品回收
回收废钢废铁
回收废旧物资
哪里回收废铜
废旧车辆回收
含金废料回收
二手设备回收电话
广州回收公司
废旧钢筋回收
废旧回收价格表
高价回收电脑
废旧家电回收