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

LeetCode:Reverse Integer

时间:2015-06-16 20:47:16      阅读:102      评论:0      收藏:0      [点我收藏+]

标签:

 

 

problem:

Reverse digits of an integer.

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

solution:逐个取数字; 处理溢出问题 (INT_MIN INT_MAX)

class Solution {
public:
    int reverse(int x) {    

       long result=0;   
        while(x)
        {   
            result=result*10+x%10;
            if(result<INT_MIN||result>INT_MAX)
              return 0;
            x=x/10;
        }     
        return result;
        
        }
};

 

LeetCode:Reverse Integer

标签:

原文地址:http://www.cnblogs.com/xiaoying1245970347/p/4581572.html

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