zoukankan      html  css  js  c++  java
  • 回文数

    题目:

    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.
    Example Questions Candidate Might Ask:
    Q: What about an empty string? Is it a valid palindrome?
    A: For the purpose of this problem, we define empty string as valid palindrome.

    解答:

    使用两个游标

    public boolean isPalindrome(String s) {
        int i = 0;
        int j = s.length()-1;
        while(i < j) {
            while(i < j && !Character.isLetterOrDigit(s.charAt(i))) {
                i++;
            }
    
            while(i < j && !Character.isLetterOrDigit(s.charAt(j))) {
                j--;
            }
    
            if(Character.toLowerCase(s.charAt(j) != Character.toLowerCase(s.charAt(i))) {
                return false;
            }
    
            i++;
            j--;
        }
    
        return true;
    }
  • 相关阅读:
    SDUSTOJ 1466
    UVa
    51Nod
    UVa
    easyui-启用禁用方法
    设置系统时间
    移除/添加属性
    .net生成二维码图片
    Mysql数据库误删恢复
    js 加减乘除运算
  • 原文地址:https://www.cnblogs.com/wylwyl/p/10346245.html
Copyright © 2011-2022 走看看