My Java solution, without using extra space and overflow


  • 0
    I
    public boolean isPalindrome(int x) {
    	        if(x<0){
    	        	return false;
    	        }
    	        if(0<=x && x<=9){
    	           return true; 
    	        }
    		    x=Math.abs(x);
    		    int cp =x;
    		    int count=-1;
    		    while(cp>0){
    		    	cp=cp/10;
    		    	count++;
    		    }
    		    int a=x;
    		    int b=x;
    		    while(a>0 && b>0){
    		      int temp1=(int)(a/Math.pow(10, count));
    		      a=a-temp1*(int)(Math.pow(10, count));
    		      count--;
    		      int temp2=b%10;
    		      b=b/10;
    		     
    		      if(temp1!=temp2){
    		    	  return false;
    		      }
    		    }
    		    return true; 
    		 }

  • 2
    U

    why does extra space exactly mean, you used a few ints in your program, does these count?


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.