验证回文串
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200915201649212.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDE4Mjc1MA==,size_16,color_FFFFFF,t_70#pic_center)
package algorithm;
public class algorithm_7 {
public boolean isPalindrome(String s) {
char[] strs = s.toCharArray();
int count = 0;
int j = 0;
for (int i = 0; i < strs.length; i++) {
if ((s.charAt(i) > 47 && s.charAt(i) < 58 || s.charAt(i) > 64
&& s.charAt(i) < 91 || s.charAt(i) > 96
&& s.charAt(i) < 123)) {
count++;
}
}
char[] str1 = new char[count];
for (int i = 0; i < strs.length; i++) {
if ((s.charAt(i) > 47 && s.charAt(i) < 58 || s.charAt(i) > 64
&& s.charAt(i) < 91 || s.charAt(i) > 96
&& s.charAt(i) < 123)) {
if (strs[i] > 64 && strs[i] < 91) {
strs[i] = (char) (strs[i] + 32);
}
str1[j] = strs[i];
j++;
}
}
j = 0;
int p = str1.length / 2;
char[] str2 = new char[p];
char[] str3 = new char[p];
for (int i = 0; i < p; i++) {
str2[j] = str1[i];
j++;
}
if (str1.length % 2 == 0) {
j = 0;
for (int i = str1.length - 1; i >= p; i--) {
str3[j] = str1[i];
j++;
}
} else {
j = 0;
for (int i = str1.length - 1; i > p; i--) {
str3[j] = str1[i];
j++;
}
}
String s1 = "";
String s2 = "";
for (int i = 0; i < str2.length; i++) {
s1 = s1 + str2[i];
s2 = s2 + str3[i];
}
if (s1.equals(s2)) {
return true;
} else {
return false;
}
}
public boolean isPalindrome_1(String s) {
StringBuffer sgood = new StringBuffer();
int length = s.length();
for (int i = 0; i < length; i++) {
char ch = s.charAt(i);
if (Character.isLetterOrDigit(ch)) {
sgood.append(Character.toLowerCase(ch));
}
}
StringBuffer sgood_rev = new StringBuffer(sgood).reverse();
return sgood.toString().equals(sgood_rev.toString());
}
public boolean isPalindrome_2(String s) {
StringBuffer sgood = new StringBuffer();
int length = s.length();
for (int i = 0; i < length; i++) {
char ch = s.charAt(i);
if (Character.isLetterOrDigit(ch)) {
sgood.append(Character.toLowerCase(ch));
}
}
int n = sgood.length();
int left = 0, right = n - 1;
while (left < right) {
if (Character.toLowerCase(sgood.charAt(left)) != Character.toLowerCase(sgood.charAt(right))) {
return false;
}
++left;
--right;
}
return true;
}
public static void main(String[] args) {
algorithm_7 a = new algorithm_7();
System.out.println(a.isPalindrome("A man, a plan, a canal: Panama"));
}
}