LeetCode:Valid Palindrome - 迴文字符串

一、題目名稱java

Valid Palindrome(迴文字符串)數組

二、題目地址ide

https://leetcode.com/problems/valid-palindrome/code

三、題目內容leetcode

英文:Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.開發

中文:給出一個字符串,只考慮字母和數字,判斷該字符串是否爲迴文字符串字符串

例如:get

"A man, a plan, a canal: Panama" 是一個迴文字符串string

"race a car" 不是一個迴文字符串it

四、題目分析

若是要判斷一個字符串是不是迴文字符串,能夠兩個變量,從兩側開始比較有效的字符是否相等。這裏有效的字符是指 'a'-'z'、'A'-'Z'、'0'-'9' 這三個區間內的字符。

五、解題方法1

能夠將字符串中有效的字符,寫入一個鏈表,再將鏈表轉化爲數組,直接判斷數組兩側對稱位置的字符是否匹配。

實現的Java代碼以下:

import java.util.LinkedList;

/**
 * 功能說明:LeetCode 125 - Valid Palindrome
 * 開發人員:Tsybius2014
 * 開發時間:2015年8月4日
 */
public class Solution {
    
    /**
     * 檢測一個字符串是不是迴文(忽略大小寫,只考慮英文和數字)
     * @param s 被檢測字符串
     * @return
     */
    public boolean isPalindrome(String s) {
        
        if (s == null) {
            return false;
        }
        
        if (s.trim().isEmpty()) {
            return true;
        }
        
        //將有效字符選出並存入鏈表
        LinkedList<Character> linkedList = new LinkedList<Character>();
        for (char ch : s.toCharArray()) {
            if ((ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9')) {
                linkedList.add(ch);
            } else if (ch >= 'A' && ch <= 'Z') {
                linkedList.add((char)(ch + 'a' - 'A'));
            }
        }
        if (linkedList.size() <= 1) {
            return true;
        }

        //將鏈表轉換爲數組比較
        Object[] array = linkedList.toArray();
        for (int i = 0; i <= array.length / 2; i++) {
            if (!array[i].equals(array[array.length - 1 - i])) {
                return false;
            }
        }
        
        return true;
    }
}

六、解題方法2

選擇兩個數字i和j,直接從字符串的首和尾開始循環,直到i>=j爲止。若是出現兩側對應字符不匹配的狀況,則直接斷定此字符串不是迴文字符串。所有字符考察完畢後,若是未發現對應位置字符不匹配的狀況,則認爲此字符串是迴文字符串。

實現的Java代碼以下:

import java.util.LinkedList;

/**
 * 功能說明:LeetCode 125 - Valid Palindrome
 * 開發人員:Tsybius2014
 * 開發時間:2015年8月4日
 */
public class Solution {
    
    /**
     * 檢測一個字符串是不是迴文(忽略大小寫,只考慮英文和數字)
     * @param s 被檢測字符串
     * @return
     */
    public boolean isPalindrome(String s) {

        if (s == null) {
            return false;
        }
        
        if (s.trim().isEmpty()) {
            return true;
        }
        
        int i = 0;
        int j = s.length() - 1;
        
        int chLeft; //左側字符
        int chRight; //右側字符
        while (i < j) {
            
            chLeft = s.charAt(i);
            chRight = s.charAt(j);

            //既非英文也非數字的字符直接忽略
            if (!(chLeft >= 'A' && chLeft <= 'Z') &&
                !(chLeft >= 'a' && chLeft <= 'z') &&
                !(chLeft >= '0' && chLeft <= '9')) {
                i++;
                continue;
            }

            if (!(chRight >= 'A' && chRight <= 'Z') &&
                !(chRight >= 'a' && chRight <= 'z') &&
                !(chRight >= '0' && chRight <= '9')) {
                j--;
                continue;
            }
                        
            //大寫英文字母轉成小寫
            if (chLeft >= 'A' && chLeft <= 'Z') {
                chLeft = chLeft + 'a' - 'A';
            }
            
            if (chRight >= 'A' && chRight <= 'Z') {
                chRight = chRight + 'a' - 'A';
            }
            
            //比較字符
            if (chLeft != chRight) {
                return false;
            } else {
                i++;
                j--;
            }
        }
        
        return true;
    }
}

END

相關文章
相關標籤/搜索