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

LeetCode:Reverse Integer

时间:2014-07-26 13:54:24      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   strong   io   for   div   amp   

Reverse digits of an integer.

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

 

 1 class Solution {
 2 public:
 3     int reverse(int x){
 4         int num[100],y=0,i,j;
 5         if(x<0)
 6         {
 7             num[0]=1;
 8             x=-x;
 9         }
10         for(i=1;x>0;i++)
11         {
12             num[i]=x%10;
13             x/=10;
14         }
15         for(j=1;j<i;j++)
16         {
17             y+=num[i-j]*pow(10,j-1);
18         }
19         y=(num[0]==1?-y:y);
20         return y;
21     }
22 };

 

LeetCode:Reverse Integer,布布扣,bubuko.com

LeetCode:Reverse Integer

标签:style   blog   color   strong   io   for   div   amp   

原文地址:http://www.cnblogs.com/levicode/p/3869620.html

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