问题:
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.Coud you solve it without converting the integer to a string?
在真实面试中也是遇到过的问题。
采用java解决
class Solution {
/**
* 思路
* 1,创建一个list集合,遍历原数的每一位,推入集合
* 2,遍历集合,判断每一位与最后一位回退的数是否相等,不等直接返回false
* 3,最后直接返回true
*/
//创建存放每一位的集合
private List list = new ArrayList();
//创建推入集合每位数的方法
public void pushInt(int x){
if(x < 10){
this.list.add(x);
}else{
while(x != 0){
this.list.add(x % 10);
x /= 10;
}
}
}
//创建遍历集合,判断是否为回文数的方法
public boolean isListPalindrome(int x){
int size = this.list.size();
int mid = size/2;
boolean flag = true;
for(int i = 0; i <= mid; i++){
if(this.list.get(i) != this.list.get(size-1-i)){
flag = false;
return flag;
}
}
return flag;
}
public boolean isPalindrome(int x) {
if(x < 0){
return false;
}else if(x < 10){
return true;
}else{
this.pushInt(x);
return this.isListPalindrome(x);
}
}
}