翻译

  1. 给定一个字符串。确定它是否是回文的,仅仅考虑当中的数字和字符并忽略其它。
  2. 比如。
  3. A man, a plan, a canal: Panama 是回文的。
  4. race a car 不是回文的
  5. 批注:
  6. 你是否考虑了字符串可能为空?这样的面试的时候是一个好问题。
  7. 对于这问题的意图,我们定义空字符串是回文的。

原文

  1. Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
  2. For example,
  3. "A man, a plan, a canal: Panama" is a palindrome.
  4. "race a car" is not a palindrome.
  5. Note:
  6. Have you consider that the string might be empty? This is a good question to ask during an interview.
  7. For the purpose of this problem, we define empty string as valid palindrome.

分析

题目解出来并非非常难。只是须要细心只是还是会出错。

第一步就是要从原字符串中的干扰去掉,也就是仅仅保留字母。这能够通过ascii来推断。

然后构造出新的字符串。再推断这个新字符串是否是回文的就好了。

然而我也还是错了。首先我没想清晰题目的样例。”aA”也是回文的。

再一次改动之后我还是错了。由于题目的alphanumeric是字母数字均包括了,我一開始翻译的就是字母,后来上文的翻译也改动了。

于是我确定彻底的改革,将功能独立出来,然而代码好长了。可是非常清晰:

代码

  1. class Solution {
  2. public:
  3. bool isAlpha(char c) {
  4. int ascii = (int)c;
  5. if ((ascii >= 65 && ascii <= 90) || (ascii >= 97 && ascii <= 122))
  6. return true;
  7. else return false;
  8. }
  9. bool isNumber(char c) {
  10. int ascii = (int)c;
  11. if (ascii >= 48 && ascii <= 57)
  12. return true;
  13. else return false;
  14. }
  15. bool isAlphaAndNumber(char c1, char c2) {
  16. if ((isAlpha(c1) && isNumber(c2)) || (isAlpha(c2) && isNumber(c1)))
  17. return true;
  18. else return false;
  19. }
  20. bool isPalindrome(string s) {
  21. if (s.size() == 0) return true;
  22. string newStr = "";
  23. for (int i = 0; i < s.size(); ++i) {
  24. // 仅仅取出当中的字母和数字
  25. if (isAlpha(s[i]) || isNumber(s[i]))
  26. newStr += s[i];
  27. }
  28. for (int i = 0; i < newStr.size() / 2; ++i) {
  29. // 两者一个是字母、一个是数字
  30. if (isAlphaAndNumber(newStr[i], newStr[newStr.size() - i - 1]))
  31. return false;
  32. // 两者均为数字
  33. else if (isNumber(newStr[i]) && isNumber(newStr[newStr.size() - i - 1])) {
  34. // 推断是否是同一个数字
  35. if (newStr[i] != newStr[newStr.size() - i - 1])
  36. return false;
  37. }
  38. // 两者均为字母
  39. else {
  40. // 前面推断是否是同一个字母,后面推断是否是互为大写和小写
  41. if (newStr[i] != newStr[newStr.size() - i - 1] && abs((int)newStr[i] - (int)newStr[newStr.size() - i - 1]) != 32)
  42. return false;
  43. }
  44. }
  45. return true;
  46. }
  47. };

进阶

一想到ascii就忘了还有toupper这些函数了,别人写的……

  1. class Solution {
  2. public:
  3. bool isPalindrome(string s) {
  4. int l = 0, r = s.size() - 1;
  5. while(l <= r){
  6. while(!isalnum(s[l]) && l < r) l++;
  7. while(!isalnum(s[r]) && l < r) r--;
  8. if(toupper(s[l]) != toupper(s[r])) return false;
  9. l++, r--;
  10. }
  11. return true;
  12. }
  13. };

LeetCode 125 Valid Palindrome(有效回文)(*)的更多相关文章

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

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

  2. [leetcode]125. Valid Palindrome判断回文串

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

  3. 125 Valid Palindrome 验证回文字符串

    给定一个字符串,确定它是否是回文,只考虑字母数字字符和忽略大小写.例如:"A man, a plan, a canal: Panama" 是回文字符串."race a c ...

  4. LeetCode 125. Valid Palindorme (验证回文字符串)

    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 ignori ...

  6. LeetCode Valid Palindrome 有效回文(字符串)

    class Solution { public: bool isPalindrome(string s) { if(s=="") return true; ) return tru ...

  7. [Leetcode] valid palindrome 验证回文

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

  8. leetcode 125. Valid Palindrome ----- java

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

  9. lintcode :Valid Palindrome 有效回文串

    题目: 有效回文串 给定一个字符串,判断其是否为一个回文串.只包含字母和数字,忽略大小写. 样例 "A man, a plan, a canal: Panama" 是一个回文. & ...

随机推荐

  1. 手动集成OWIN

    1.Install-Package Microsoft.AspNet.Identity.Owin Owin的很大亮点之一就是它可以让我们的ASP.NET 网站摆脱IIS,但是毕竟大多数的ASP.NET ...

  2. 常用的SQL

    --时间计算: select GETDATE() ,GETDATE()) ,GETDATE()) ,GETDATE()) ,GETDATE()) ,GETDATE()) ,GETDATE()) --查 ...

  3. WiX and System Folders 系统目录 installshield 如何将文件安装到C盘根目录

    Property name Brief description of property AdminToolsFolder Full path to the directory containing a ...

  4. Unity3D动画面板编辑器状态属性对照表

    不推荐用AnimationUtility.SetEditorCurve问题很多,推荐AnimationCurve.AddKey.通过AnimationUtility.GetAllCurves可以获得编 ...

  5. redmine 的安装

    https://bitnami.com/stack/redmine/installer#官方地址 安装很简单,给权限 chmod +x bitnami-redmine-3.3.0-1-linux-x6 ...

  6. 53. Reverse Words in a String【easy】

    Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...

  7. tornado部署

    1.为什么要运行多个tornado实例同步请求时,在应用处理过程中(如数据库查询,磁盘访问),服务器进程不能接受新请求.所以需要运行多个服务器进程实例.异步请求时,在应用处理时,服务器进程是非阻塞的, ...

  8. swift 属性和方法

    属性和常量 如果创建了一个结构体的实例并赋值给一个常量,则无法修改实例的任何属性: let rangeOfFourItems = FixedLengthRange(firstValue: 0, len ...

  9. [转]软件测试 Top 120 Blog (博客)

    [转]软件测试 Top 120 Blog (博客) 2015-06-08 转自:    软件测试 Top 120 Blog (博客) # Site Author Memo DevelopSense M ...

  10. django 文件上传(阿里云oss)下载(支持大文件下载)

    1.文件上传 Models 设计 class Upload_File(models.Model): image = models.FileField(upload_to='file/%Y/%m',de ...