Valid Palindrome

题目描述
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.

题目出处: https://oj.leetcode.com/problems/valid-palindrome/

解题思路
本题判断字符串是否为回文,只考虑字母数字且不考虑大小写。

自己的代码1
这个代码超时了,需要寻找更加有效的方法。
package leetcode;

public class ValidPalindrome {
	public boolean isPalindrome(String s) {
		if(s == null) return false;
		if(s == "") return true;
		
		s = s.toLowerCase();//System.out.println(s);
		String str = "";
		for(int i = 0; i < s.length(); i++){
			char c = s.charAt(i);
			if(c >= 'a' && c <= 'z') str += c;
		}//System.out.println(str);
		
		if(str.length() == 0) return true;
		if(str.length() == 1) return true;
		boolean palindrome = true;
		for(int i = 0; i < str.length()/2; i++){
			if(str.charAt(i) != str.charAt(str.length()-1-i)){
				palindrome = false;
				break;
			}
		}
		
		
        return palindrome;
    }
	
	public static void main(String[] args) {
		//String s = "";
		//String s = "   ";
		//String s = " , .  ";
		//String s = "A man, a plan, a canal: Panama";
		String s = "race a car";
		
		ValidPalindrome vp = new ValidPalindrome();
		System.out.println(vp.isPalindrome(s));
	}
}


自己的代码2
这段代码可以运行,只对字符串进行了一次遍历。
package leetcode;

public class ValidPalindrome {
	public boolean isPalindrome(String s) {
		if(s == null) return false;
		if(s == "") return true;
		if(s.length() == 1) return true;
		
		/*int a = 'a';System.out.println(a);
		int z = 'z';System.out.println(z);
		int A = 'A';System.out.println(A);
		int Z = 'Z';System.out.println(Z);
		int A1 = 'A' - 32;System.out.println((char)A1);
		int zero = '0';System.out.println(zero);
		int nine = '9';System.out.println(nine);*/
		
		int begin = 0;
		int end = s.length() - 1;
		boolean palindrome = true;
		while(true){
			char cBegin = 0, cEnd = 0;
			while(begin < s.length()){
				cBegin = s.charAt(begin);
				if((cBegin >= 'A' && cBegin <= 'Z') || (cBegin >= '0' && cBegin <= '9')) break;
				else if(cBegin >= 'a' && cBegin <= 'z') {
					cBegin = (char) (cBegin - 32);
					break;
				}
				else begin++;
			}
			while(end >= 0){
				cEnd = s.charAt(end);
				if((cEnd >= 'A' && cEnd <= 'Z') || (cEnd >= '0' && cEnd <= '9')) break;
				else if(cEnd >= 'a' && cEnd <= 'z') {
					cEnd = (char) (cEnd - 32);
					break;
				}
				else end--;
			}
			
			if(begin >= end) break;
			else{
				if(cBegin != cEnd) {
					palindrome = false;
					break;
				}
				else{
					begin++;
					end--;
				}
			}
		}
		
		
        return palindrome;
    }
	
	public static void main(String[] args) {
		//String s = "";
		//String s = "   ";
		//String s = " , .  ";
		//String s = "A man, a plan, a canal: Panama";
		//String s = "race a car";
		//String s = "aA";
		//String s = "1a2";
		String s = "ab2a";
		
		ValidPalindrome vp = new ValidPalindrome();
		System.out.println(vp.isPalindrome(s));
	}
}

你可能感兴趣的:(ROM)