My accepted Java Code


  • 0
    J
    public class Solution {
    public static int reverse(int x){
    	int j=0;
    	int i=0;
    	if(x<0){
    		x=~x+1;
    		int q=x;
    		if(q<10)
    			return j+=q;
        	else{
    		for(i=0;x>=10;i++){
    			x=x/10;}
    		int k;
    		for(int a=0;q>=10;a++){
    		k=q%10;
    		q=q/10;
    		int result = (int) Math.pow(10,i);
    		j+=k*result;
    		i--;}
        	}
    		return ~(j+q)+1;
    	}
    	int q=x;
    	if(q<10)
    		return j+=q;
    	else{
    	for(i=0;x>=10;i++){
    		x=x/10;}
    	int k;
    	for(int a=0;q>=10;a++){
    	k=q%10;
    	q=q/10;
    	int result = (int) Math.pow(10,i);
    	j+=k*result;
    	i--;}
    	}
    	return j+q;
    }
    

    }


Log in to reply
 

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