[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 ...
随机推荐
- 编码转换(UTF8->GBK)
WCHAR woutstr[]; ]; , value, -, NULL, ); MultiByteToWideChar(CP_UTF8, , value, -, woutstr, len); len ...
- oracle的同义词总结
oracle的同义词总结 从字面上理解就是别名的意思,和视图的功能类似.就是一种映射关系. 同义词拥有如下好处: 节省大量的数据库空间,对不同用户的操作同一张表没有多少差别; 扩展的 ...
- Python if判断语句
a=input('输入你的用户名:') if a == "lilei": print('李磊,等你好久了') elif a == "wanghui": prin ...
- 「小程序JAVA实战」小程序和后台api通信(28)
转自:https://idig8.com/2018/08/19/xiaochengxujavashizhanxiaochengxuhehoutaiapitongxin28/ 开发最重要的就是实操! 小 ...
- 禁用Java JDK的自动更新
- 基于 DirectX11 的 MMDViewer 04-渲染目标视图和多视口
上篇文章给出了一个简单并且可以运行的渲染框架,接下来将介绍框架中的渲染管线构成. 1.创建渲染管线 在你创建完一个窗口后,接着便要创建渲染管线,使用的函数是 D3D11CreateDeviceAndS ...
- c#文件下载---以文件流形式
/// <summary> /// 文件下载 /// </summary> /// <param name="FileName">文件名< ...
- NHibernate-NativeSQL
一.调用方式 1.创建查询 var sql = session.CreateSQLQuery("SELECT * FROM sns_User WHERE UserName LIKE :use ...
- bootstrap-select.js 下拉框多选后动态赋值
话不多说先上demo 其实demo是从官网下载的 只稍作改动 由于没有搞清楚怎么上传源代码 就把官网的链接贴出来吧 https://github.com/silviomoreto/bootstrap- ...
- Unity 之 Shader 面的剔除 Cull
面的剔除 Cull 在渲染的时候,默认情况下是只有朝向摄像机的面才会被渲染,可以告诉Unity,我想渲染哪一个朝向的面,使用Cull命令在计算体积阴影的时候会用到对Cull的操作来计算和物体相交的投影 ...