Valid Palindrome

[size=large][color=darkred][b]题目描述[/b][/color][/size]
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.

[b]Note:[/b]
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.

[b]题目出处:[/b][url]https://oj.leetcode.com/problems/valid-palindrome/[/url]

[size=large][color=darkred][b]解题思路[/b][/color][/size]
本题判断字符串是否为回文,只考虑字母数字且不考虑大小写。

[size=large][color=darkred][b]自己的代码1[/b][/color][/size]
[size=medium][b]这个代码超时了,需要寻找更加有效的方法。[/b][/size]
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));
}
}


[size=large][color=darkred][b]自己的代码2[/b][/color][/size]
[size=medium][b]这段代码可以运行,只对字符串进行了一次遍历。[/b][/size]
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));
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值