码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode Reverse Integer

时间:2014-09-11 20:48:02      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   strong   div   sp   log   on   

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

标签:style   blog   color   io   strong   div   sp   log   on   

原文地址:http://www.cnblogs.com/birdhack/p/3967102.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!