Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return-321
1 Public classSolution {2 Public intReverseintx) {3String str=x+ "";4 Booleanisneg=false;5 if(Str.charat (0) = = '-')6 {7Str=str.substring (1);8isneg=true;9 }Ten One //System.out.println (str); A CharTemp=0; - Char[] arr=New Char[Str.length ()]; - the for(intI=0;i<str.length (); i++) - { -arr[i]=Str.charat (i); - } + //System.out.println (arrays.tostring (arr)); - for(inti=0,j=arr.length-1;i<arr.length;i++,j--) + { A if(i<j) at { -temp=Arr[i]; -arr[i]=Arr[j]; -arr[j]=temp; - } - } in //System.out.println (arrays.tostring (arr)); - to //System.out.println (Integer.parseint (string.valueof (arr))); + returnisneg==false? Integer.parseint (string.valueof (arr)):-Integer.parseint (string.valueof (arr)); - } the}
Solution 2 fully illustrates what a lame programmer I am ... Cry...
1 Public intReverseintx) {2 intRes=0;3 while(x!=0)4 {5Res=res*10+x%10;6x/=10;7 }8 returnRes;9}
Reverse Integer (JAVA)