题解 | #回文数字#
回文数字
http://www.nowcoder.com/practice/35b8166c135448c5a5ba2cff8d430c32
import java.util.*;
public class Solution {
/**
*
* @param x int整型
* @return bool布尔型
*/
public boolean isPalindrome (int x) {
if(x == 0) return true ;//0是回文
if(x < 0) return false ;//负数不是回文
int count = 1 ;//记录x的量级 例如101量级是100,15量级是10
int tmp = x ;
while(tmp / 10 != 0) {
tmp /= 10 ;
count *= 10 ;
}
int leftBase = count ;//左边基数
int rightBase = 1 ;//右边基数
while(leftBase > rightBase) {//依次取两端的数,相比较
int leftCur = x / leftBase % 10 ;
int rightCur = x / rightBase % 10 ;
if(leftCur != rightCur) {
return false ;
}
leftBase /= 10 ;
rightBase *= 10 ;
}
return true ;
}
}
一个菜鸟的算法刷题记录 文章被收录于专栏
分享一个菜鸟的成长记录
