给定一个字符串,判断其是否为一个回文串。只包含字母和数字,忽略大小写。

注意事项

你是否考虑过,字符串有可能是空字符串?这是面试过程中,面试官常常会问的问题。

在这个题目中,我们将空字符串判定为有效回文。

样例

"A man, a plan, a canal: Panama" 是一个回文。

"race a car" 不是一个回文。

挑战

O(n) 时间复杂度,且不占用额外空间。

  bool isPalindrome(string& s) {
// Write your code here
int len,j;
for(len = , j = ; j < s.size(); ++j)
{
if(s[j] >= '' && s[j] <= '')
{
s[len++] = s[j];
}
else if(((s[j] >= 'a') && (s[j] <= 'z')) || ((s[j] >= 'A') && (s[j] <= 'Z')))
{
s[len++] = tolower(s[j]);
}
}
int i = ;
j = len - ;
while(i < j)
{
if(s[i] == s[j])
{
i++;
j--;
}
else return false;
}
return true;
}

LintCode_415 有效回文串的更多相关文章

  1. [LeetCode] Longest Palindrome 最长回文串

    Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...

  2. [LeetCode] Shortest Palindrome 最短回文串

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  3. [LeetCode] Palindrome Partitioning II 拆分回文串之二

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  4. [LeetCode] Palindrome Partitioning 拆分回文串

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  5. [LeetCode] Longest Palindromic Substring 最长回文串

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  6. bzoj 3676 回文串 manachar+hash

    考虑每个回文串,它一定是它中心字母的最长回文串两侧去掉同样数量的字符后的一个子串. 所以我们可以用manachar求出每一位的回文半径,放到哈希表里并标记出它的下一个子串. 最后拓扑排序递推就行了.. ...

  7. BZOJ 3676: [Apio2014]回文串

    3676: [Apio2014]回文串 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 2013  Solved: 863[Submit][Status ...

  8. SPOJ - PLSQUARE Palin Squar(hash+回文串)

    题意:给你一个n*n (n<=200)的字符串矩阵,问你每行每列都是回文串的最大的m*m的矩阵是多少 题解:首先答案不满足单调性,即m成立而m-1与m+1都却不一定成立,所以必须枚举答案确定现在 ...

  9. 删除部分字符使其变成回文串问题——最长公共子序列(LCS)问题

    先要搞明白:最长公共子串和最长公共子序列的区别.    最长公共子串(Longest Common Substirng):连续 最长公共子序列(Longest Common Subsequence,L ...

随机推荐

  1. uoj60 怎样提高智商

    题意:你需要构造n个四项选择题.格式为:问在前i个问题中选了几个hi字母? 输出有最多正确答案的构造方案. 标程: #include<cstdio> using namespace std ...

  2. 布局页中的特殊情况(比如说只有某页有的banner)

    仅作代码记录之用 /WEB-INF/tags/section.tag <%@ tag language="java" import="java.util.*,jav ...

  3. 【转载】linux进程及进程控制

    Linux进程控制   程序是一组可执行的静态指令集,而进程(process)是一个执行中的程序实例.利用分时技术,在Linux操作系统上同时可以运行多个进程.分时技术的基本原理是把CPU的运行时间划 ...

  4. System.Web.Mvc.HttpStatusCodeResult.cs

    ylbtech-System.Web.Mvc.HttpStatusCodeResult.cs 1.程序集 System.Web.Mvc, Version=5.2.3.0, Culture=neutra ...

  5. Git 之 版本回退

    1. git log 定义:该命令显示从最近到最远的提交日志.每一次提交都有对应的 commit id 和 commit message.    

  6. MyBatis - 输入和输出参数

    基础知识 mybatis规定mapp.xml中每一个SQL语句形式上只能有一个@parameterType和一个@resultType 1. 返回值是一个对象的集合,@resultType中只能写该对 ...

  7. 关于不同数据库的连接配置(MySql和Oracle)

    mysql: driverClass=com.mysql.jdbc.Driver #在和mysql传递数据的过程中,使用unicode编码格式,并且字符集设置为utf-8,插入数据库防止中文乱码 ur ...

  8. Python学习day06-Python基础(4)流程控制之while和for循环

    Python学习day06-流程控制之while和for循环 Python学习day06-流程控制之while和for循环while循环1. 语法2. while+break,while+contin ...

  9. ubuntu关闭防火墙(默认命令)

  10. JPA、Hibernate、Spring data jpa之间的关系

    什么么是JPA? 全称Java Persistence API,可以通过注解或者XML描述[对象-关系表]之间的映射关系,并将实体对象持久化到数据库中. 为我们提供了: 1)ORM映射元数据:JPA支 ...