首页 > 代码库 > LeetCode Reverse Integer

LeetCode 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 temp=x; 4             int sum=0; 5             if (x<0) { 6                 x=-x; 7             } 8             if (x==0) { 9                 return 0;10             }11             while(x%10==0){12                 x=x/10;    13             }14             while (x!= 0) {15                 sum=sum*10+x%10;16                 x=x/10;17             }18             if (temp>0) {19                 return sum;20             }21             return 0-sum;22     }23 }

 

LeetCode Reverse Integer