首页 > 代码库 > Reverse Integer

Reverse Integer

Reverse Integer

Reverse digits of an integer.

Example1: x = 123, return 321
Example2: x = -123, return -321

 1 public class Solution { 2     public int reverse(int x) { 3         int result = 0; 4         boolean flag_neg = false; 5         if(x < 0){ 6             flag_neg = true; 7             x = -x; 8         } 9         while(x != 0){10             int temp = x % 10;11             result = result * 10 + temp;12             x /= 10;13         }14         if(flag_neg)15             result = -result;16         return result;17     18     }19 }

 

Reverse Integer