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

相关文章
相关标签/搜索