zoukankan      html  css  js  c++  java
  • Valid Palindrome

    package cn.edu.xidian.sselab.string;

    import java.awt.datatransfer.StringSelection;

    /**
     *
     * @author zhiyong wang
     * title: Valid Palindrome
     * content:
     *  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.
     *
     */
    public class ValidPalindrome {

        //这个题很多地方一开始没有想到,调试出了许多错误:
        //1.没有注意到字符串里面有很多不是字母或者数字,如果是不是字母或者数字,要跳过他们,不管他们
        //2.内部两个循环,第一落下了start小于end的约束条件,第二忘记把if改成while循环
        //3.比较的时候忘记了还有大小写的情况,数字、字母都可以调用toLowerCase
        public boolean isPalindrome(String s){
            if(s == null) return false;
            int len = s.length();
            if(s.isEmpty()) return true;
            int start = 0;
            int end = len - 1;
            while(start < end){
                while(start < end && !Character.isLetterOrDigit(s.charAt(start)))
                    start++;
                while(start < end && !Character.isLetterOrDigit(s.charAt(end)))
                    end--;
                if(start < end && Character.toLowerCase(s.charAt(start)) != Character.toLowerCase(s.charAt(end)))
                        return false;
                start++;
                end--;
            }
            return true;
        }
    }

  • 相关阅读:
    面试笔试
    scala(9) Monad
    scala (8) 模糊匹配
    scala (7) Set and Tuple
    scala (6) Map
    scala (5) 可变序列和不可变序列
    scala (4) 可变数组和不可变数组
    scala (3) Function 和 Method
    scala (2) while 和变量
    scala (1) for 循环
  • 原文地址:https://www.cnblogs.com/wzyxidian/p/5217657.html
Copyright © 2011-2022 走看看