题解 | #回文数字#
回文数字
http://www.nowcoder.com/practice/35b8166c135448c5a5ba2cff8d430c32
import java.util.*;
public class Solution {
/**
*
* @param x int整型
* @return bool布尔型
*/
// return false;
// }
// int halfNum = 0;
// while(x>halfNum){
// halfNum = halfNum*10+x%10;
// x/=10;
// }
return false;
}
String str = String.valueOf(x);
char[] cs = str.toCharArray();
int left = 0;
int right = str.length()-1;
while(left<right) {
if (cs[left] != cs[right]) return false;
else {
left++;
right--;
}
}
return true;
}
}
public class Solution {
/**
*
* @param x int整型
* @return bool布尔型
*/
public boolean isPalindrome (int x) {
//一半翻转法
// if(x<0||(x%10==0&&x!=0)){// return false;
// }
// int halfNum = 0;
// while(x>halfNum){
// halfNum = halfNum*10+x%10;
// x/=10;
// }
// return x==halfNum||x==halfNum/10;
//双指针法
if (x<0||(x%10==0&&x!=0)){return false;
}
String str = String.valueOf(x);
char[] cs = str.toCharArray();
int left = 0;
int right = str.length()-1;
while(left<right) {
if (cs[left] != cs[right]) return false;
else {
left++;
right--;
}
}
return true;
}
}