給定一個字符串涌矢,判斷其是否是回文
public class Parlindrome {
public boolean parlindrome(char[] str) {
if (str == null || str.length == 0) {
return false;
}
//指向頭,尾
int i = 0;
int j = str.length - 1;
//當指針相遇則是回文
while (i < j) {
//如果頭尾所指不同吨瞎,則不是回文
if (str[i] != str[j]) {
return false;
}
//向中間靠攏
i++;
j--;
}
return true;
}
public static void main(String[] args) {
Parlindrome parlindrome = new Parlindrome();
System.out.println(parlindrome.parlindrome("stttttss".toCharArray()));
}
}