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的更多相关文章

  1. 前端与算法 leetcode 125. 验证回文串

    目录 # 前端与算法 leetcode 125. 验证回文串 题目描述 概要 提示 解析 解法一:api侠 解法二:双指针 算法 传入测试用例的运行结果 执行结果 GitHub仓库 查看更多 # 前端 ...

  2. Leetcode 125 Valid Palindrome 字符串处理

    题意:判断字符串是否是回文字符串 先将所有的字母和数字字符保留,并将大写字母转化成小写字母,然后将字符串倒置,比较前后两个字符串是否相同. 该题最好的解法可以模仿 Leetcode 345 Rever ...

  3. LeetCode(125)题解--Valid Palindrome

    https://leetcode.com/problems/valid-palindrome/ 题目: Given a string, determine if it is a palindrome, ...

  4. LeetCode 125. Valid Palindorme (验证回文字符串)

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  5. [LeetCode] 125. Valid Palindrome 验证回文字符串

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  6. Java实现 LeetCode 125 验证回文串

    125. 验证回文串 给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写. 说明:本题中,我们将空字符串定义为有效的回文串. 示例 1: 输入: "A man, ...

  7. Valid Palindrome ---- LeetCode 125

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  8. leetcode 125. Valid Palindrome ----- java

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  9. LeetCode 125. Valid Palindrome

    这个题目只要注意大小写问题即可解决,而且我们只关注的是字母和数值 Given a string, determine if it is a palindrome, considering only a ...

随机推荐

  1. tinymce 编辑器 上传图片

    tinymce编辑器进行本地图片上传 首先下载tinymce.js之后 在form中添加一个<textarea>元素 给其一个id和name 然后就可以初始化编辑器了 tinymce.in ...

  2. Java String字符串补0或空格

    package cn.com.songjy; import java.text.NumberFormat; //Java 中给数字左边补0 public class NumberFormatTest  ...

  3. Day6 google Geocoding API

    在看机器学习实战中K-means一章,练习中需要调用Yahoo PlaceFinder API 为地点添加经纬度,语言是python.申请到了appid但调用好像还要收费,要填写银行卡号才能用,没管那 ...

  4. E1_1 用邻接矩阵存储有向图,并输出各顶点的出度和入度

    参考书:图论算法理论.实现及应用(北京大学出版社) 输入数据:(test.txt) 程序: /* 邻接矩阵存储有向图 */ #include <cstring> #include < ...

  5. sqlserver08评估期已过的解决方法

    打开sqlserver出现提示:评估期已过.有关如何升级的测试版软件的信息,请访问http://www.microsoft.com/sql/howtobuy 解决方法如下: 第一步:进入开始菜单--- ...

  6. 为sproto添加python绑定

    项目地址:https://github.com/spin6lock/python-sproto 第一次写Python的C扩展,留点笔记记录一下.主要的参考文档是:Extending Python wi ...

  7. 修改mysql用户名密码 和 PHPmysqlAdmin对应密码修改

    本地的mysql运行时,可能会用到修改用户名密码: mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('输入新密码');不存在修改用户啊 ...

  8. viewpager接受值图片轮播

    package com.baway.test; import java.util.ArrayList;import java.util.List;import java.util.Timer;impo ...

  9. [Codeforces Round #275 (Div. 2)]B - Friends and Presents

    最近一直在做 codeforces ,总觉得已经刷不动 BZOJ 了? ——真是弱喵 你看连 Div.2 的 B 题都要谢谢题解,不是闲就是傻 显然我没那么闲 ╮(╯_╰)╭ 我觉得这题的想法挺妙的~ ...

  10. jQuery radio change事件 checkbox选中事件

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...