zoukankan      html  css  js  c++  java
  • leecode 回文字符串加强版

    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 public class Solution {
     2     public boolean isPalindrome(String s) {
     3          
     4         int low=0;
     5         int high=s.length()-1;
     6         String s1=s.toLowerCase();
     7         while(low<high)
     8         {
     9           
    10             while(low<high)
    11             {
    12                char c=s1.charAt(low);
    13                if((c>='a'&&c<='z')||(c>='0'&&c<='9'))
    14                {
    15                    break;
    16                }
    17                else  low++;
    18                 
    19             }
    20             
    21              while(low<high)
    22             {
    23                char c=s1.charAt(high);
    24                if((c>='a'&&c<='z')||(c>='0'&&c<='9'))
    25                {
    26                    break;
    27                }else high--;
    28                 
    29             }
    30             
    31             
    32             
    33             if(s1.charAt(low)==s1.charAt(high)) 
    34             {
    35                 low++;
    36                 high--;
    37             }
    38             else
    39             {
    40                 return false;
    41             }
    42            
    43             
    44             
    45         }
    46         
    47         return true;
    48         
    49         
    50     }
    51 }
  • 相关阅读:
    项目进展1
    团队项目(百药食坊)介绍
    结对编程—黄金点游戏(庞思瑶&季远琦)
    WC项目
    四则运算
    Week3——Session
    Spring IOC (DI-依赖注入)
    Week2——XML
    Week2——提交表单后后台的工作
    Week1——JavaEE
  • 原文地址:https://www.cnblogs.com/hansongjiang/p/3821322.html
Copyright © 2011-2022 走看看