415-有效回文串

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

注意事项

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

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

样例

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

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

挑战

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

标签

两根指针 字符串处理 Zenefits 优步 脸书

思路

消除原串中的非字母或数字字符,并将大学字母转换为小写字母,之后便是一般的回文串判断

code

class Solution {
public:
/*
* @param s: A string
* @return: Whether the string is a valid palindrome
*/
bool isPalindrome(string s) {
// write your code here
int size = s.size();
if (size <= 0) {
return true;
}
int cur = 0;
for (int i = 0; i < size; i++) {
if (s[i] <= 'Z' && s[i] >= 'A') {
s[cur] = s[i] - 'A' + 'a';
cur++;;
}
else if (s[i] <= 'z' && s[i] >= 'a' || s[i] <= '9' && s[i] >= '0') {
s[cur] = s[i];
cur++;
}
}
for (int i = 0; i < cur / 2; i++) {
if (s[i] != s[cur - 1 - i]) {
return false;
}
}
return true;
}
};

lintcode-415-有效回文串的更多相关文章

  1. lintcode :Valid Palindrome 有效回文串

    题目: 有效回文串 给定一个字符串,判断其是否为一个回文串.只包含字母和数字,忽略大小写. 样例 "A man, a plan, a canal: Panama" 是一个回文. & ...

  2. lintcode:Palindrome Partitioning 分割回文串

    题目: 分割回文串 给定一个字符串s,将s分割成一些子串,使每个子串都是回文串. 返回s所有可能的回文串分割方案. 样例 给出 s = "aab",返回 [ ["aa&q ...

  3. lintcode最长回文子串(Manacher算法)

    题目来自lintcode, 链接:http://www.lintcode.com/zh-cn/problem/longest-palindromic-substring/ 最长回文子串 给出一个字符串 ...

  4. lintcode-108-分割回文串 II

    108-分割回文串 II 给定一个字符串s,将s分割成一些子串,使每个子串都是回文. 返回s符合要求的的最少分割次数. 样例 比如,给出字符串s = "aab", 返回 1, 因为 ...

  5. HDU 5340——Three Palindromes——————【manacher处理回文串】

    Three Palindromes Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  6. [leetcode/lintcode 题解] 有效回文 II · Valid Palindrome II

    [题目描述] 给一个非空字符串 s,你最多可以删除一个字符.判断是否可以把它变成回文串. 在线评测地址: https://www.lintcode.com/problem/valid-palindro ...

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

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

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

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

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

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

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

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

随机推荐

  1. 一图看懂hadoop分布式文件存储系统HDFS工作原理

    一图看懂hadoop分布式文件存储系统HDFS工作原理

  2. 【JVM】TroubleShooting之内存溢出异常(OOM)与调优

    1. OOM概述 If your application's execution time becomes longer and longer, or if the operating system ...

  3. actor、reactor与proactor模型:高性能服务器的几种模型概念(转)

    actor模型: 实体之通过消息通讯,各自处理自己的数据,能够实现这并行. 说白了,有点像rpc. skynet是actor模型. reactor模型: 1 向事件分发器注册事件回调 2 事件发生 4 ...

  4. java入门---变量类型&类变量&局部变量&实例变量&静态变量

        在Java语言中,所有的变量在使用前必须声明.声明变量的基本格式如下:     type identifier [ = value][, identifier [= value] ...] ; ...

  5. Redis数据结构总结

    Redis 字符串(String) SET runoobkey redis GET runoobkey Redis 哈希(Hash) Redis hash 是一个string类型的field和valu ...

  6. 20145209刘一阳《网络对抗》Exp9 Web安全基础实践

    20145209刘一阳<网络对抗>Exp9 Web安全基础实践 基础问题回答 1.SQL注入攻击原理,如何防御? SQL注入攻击就是通过把SQL命令插入到Web表单递交或输入域名或页面请求 ...

  7. Struts 2(一):初识Struts

    [很久以前的笔记,后续继续完善] 在了解Struts 2框架之前,首先了解一下Model 1和Model 2架构,以及它们的优缺点. 1.1 Model 1架构模式 Model 1的核心是JSP文件, ...

  8. ubuntu apt-xxx

    1. apt-get install xxx 2. dpkg -l ; list software already installed. 3. apt-cache dumpavail ; print ...

  9. static和构造函数初始化顺序

    abstract class demo{ public demo() {} protected void a() { System.out.println("I am parents!&qu ...

  10. InTelliJ 字体调整

    Java IDE 工具InTelliJ 调整字体大小 1.File -> Settings 2.左上的搜索框中输入 font. 等待自动查找结果. 3.修改size 大小