zoukankan      html  css  js  c++  java
  • leetcode 125

    125. 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         int n = s.size(); 
     5         int st = 0; 
     6         if(n == 0)
     7         {
     8             return true;
     9         }
    10         n--;
    11         while(st < n)
    12         {
    13             if(isValid(s[st]) && isValid(s[n]))
    14             {
    15                 if(s[st] <= 57 && s[st] >= 48)
    16                 {
    17                     if(s[n] > 57 || s[n] < 48)
    18                     {
    19                         
    20                         return false;
    21                     }
    22                     else if(s[st] != s[n])
    23                     {
    24                         return false;
    25                     }
    26                     else
    27                     {
    28                         st++;
    29                         n--;
    30                         continue;
    31                     }
    32                 }
    33                 if(s[st] == s[n] || s[st]-s[n] == 32 || s[n]-s[st] == 32)
    34                 {
    35                     st++;
    36                     n--;
    37                     continue;
    38                 }
    39                 else
    40                 {
    41                     return false;
    42                 }
    43             }
    44             else if(isValid(s[st]))
    45             {
    46                 n--;
    47             }
    48             else if(isValid(s[n]))
    49             {
    50                 st++;
    51             }
    52             else
    53             {
    54                 n--;
    55                 st++;
    56             }
    57         }
    58         return true;
    59     }
    60     bool isValid(char s)
    61     {
    62         if(s < 48 || s > 122)
    63         {
    64             return false;
    65         }
    66         else if(s > 57 && s < 65)
    67         {
    68             return false;
    69         }
    70         else if(s > 90 && s < 97)
    71         {
    72             return false;
    73         }
    74         return true;
    75     }
    76 };
  • 相关阅读:
    ‘Host’ is not allowed to connect to this mysql server
    centos7安装mysql
    further configuration avilable 不见了
    Dynamic Web Module 3.0 requires Java 1.6 or newer
    hadoop启动 datanode的live node为0
    ssh远程访问失败 Centos7
    Linux 下的各种环境安装
    Centos7 安装 python2.7
    安装scala
    Centos7 安装 jdk 1.8
  • 原文地址:https://www.cnblogs.com/shellfishsplace/p/5926020.html
Copyright © 2011-2022 走看看