您的位置:首页 > 编程语言 > Go语言

【Leetcode Algorithm】Palindrome Number

2015-07-02 15:17 477 查看
Determine whether an integer is a palindrome. Do this withoutextra space.代码:
123456789101112131415161718192021222324252627
public
 
class
 
Solution {
    
public
 
boolean
 
isPalindrome(
int
 
x) {
        
//负数不是回文数字
        
if
(x<
0
){
            
return
 
false
;
        
}
        
//0是回文数字
        
else
 
if
(x==
0
){
            
return
 
true
;
        
}
        
//如果x为正数
        
else
{
            
int
 
tmpx = x;
            
int
 
newx = 
0
;
            
//翻转x
            
while
(tmpx>
0
){
                
newx = newx*
10
 
+ (tmpx%
10
);
                
tmpx = tmpx/
10
            
}
            
//判断翻转后的newx和x是否相同
            
if
(newx==x){
                
return
 
true
;
            
}
            
return
 
false
;
        
}
    
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode