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 }
  • 相关阅读:
    ui、li模拟下拉框
    六项精进
    Echarts柱状图添加点击事件
    [UWP]爱恋动漫BT开发小记
    [杂谈]这个四月
    [uwp]自定义图形裁切控件
    [uwp]自定义Behavior之随意拖动
    [uwp]数据绑定再学习
    [mvc]记一次“项目”的历程
    [uwp]ImageSource和byte[]相互转换
  • 原文地址:https://www.cnblogs.com/hansongjiang/p/3821322.html
Copyright © 2011-2022 走看看