leetcode: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.
分析:题意为 给出一个字符串,检查它是不是一个回文的情况。判断时只考虑字母数字的字符并且忽略大小写。
注意点 1、考虑字符串为空的情况,并将其作为回文的
2、忽略大小写的差别,比较之前先将大小写转换为一致的
3、非字母数字字符要跳过,将它滤除,得到有效字符串
4、对有效字符串前后相应位置进行比较判断
代码如下:
class Solution {
public:
bool isStr(char &ch){
if(ch >= '0' && ch <= '9'){
return true;
} else if(ch >= 'a' && ch <= 'z'){
return true;
} else if(ch >= 'A' && ch <= 'Z'){
ch += 32;
return true;
}
return false;
}
bool isPalindrome(string s) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int len = s.length();
if(len == 0){
return true;
}
string str = "";
for(int i = 0; i < len; i++){ // remove illegal char, such as "?" "/" ...
if(isStr(s[i])){
str += s[i];
}
}
len = str.length();
int mid = (len + 1) / 2;
for(int i = 0; i < mid; i++){
if(str[i] != str[len - 1 - i]){ // check front and end char
return false;
}
}
return true;
}
};
其他解法:
leetcode: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之“字符串”:Valid Palindrome
题目链接 题目要求: Given a string, determine if it is a palindrome, considering only alphanumeric characters ...
- LeetCode OJ:Valid Palindrome(验证回文)
Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric char ...
- leetcode题解:Valid Palindrome(判断回文)
题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ig ...
- [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][JAVA] Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
随机推荐
- protobuf的安装和使用
以下全部基于win7系统. protobuf是什么,有什么用网上说的已经很多了.这里就是说一下怎么使用.就当给自己做个笔记吧. .proto文件的语法什么的也请网上查看,挺多的. 第一步: 下载pro ...
- Sqli-labs less 26a
Less-26a 这关与26的区别在于,sql语句添加了一个括号,同时在sql语句执行抛出错误后并不在前台页面输出.所有我们排除报错注入,这里依旧是利用union注入. sql语句为SELECT * ...
- SPOJ375 Query on a tree(LCT边权)
之前做了两道点权的LCT,这次做一下边权的LCT.上网找了一下资料,发现对于边权的LCT有这么两种处理方法,一种是每条边建一个点,于是边权就转成点权了.另外一种则是每个边权对应到点权上,也就是每个点对 ...
- HDU 2059 龟兔赛跑(动态规划)
龟兔赛跑 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- GridControl Find/Clear 添加图标
public static void ControlFind(GridControl grid) { FindControl fControl = null; foreach (Control ite ...
- Windows启动系统程序命令
DEVMGMT.MSC - Device Manager 设备管理器 DISKMGMT.MSC - Disk Management 磁盘管理 WindowsXP常用命令http://baike.b ...
- 2013 ACM/ICPC Asia Regional Online —— Warmup
1003 Rotation Lock Puzzle 找出每一圈中的最大值即可 代码如下: #include<iostream> #include<stdio.h> #inclu ...
- dom对象详解--document对象(三)
form对象 form对象代表一个HTML表单,在HTML文档中<form>每出现一次,form对象就会被创建.从dom对象层次图看,document.forms对象是当前文档所有for ...
- JS之DOM(二)
一.DOM节点的操作 1.增加: (1). document.creatElement('标签名');创建元素节点 (2). document.creatTextNode('文本内容'):创建文本节点 ...
- Android上使用MP3格式录制声音
0. 下载LAME 并解压缩 http://lame.sourceforge.net/download.php http://sourceforge.net/projects/lame/files/l ...