//整数是不是回文
public static boolean isValid(int num){
if(num <0){
return false;
}
ArrayList result = new ArrayList();
while (num != 0){
int carry = num % 10;
result.add(carry);
num = num / 10;
}
for(int i = 0;i
if(result.get(i) != result.get(result.size() -i -1)){
return false;
}
}
return true;
}
//字符串是不是回文
public static boolean isValid(String str){
for(int i = 0;i
if(str.charAt(i) != str.charAt(str.length()-i-1)){
return false;
}
}
return true;
}
//链表是不是回文
public static boolean isValid(LinkedLIST head){
LinkedLIST fast = head;
LinkedLIST slow = head;
LinkedLIST pre = null;
LinkedLIST next = null;
while (fast != null && fast.next != null){
fast =fast.next.next;
next = slow.next;
slow.next = pre;
pre = slow;
slow = next;
}
if(fast != null){
slow = slow.next;
}
while (pre != null && slow != null){
if(pre.value != slow.value){
return false;
}
pre = pre.next;
slow = slow.next;
}
return true;
}
网友评论