[leetcode] 1. Valid Palindrome
leetcode的第一题,回文数判断。
原题如下:
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.
回文数是个很基础的东西,这里给了两个example作为提示:给的样本中可能会有空格,标点和特殊符号等,要考虑一下先判断string是否是纯string再去进行回文判断。
leetcode给的答案是伪代码,也解释的很清楚:
public boolean isPalindrome(String s)
{
int i = , j = s.length() - ; while (i < j)
{ while (i < j && !Character.isLetterOrDigit(s.charAt(i))) i++;
while (i < j && !Character.isLetterOrDigit(s.charAt(j))) j--; if (Character.toLowerCase(s.charAt(i))
!= Character.toLowerCase(s.charAt(j)))
{
return false;
} i++; j--; } return true; }
思路结束,具体在操作的时候就是C++的能力本身了。随手写的代码(日后改)[2014-11-09]:
#include <string>
#include <algorithm> bool isPalindrome(string s)
{
if (s.empty())
{
return false;
}
string tmp = "";
for (int i = ; i < s.size(); i++)
{
if (isalnum(s.at(i)))
{
tmp += s[i];
}
}
transform(tmp.begin(), tmp.end(), tmp.begin(), ::toupper);
for (int i = ; i < tmp.size() / ; i++)
{
if (tmp.at(i) != tmp.at(tmp.size() - - i))
{
return false;
}
} return true;
}
C++水平还不够,但是过了。回文的问题以后再补充。
[leetcode] 1. Valid Palindrome的更多相关文章
- [LeetCode] 680. Valid Palindrome II 验证回文字符串 II
Given a non-empty string s, you may delete at most one character. Judge whether you can make it a pa ...
- [Leetcode][JAVA] Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- LeetCode 1216. Valid Palindrome III
原题链接在这里:https://leetcode.com/problems/valid-palindrome-iii/ 题目: Given a string s and an integer k, f ...
- [LeetCode] 125. Valid Palindrome 验证回文字符串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- 【leetcode】Valid Palindrome
题目简述: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ...
- 【题解】【字符串】【Leetcode】Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- leetcode 125. Valid Palindrome ----- java
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- LeetCode 125. Valid Palindrome
这个题目只要注意大小写问题即可解决,而且我们只关注的是字母和数值 Given a string, determine if it is a palindrome, considering only a ...
- leetcode:Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
随机推荐
- Python 小知识点(6)--静态方法、类方法、属性方法
(1)静态方法-->-@staticmethod装饰类中方法 只是名义上归类管理, 实际上在静态方法里访问不了类或实例中的任何属性 class Dog(object): def __init__ ...
- Weak References
http://docwiki.embarcadero.com/RADStudio/Seattle/en/Automatic_Reference_Counting_in_Delphi_Mobile_Co ...
- iOS布局之Auto Layout
学习资源: <iOS6核心编程>自动布局部分 <iOS6范例经典>自动布局部分 Tutorial: iOS 6 Auto Layout versus Springs and S ...
- java算法 第七届 蓝桥杯B组(题+答案) 6.方格填数
6.方格填数 (结果填空) 如下的10个格子 (如果显示有问题,也可以参看[图1.jpg]) 填入0~9的数字.要求:连续的两个数字不能相邻.(左右.上下.对角都算相邻) 一共有多少种可能的填数方案 ...
- UV-Sprite动画
[UV-Sprite动画] 下文以单行Sprite纹理作为动画贴图.首先需要输入纹理宽度.Sprint数量.速度: 计算每个Sprite的像素宽与UV宽: 根据_Time,计算当前显示第几个Sprit ...
- 必看的 jQuery性能优化的38个建议
一.注意定义jQuery变量的时候添加var关键字 这个不仅仅是jQuery,所有javascript开发过程中,都需要注意,请一定不要定义成如下: $loading = $('#loading'); ...
- 解剖Nginx·自动脚本篇(6)编译器名称变量脚本 auto/cc/name
回顾变量 CC 最初是在auto/options脚本中初始化的: CC=${CC:-gcc} 1 C Compiler 的 feature Windows 平台的编译器叫做MSVC,其他平台的都统称为 ...
- linux系统文件的安全与特殊权限
一. 文件与目录的原始属性 由于不希望文件具有可执行的原始权限,默认情况下,文件是没有可执行(x)权限的,因此文件的原始属性是:-rw-rw-rw(0666) 目录的原始属性全部开放,为:-rwxrw ...
- 监控web接口和添加触发器
1: 以监控百度接口为例子: www.baidu.com1: 打开 2: 过滤ss0.bdstatic.com 3: 选择两个url进行监控 https://ss0.bdstatic.com/5aV1 ...
- Mysql Join语法以及性能优化
引言 内外联结的区别是内联结将去除所有不符合条件的记录,而外联结则保留其中部分.外左联结与外右联结的区别在于如果用A左联结B则A中所有记录都会保留在结果中,此时B中只有符合联结条件的记录,而右联结相反 ...