LeetCode(125) 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.
分析
判断给定字符串是否为忽略大小写,特殊符号,空格等之后的回文串。
AC代码
class Solution {
public:
bool isPalindrome(string s) {
if (s.empty())
return true;
int size = s.length();
int lhs = 0, rhs = size - 1;
while (lhs < rhs)
{
if (!isalpha(s[lhs]))
{
++lhs;
continue;
}
if (!isalpha(s[rhs]))
{
--rhs;
continue;
}//if
if (s[lhs] != s[rhs])
return false;
else
{
++lhs;
--rhs;
}
}//while
return true;
}
//判断是否是字母数字,如果是大写字母则将其转化为小写字母
bool isalpha(char &c){
if ((c >= 'A'&&c <= 'Z')){
c = c - 'A' + 'a';
return true;
}
return (c >= 'a'&&c <= 'z') || (c >= '0'&&c <= '9');
}
};
LeetCode(125) Valid Palindrome的更多相关文章
- LeetCode(36)Valid Sudoku
题目 Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...
- LeetCode(242)Valid Anagram
题目 Given two strings s and t, write a function to determine if t is an anagram of s. For example, s ...
- LeetCode(49)-Valid Parentheses
题目: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the ...
- LeetCode(65) Valid Number
题目 Validate if a given string is numeric. Some examples: "0" => true " 0.1 " ...
- LeetCode(20)Valid Parentheses
题目 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the i ...
- LeetCode(125):验证回文串
Easy! 题目描述: 给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写. 说明:本题中,我们将空字符串定义为有效的回文串. 示例 1: 输入: "A man, ...
- Leetcode(5)最长回文子串
Leetcode(4)寻找两个有序数组的中位数 [题目表述]: 给定一个字符串 s,找到 s 中 最长 的回文子串.你可以假设 s 的最大长度为 1000.' 第一种方法:未完成:利用回文子串的特点 ...
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
随机推荐
- UVA10129:Play on Words(欧拉回路)
Some of the secret doors contain a very interesting word puzzle. The team of archaeologists has to s ...
- 洛谷 P1094 纪念品分组
P1094 纪念品分组 先按价格对纪念品排序(这里是从大到小),然后从两端向中心开始配对,有两个变量i和j,表示正在处理的两个纪念品编号,开始时i=1,j=n,如果a[i]+a[j]>w则第i贵 ...
- UG 常用设置
Q01:UG制图,添加基本视图之后的中心线怎么去掉? A01:“菜单-->文件-->首选项-->制图-->视图-->公共-->常规-->□带中心线创建”,取消 ...
- ajax中get和post区别
参考地址:http://blog.csdn.net/laijieyao/article/details/40426257 首先要明确的事$.get方法是使用GET方式进行异步请求.$.post方法使用 ...
- odoo8 报表页面修改和字体设置
版本8.0, 想要发票修改报表页眉的内容,去公司设置下修改,返现无论如何也不生效. 放狗后得知: You probably already know that you can customise th ...
- mysql join操作
join的类型 1. 内联结:将两个表中存在联结关系的字段符合联结关系的那些记录形成记录集的联结. 2. 外联结:分为外左联结和外右联结. 案例背景 create table java (name ...
- Vue 简单实用---代码可以直接用
<!DOCTYPE html> <html> <head> <title></title> <script src="htt ...
- CSS子元素设置margin-top作用于父容器?
CSS子元素设置margin-top作用于父容器? 原因: In this specification, the expression collapsing margins means that ad ...
- 爬虫的两种解析方式 xpath和bs4
1.xpath解析 from lxml import etree 两种方式使用:将html文档变成一个对象,然后调用对象的方法去查找指定的节点 (1)本地文件 tree = etree.parse(文 ...
- npm install -g cnpm --registry=https://registry.npm.taobao.org
npm install -g cnpm --registry=https://registry.npm.taobao.org