leetcode 125
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.
判断字符串是否为回文字符串,只需考虑字符串中的数字和字母。
思路:字符串首尾各一个指针,进行比较;
指针移动规则:若相等则首指针向前移动一位,尾指针向后移动以为;若出现非法字符则首尾指针相应的移动一位。
代码如下:
class Solution {
public:
bool isPalindrome(string s) {
int n = s.size();
int st = ;
if(n == )
{
return true;
}
n--;
while(st < n)
{
if(isValid(s[st]) && isValid(s[n]))
{
if(s[st] <= && s[st] >= )
{
if(s[n] > || s[n] < )
{
return false;
}
else if(s[st] != s[n])
{
return false;
}
else
{
st++;
n--;
continue;
}
}
if(s[st] == s[n] || s[st]-s[n] == || s[n]-s[st] == )
{
st++;
n--;
continue;
}
else
{
return false;
}
}
else if(isValid(s[st]))
{
n--;
}
else if(isValid(s[n]))
{
st++;
}
else
{
n--;
st++;
}
}
return true;
}
bool isValid(char s)
{
if(s < || s > )
{
return false;
}
else if(s > && s < )
{
return false;
}
else if(s > && s < )
{
return false;
}
return true;
}
};
leetcode 125的更多相关文章
- 前端与算法 leetcode 125. 验证回文串
目录 # 前端与算法 leetcode 125. 验证回文串 题目描述 概要 提示 解析 解法一:api侠 解法二:双指针 算法 传入测试用例的运行结果 执行结果 GitHub仓库 查看更多 # 前端 ...
- Leetcode 125 Valid Palindrome 字符串处理
题意:判断字符串是否是回文字符串 先将所有的字母和数字字符保留,并将大写字母转化成小写字母,然后将字符串倒置,比较前后两个字符串是否相同. 该题最好的解法可以模仿 Leetcode 345 Rever ...
- LeetCode(125)题解--Valid Palindrome
https://leetcode.com/problems/valid-palindrome/ 题目: Given a string, determine if it is a palindrome, ...
- LeetCode 125. Valid Palindorme (验证回文字符串)
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- [LeetCode] 125. Valid Palindrome 验证回文字符串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- Java实现 LeetCode 125 验证回文串
125. 验证回文串 给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写. 说明:本题中,我们将空字符串定义为有效的回文串. 示例 1: 输入: "A man, ...
- Valid Palindrome ---- LeetCode 125
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- leetcode 125. Valid Palindrome ----- java
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- LeetCode 125. Valid Palindrome
这个题目只要注意大小写问题即可解决,而且我们只关注的是字母和数值 Given a string, determine if it is a palindrome, considering only a ...
随机推荐
- Javascript中封装window.open的例子
对window.open进行封装, 使其更好用, 且更兼容, 很多人说window.open不兼容,其实不是, 因为不能直接执行, 必须通过用户手动触发才行;看代码: 代码如下 复制代码 var op ...
- cassandra CQL 常用操作
1. CQL客户端链接 bin/cqlsh ip username password 2. (1)建立keyspace语句,keyspace类似于 mysql 中的数据库,一个数据库中可以有很多表: ...
- RedHat5配置网卡
RedHat5配置网卡过程: 1.vi /etc/sysconfig/network-scripts/ifcfg-eth0 2.将hdcp修改成static 3.最后添加 IPADDR=192.168 ...
- 浅谈C#中的接口和抽象类
C#中接口为"其他方面互不相干"的类型提供公共的服务和特征:C#中class只支持但继承,使用接口却支持多继承,例如:C#中System.String是从System空间中的4个i ...
- PHP-query 的用法
Jquery Jquery实际上相当于一个升级版的JS,Jquery里面封装了很多的东西,Jquery的功能要比JS强大,用起来比JS方便.Jquery和JS都属于JS,只不过Jquery是封装了一个 ...
- 5X + 2Y +Z = 50 的所有非负整数解
这种题的解题方法都差不多,不停的循环,不过如果做一下细分,效率应该可以提升很多,下面把最常规效率也最低的代码贴上,有时间再优化 #include <iostream> using name ...
- 如何取Android设备日志
安装Android SDK 运行 adb 命令 adb devices 查看链接的设备 adb logcat 日志相关
- OpenGL概述
简介 状态机 glBegin()与glEnd() glFlush()与glFinish() OpenGL简介 OpenGL是图形硬件的一种软件接口.它被设计为硬件独立的接口,可用于多种不同硬件平台.O ...
- spring boot Swagger 集成
1. pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://ww ...
- 【java】spring-data-jpa 集成hibernate实现多条件分页查询
初次接触spring-data-jpa,实现多条件分页查询. 基础环境 Spring Boot+spring-data-jpa+hibernate+mysql 1.接口 要继承这个接口,这个接口提供了 ...