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 ...
随机推荐
- 利用arguments对象在javaScript中实现重载(overload)
一些概念: 重载(overload): 什么是: 相同函数名,不同参数列表的多个函数,在调用时,可根据传入参数的不同,自动选择对应的函数调用! 为什么: 减轻调用者的负担,一个函数名,可执行多种操作 ...
- JavaScript引擎基本原理:Shapes和Inline Caches
原文链接: JavaScript engine fundamentals:Shapes and line Cahes 这篇文章描述了一些在js引擎中通用的关键点, 并不只是V8, 这个引擎的作者(Be ...
- Linux下Java运行.class文件,报错找不到或无法加载主类
classpath配置的错误,所以找不到.class文件. 原先的etc/profile中的classpath配置 export CLASSPATH=$JAVA_HOME/lib/tools.jar ...
- ASP.NET AJAX入门系列(5):使用UpdatePanel控件(二)
UpdatePanel可以用来创建丰富的局部更新Web应用程序,它是ASP.NET 2.0 AJAX Extensions中很重要的一个控件,其强大之处在于不用编写任何客户端脚本,只要在一个页面上添加 ...
- asp.net网页跳转
1.Response.Redirect("http://www.bcbbs.net",false); 目标页面和原页面可以在2个服务器上,可输入网址或相对路径.后面的bool ...
- MQTT进阶篇
我们介绍了最流行的物联网协议MQTT的背景以及基本使用方法.在这篇文章中,我们会继续考察MQTT的高级玩法——与网页应用的交互.MQTT是基于TCP协议实现,基于HTTP的网页应用便无 ...
- CF1081C Colorful Bricks
思路: dp[i][j]表示到第i个砖块为止共计有j个砖块和它左边的砖块颜色不同. 实现: #include <bits/stdc++.h> using namespace std; ty ...
- 盒子模型--IE与标准
从上图可以看到标准 W3C 盒子模型的范围包括 margin.border.padding.content,并且 content 部分不包含其他部分. 从上图可以看到 IE 盒子模型的范围也包括 ma ...
- ftpclient 遇到的一些问题
1. FTPFile[] files=ftpClient.listFiles(ftpDirectory); 没有数据 public static boolean ftpLogin(String ser ...
- 最新深度ghost win7系统下载
深度技术ghost win7系统 64位快速安装版 V2016年2月,深度技术ghost win7 64位快速安装版在不影响大多数软件和硬件运行的前提下,已经尽可能关闭非必要服务,自动安装AMD/In ...