zoukankan      html  css  js  c++  java
  • LeetCode之“字符串”:Valid Palindrome

      题目链接

      题目要求:

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

      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.

      程序如下:

     1 class Solution {
     2 public:
     3     bool isPalindrome(string s) {
     4         if(!s.empty() && s.front() == ' ')
     5             s.erase(0, 1);
     6         if(!s.empty() && s.back() == ' ')
     7             s.pop_back();
     8         
     9         bool ret = true;
    10         int sz = s.size();
    11         if(sz == 0)
    12             return true;
    13         
    14         int i = 0;
    15         int j = sz - 1;
    16         while(i < j)
    17         {
    18             while(i < sz && !isalnum(s[i]))
    19                 i++;
    20             while(j > -1 && !isalnum(s[j]))
    21                 j--;
    22             if(i < sz && j > -1 && s[i] != s[j])
    23             {
    24                 if(tolower(s[i]) != tolower(s[j]))
    25                 {
    26                     ret = false;
    27                     break;
    28                 }
    29             }
    30             i++;
    31             j--;
    32         }
    33         
    34         return ret;
    35     }
    36 };
  • 相关阅读:
    java线程小结1
    String和StringBuffer
    java队列的实现
    java栈的实现
    java链表
    this与super关键字总结
    JVM内存杂记1
    面试题18:删除链表节点
    面试题17:打印从 1 到最大的 n 位数
    面试题16:数值的整数次方
  • 原文地址:https://www.cnblogs.com/xiehongfeng100/p/4575150.html
Copyright © 2011-2022 走看看