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 };
  • 相关阅读:
    Java 9 揭秘(14. HTTP/2 Client API)
    Java 9 揭秘(13. Collection API 更新)
    Java 9 揭秘(12. Process API 更新)
    JAVA数组与List相互转换
    linux下开启oracle服务和开启监听
    Centos7安装Redis
    Hibernate乐观锁无法Catch到org.hibernate.StaleObjectStateException
    Eclipse远程调试Tomcat
    Centos7 使用Docker搭建Oracle测试环境
    Maven安装Oracle驱动包到本地仓库
  • 原文地址:https://www.cnblogs.com/shellfishsplace/p/5926020.html
Copyright © 2011-2022 走看看