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

Reverse Integer

时间:2014-09-29 23:48:01      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   ar   strong   sp   div   c   

题目描述:

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         //char word[15];
 5         int flag;
 6         int result = 0;
 7         int count = 1;
 8         if( x >= 0){
 9             flag = 1;
10         }else{
11             flag = -1;
12             x = -x;
13         }
14         while(true){
15             int temp = x % 10;
16             //cout<<temp<<endl;
17             result = result * count + temp;
18             count = 10;
19             x /= 10;
20             //cout << x<<endl;
21             if(x == 0){
22                 break;
23             }
24         }
25 
26         return flag * result;
27     }
28 };

 

Reverse Integer

标签:style   blog   color   io   ar   strong   sp   div   c   

原文地址:http://www.cnblogs.com/skycore/p/4001109.html

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