[面试真题] LeetCode:Reverse Integer

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         // Start typing your C/C++ solution below
 5         // DO NOT write int main() function
 6         int sig = 1;
 7         if(x<0){
 8             sig = -1;
 9             x = -x;
10         }
11         int rtn = 0;
12         while(x>0){
13             rtn = rtn * 10 + x%10;
14             x /= 10;
15         }
16         if(sig == -1){
17             return -rtn;
18         }
19         return rtn;
20     }
21 };

Run Status: Accepted!
Program Runtime: 56 milli secs

Progress: 1020/1020 test cases passed.

 

posted @ 2013-05-14 12:43  infinityu  阅读(302)  评论(0编辑  收藏  举报