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 public boolean isPalindrome(String s) {
     2         // Start typing your Java solution below
     3         // DO NOT write main() function
     4         int len = s.length();
     5         if(len == 0)
     6             return true;
     7             
     8         String ignoredStr = ignore(s.toLowerCase());
     9         
    10         boolean result = checkPali(ignoredStr);
    11         
    12         return result;
    13     }
    14     
    15     public static String ignore(String s){
    16         StringBuilder sb = new StringBuilder();
    17         int len = s.length();
    18         for(int i = 0; i < len; i++){
    19             char c = s.charAt(i);
    20             if((c >= '0' && c <= '9') ||
    21                 (c >= 'a' && c <= 'z')){
    22                     sb.append(c);
    23                 }
    24         }
    25         return sb.toString();
    26     }
    27     
    28     public static boolean checkPali(String s){
    29         int len = s.length();
    30         boolean flag = true;
    31         for(int i = 0; i < (len / 2); i++){
    32             if(s.charAt(i) != s.charAt(len - 1 - i)){
    33                 flag = false;
    34                 return flag;
    35             }
    36         }
    37         return flag;
    38     }
  • 相关阅读:
    Android中得到布局文件对象有三种方式
    android中的键值对
    .length()与.length与.size()
    异常处理
    Python操作Excel
    写一个简单的爬虫(博客)
    开发一个登录接口(Mysql)
    常用模块
    内置函数
    装饰器
  • 原文地址:https://www.cnblogs.com/feiling/p/3244828.html
Copyright © 2011-2022 走看看