valid-palindrome leetcode C++
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.
C++
class Solution {
bool isAlphaNum(char &ch) {
if (ch >= 'a' && ch <= 'z') return true;
if (ch >= 'A' && ch <= 'Z') return true;
if (ch >= '0' && ch <= '9') return true;
return false;
}
public:
bool isPalindrome(string s) {
int left = 0, right = s.size() - 1 ;
while (left < right) {
if (!isAlphaNum(s[left])) ++left;
else if (!isAlphaNum(s[right])) --right;
else if ((s[left] + 32 - 'a') %32 != (s[right] + 32 - 'a') % 32) return false;
else {
++left; --right;
}
}
return true;
}
bool isPalindrome2(string s){
if("" == s) return true;
int left = 0;
int right = s.size() - 1;
while (left<right){
if(!isAlphaNum(s[left])) left++;
else if (!isAlphaNum(s[right])) right--;
else if ((s[left] + 32 - 'a') %32 != (s[right] + 32 - 'a') % 32) return false;
else { left++; right--;}
}
return true;
}
};
valid-palindrome leetcode C++的更多相关文章
- Valid Palindrome [LeetCode]
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- Valid Palindrome leetcode java
题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ig ...
- Valid Palindrome ---- LeetCode 125
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 ignori ...
- [LeetCode]题解(python):125 Valid Palindrome
题目来源 https://leetcode.com/problems/valid-palindrome/ Given a string, determine if it is a palindrome ...
- [Leetcode][JAVA] Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- 【LeetCode OJ】Valid Palindrome
Problem Link: http://oj.leetcode.com/problems/valid-palindrome/ The following two conditions would s ...
- LeetCode——Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- [LeetCode] Valid Palindrome II 验证回文字符串之二
Given a non-empty string s, you may delete at most one character. Judge whether you can make it a pa ...
- 【一天一道LeetCode】#125. Valid Palindrome
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
随机推荐
- java.net.NoRouteToHostException: 无法指定被请求的地址
今天压力测试时, 刚开始出现了很多异常, 都是 java.net.NoRouteToHostException: Cannot assign requested address. 经网上查资料, 是 ...
- stderr,stdin,stdout相关
转载请保留原作者. 目录 一.stdin和stdout 1.意义 2.缓冲 2.1.scanf的缓冲问题 2.2.fflush 3.freopen 二.stderr 1.输出方法 2.默认缓冲 一.s ...
- Java定时任务Quartz
第一步:pom文件中添加依赖包 第二步:创建xml文件,名称为:spring-scheduler 路径如下图: 第三步:spring-scheduler配置详情 <!--创建任务--> & ...
- linux新安装了php,但是使用mysqli连接数据库一直超时
centos7+mysql5.5+php5.6+nginx mysql php nginx都安装完成,然后启动了,网站也运行, 但是php文件中使用mysqli_connect时一直超时,有时也报错, ...
- JS验证监听输入银行卡号
$("#AccountNum").keydown(function(e) { if(!isNaN(this.value.replace(/[ ]/g,""))) ...
- Jmeter系列(35)- 设置JVM内存
场景 单台机器的下JMeter启动较大线程数时可能会出现运行报错的情况,或者在运行一段时间后,JMeter每秒生成的请求数会逐步下降,直到为0,即JMeter运行变得很"卡",这时 ...
- Xshell和Xftp - 下载安装
简介 Xshell 实际工作运用:连接Linux Xftp 实际工作运用:传输文件到Linux系统 下载安装 三连后评论区留言私发,此贴长期有效!!!
- pandas学习小记
pandas操作整理 导入数据: pd.read_csv(filename):从CSV文件导入数据 pd.read_table(filename):从限定分隔符的文本文件导入数据 pd.read_ex ...
- libcurl 概述(翻译)
l名称 libcurl 客户端 URL 传输 描述 这是一个如何使用libcurl进行C语言编程的简短概述.这里提到的每个功能都有特定的手册页.还有 libcurl-easy 手册页,libcurl- ...
- MySQL、Redis、MongoDB网络抓包工具
简介 go-sniffer 可以抓包截取项目(MySQL.Redis.MongoDB)中的请求并解析成相应的语句,并格式化输出.类似于在之前的文章 MySQL抓包工具:MySQL Sniffer[转] ...