palindromic sequence verification leetcode-125-

problem:

 

Package com.example.demo; 

public  class Test125 { 

    / ** 
     * Verify palindromic string 
     * Analysis: 
     * two indexes defined, respectively, execution start and end of the string, while moving toward the middle two index 
     * in when the comparison, if they are non-numeric characters and letters, in right-- or left ++ (skip the current character) 
     * / 
    public  Boolean isPalindrome (String S) {
         IF (S == null ) {
             return  to false ; 
        } 
        IF ( s.length () == 0 ) {
             return  to true ; 
        } 

        // ignore case 
        S = s.toLowerCase (); 

        int left = 0 ;
        int right = s.length() - 1;
        while (left < right) {
            // 判断是否是数字或字母
            while (left < right && !Character.isLetterOrDigit(s.charAt(left))) {
                left++;
            }
            while (left < right && !Character.isLetterOrDigit(s.charAt(right))) {
                right--;
            }
            if (s.charAt(left) != s.charAt(right)) {
                return false;
            }
            left++;
            right--;
        }
        return true;
    }

    public static void main(String[] args) {
        Test125 t = new Test125();
        boolean asdffdsa = t.isPalindrome("A man, a plan, a canal: Panama");
        System.out.println(asdffdsa);
    }
}

 

Guess you like

Origin www.cnblogs.com/nxzblogs/p/11274950.html