码迷,mamicode.com
首页 > 其他好文 > 详细

【LeetCode从零单排】No.9 Palindrome Number

时间:2015-02-07 13:16:36      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:java   leetcode   

题目

      这道题是迄今为止最快通过的一道题,改了两次就过了,runtime一般(中等偏下,这点不太满意)。Palindrome就是判断一个整数是否对称。

Determine whether an integer is a palindrome. Do this without extra space.

click to show spoilers.

Some hints:

Could negative integers be palindromes? (ie, -1)

If you are thinking of converting the integer to string, note the restriction of using extra space.

You could also try reversing an integer. However, if you have solved the problem "Reverse Integer", you know that the reversed integer might overflow. How would you handle such case?

There is a more generic way of solving this problem.




代码

public class Solution {
    public boolean isPalindrome(int x) {
            if(x<0){
    	    	return false;
    	    }
    	    else{
    	    	  if(x==0){
    	    		  return true;
    	    	  }
    	    	  else{
    	    		  int temp=0;
    	    		  int temp_x=0;
    	    		  temp=x;
    	    		  while(temp/10!=0 || (temp<=9 && temp>0)){
    	    			  temp_x=temp_x*10;
    	    			  temp_x+=temp%10;
    	    			  temp=temp/10;
    	    		  }
    	    		//  System.out.print(""+temp_x);
    	    		  if(x==temp_x){
    	    			  return true;
    	    		  }
    	    		  else{
    	    			  return false;
    	    		  }
    	    		  
    	    	  }
    	    }
 }
    
}



/********************************

* 本文来自博客  “李博Garvin“

* 转载请标明出处:http://blog.csdn.net/buptgshengod

******************************************/



【LeetCode从零单排】No.9 Palindrome Number

标签:java   leetcode   

原文地址:http://blog.csdn.net/buptgshengod/article/details/43602103

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!