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的更多相关文章

  1. [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 ...

  2. [Leetcode][JAVA] Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  3. LeetCode 1216. Valid Palindrome III

    原题链接在这里:https://leetcode.com/problems/valid-palindrome-iii/ 题目: Given a string s and an integer k, f ...

  4. [LeetCode] 125. Valid Palindrome 验证回文字符串

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  5. 【leetcode】Valid Palindrome

    题目简述: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ...

  6. 【题解】【字符串】【Leetcode】Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  7. leetcode 125. Valid Palindrome ----- java

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  8. LeetCode 125. Valid Palindrome

    这个题目只要注意大小写问题即可解决,而且我们只关注的是字母和数值 Given a string, determine if it is a palindrome, considering only a ...

  9. leetcode:Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

随机推荐

  1. SpringBoot核心

    1.基本配置 1.1入口类和@SrpingBootApplication SpringBoot通常有一个名为*Application的入口类,入口类里有一个main方法,这个main方法就是一个标准的 ...

  2. leetcode830

    public class Solution { public IList<IList<int>> LargeGroupPositions(string S) { //" ...

  3. short i=1;short i=i+1对或错,错的理由;short i+=1对或错,错的理由

    short i=1; i=i+1 i+=1 这是一个类型高级与低级的问题,前面的C是short型的,后面的1为int型的,short型与int型的相加得到short型是不可取,因为必须将int型转换为 ...

  4. java.lang.IllegalStateException: getOutputStream() has already been called for this response解决方案

    异常产生原因:web容器生成的servlet代码中有out.write(""),这个和JSP中调用的response.getOutputStream()产生冲突.即Servlet规 ...

  5. WPF Shader 正片叠底

    新建文本文档拷贝以下代码 //正片叠底sampler2D implicitInput : register(s0);sampler2D oldInput : register(s1);float4 m ...

  6. 如何显示当前Mipmap级别?

    [如何显示当前Mipmap级别?] 乘以 mainTextureSize/mipTextureSize是为了让mipColorsTexture纹理与mainTexture级别对应.直接用uv是不行的, ...

  7. 【codevs3160】 LCS 【后缀自动机】

    题意 给出两个字符串,求它们的最长公共子串. 分析 后缀自动机的基础应用. 比如两个字符串s1和s2,我们把s1建为SAM,然后根据s2跑,找出s2每个前缀的最长公共后缀. 我们可以理解为,当向尾部增 ...

  8. 刷题向》一道逆向思维题(BZOJ1046)(NORMAL)

    这道题对于一类题都有一个通用思路:反向递减序列即为正向字典序. 对于逆向思维的题还要多做才能培养这种对于逆向思维的感觉. 想到这种方法之后,就很简单了. 因为n×m不会炸,所以反向LIS叠一个贪心就能 ...

  9. mysql的GTID复制和多源复制

    配置基于GTID的复制--------------------------------------------在参数文件/etc/my.cnf增加下面内容:主库master_info_reposito ...

  10. loadrunner录制时web时,ie报安全证书问题

    解决方法:在Recording_Options下Port Mapping>Capture level设置为 WinNet level data Capture Level的设置说明:1.Sock ...