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 ...
随机推荐
- 剑指offer--13题
#include "stdafx.h" #include <iostream> using namespace std; void FirstNoRepeatCh(co ...
- 监听HTML input输入框值的即时变化onpropertychange、oninput兼容IE,Chrome,FF,Opera等
转自:http://blog.csdn.net/itchiang/article/details/7769337 要达到的效果 很多情况下我们都会即时监听输入框值的变化,以便作出即时动作去引导浏览者增 ...
- vml 在IE8 不显示的问题, Group不能用等问题.
IE8 不显示的问题: <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" /> ...
- Linux常用命令大全(转载)
系统信息 arch 显示机器的处理器架构(1) uname -m 显示机器的处理器架构(2) uname -r 显示正在使用的内核版本 dmidecode -q 显示硬件系统部件 - (SMBIOS ...
- Hadoop分布式配置
本作品由Man_华创作,采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可.基于http://www.cnblogs.com/manhua/上的作品创作. 请先参照Linux安 ...
- Apache CXF实现Web Service(5)—— GZIP使用
Apache CXF实现Web Service(5)-- GZIP使用 参考来源: CXF WebService整合Spring Apache CXF实现Web Service(1)--不借助重量级W ...
- Unity Texture 2D Compress
测试了一下 unity 图片 对 apk 的影响. 上两种测试环境 1024 * 1024 带 alpha的话 默认压缩就是RBA 16bit就是2M 不带的话就是 etc 的话 ...
- 网格导入设置 Import settings for Meshes
原地址:http://game.ceeger.com/Components/FBXImporter-Model.html The Import Settings for a model file wi ...
- POJ 2007 Scrambled Polygon (简单极角排序)
题目链接 题意 : 对输入的点极角排序 思路 : 极角排序方法 #include <iostream> #include <cmath> #include <stdio. ...
- ExtJs之单选及多选框
坚持 <!DOCTYPE html> <html> <head> <title>ExtJs</title> <meta http-eq ...