lintcode-415-有效回文串
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-有效回文串的更多相关文章
- lintcode :Valid Palindrome 有效回文串
题目: 有效回文串 给定一个字符串,判断其是否为一个回文串.只包含字母和数字,忽略大小写. 样例 "A man, a plan, a canal: Panama" 是一个回文. & ...
- lintcode:Palindrome Partitioning 分割回文串
题目: 分割回文串 给定一个字符串s,将s分割成一些子串,使每个子串都是回文串. 返回s所有可能的回文串分割方案. 样例 给出 s = "aab",返回 [ ["aa&q ...
- lintcode最长回文子串(Manacher算法)
题目来自lintcode, 链接:http://www.lintcode.com/zh-cn/problem/longest-palindromic-substring/ 最长回文子串 给出一个字符串 ...
- lintcode-108-分割回文串 II
108-分割回文串 II 给定一个字符串s,将s分割成一些子串,使每个子串都是回文. 返回s符合要求的的最少分割次数. 样例 比如,给出字符串s = "aab", 返回 1, 因为 ...
- HDU 5340——Three Palindromes——————【manacher处理回文串】
Three Palindromes Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- [leetcode/lintcode 题解] 有效回文 II · Valid Palindrome II
[题目描述] 给一个非空字符串 s,你最多可以删除一个字符.判断是否可以把它变成回文串. 在线评测地址: https://www.lintcode.com/problem/valid-palindro ...
- [LeetCode] Longest Palindrome 最长回文串
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- [LeetCode] Shortest Palindrome 最短回文串
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- [LeetCode] Palindrome Partitioning II 拆分回文串之二
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- [LeetCode] Palindrome Partitioning 拆分回文串
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
随机推荐
- php wamp基础环境搭建
一.apache 安装配置: 1.安装apache 1.1 下载地址:https://www.apachelounge.com/download/ 1.2 将下载的文件解压到你想安装的目录 D:\WA ...
- Windows.Devices API in a C# WinForm Win32 Desktop application in Windows 10
https://social.msdn.microsoft.com/Forums/windows/en-US/40b4f86d-b6d5-430e-a235-7fc003dec4c4/how-to-u ...
- 一图看懂hadoop Spark On Yarn工作原理
hadoop Spark On Yarn工作原理
- 【树形DP】MZOJ_1063_士兵守卫
本题也是这三天来在下写的几篇树形DP之一,但是不知道为什么洛谷上面老是unknown error,...直接去了UVa,说我编译错误...我在想是不是头文件的原因,于是被逼无奈,交了一道c89的代码. ...
- ESP32 学习笔记 - Ubuntu安装
最近买了一个ESP32的开发板,以前只玩过ESP8266,没经验只能自己一步步摸索 现在把学习的过程记录下来,以免过了一段时间就忘记了 第一步安装 VMware 可以在我的网盘下载 链接:https: ...
- [Golang学习笔记] 05 程序实体2 作用域访问权限和变量重声明
作用域访问权限: 程序实体访问权限(作用域)有三种:1. 包级私有(代码包)2. 模块级私有(代码包)3. 公开(全域). 一个函数是一个代码块.一个程序实体的作用域总是会被限制在某个代码块中.好处: ...
- python学习之路_字符编码
字符编码及python中的转码问题,这篇博客讲的比较清楚,python 之路,致那些年,我们依然没搞明白的编码
- 记账本APP(2)
今天下载了Hbuiler,生成了一个记账本APP,目前里面只可以 输入今日消费 明天将会做出来记录以及计算总额于月消费.
- postgres-xl 安装与部署 【异常处理】ERROR: could not open file (null)/STDIN_***_0 for write, No such file or directory
https://www.jianshu.com/p/82aaf352b772 这篇文章很不错,里面有个bug,可能是版本不对. 当前(2018-04-11)通过git 下载原代码时,在配置 pgxc ...
- python--基本类型之元组
tuple(元组): 定义和创建和作用: 元组--只读列表,只可以被查询,不能被修改.所以,列表的切片操作同样适用与元组. 元组写在小括号(())里,元素之间用逗号隔开. 虽然元组的元素不可改变,但他 ...