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. MongoDB DBA 实践7-----MongoDB的分片集群操

    一.使用Ranged Sharding对集合进行分片 从mongo连接到的shell中mongos,使用该sh.shardCollection()方法对集合进行分片. 注意: 必须已为集合所在的数据库 ...

  2. redis学习笔记(三)

    Spring data redis: 要求: Redis 版本 > 2.6 与 Lettuce 或 Jedis 集成,两种java开源Redis库. Spring redis主要做的两件事: 连 ...

  3. 记一次学习PHP中的错误

    今天学习PHP时,回想起一段代码 <?php> $i = true; $o = true; $p = false; if($i or $o and $p){ echo '输出为'.'tru ...

  4. 工作和面试中的gdb

    gdb是C/C++程序员必备的专业技能,工作中gdb最常用的场景有两个,一个是分析core文件,另一个是调试程序. 分析core文件的方法如下: 1.gdb 程序名 core文件名 2.bt或wher ...

  5. 证明SG中梯度的期望等于GD的梯度

    参考链接: https://zhuanlan.zhihu.com/p/36435504

  6. C# 访问修饰符和const、readonly

    今天被人问起const和readonly,竟然有点咬不准,复习一遍. 访问修饰符 public 公有访问.不受任何限制. private 私有访问.只限于本类成员访问,子类,实例都不能访问. prot ...

  7. echarts 去掉上面的小图标

    在option里找到toolbox,删除对应的代码即可: toolbox: { y : -30, show : true, feature : { mark : '辅助线开关', markUndo : ...

  8. flume 安装过程记录

    1.安装jdk 2.下载安装包 : apache-flume-1.7.0-bin.tar.gz 安装包是在win下载的,需要拖动到ubuntu下的/home/hadoop (拖动不了需要先安装  lr ...

  9. c++编译器处理 函数返回值

    X bar() { X xx; return xx; } // compiler generated temporary X __temp0; ( bar( __temp0 ), __temp0 ). ...

  10. nmap保存结果

    nmap 192.168.0.2 -oX D:\myscan.xml 参数解释: -oN <filespec> (标准输出) -oX <filespec> (XML输出) -o ...