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

Reverse Integer

时间:2018-09-27 01:37:06      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:bsp   eal   dea   function   color   span   store   margin   UNC   

Description:

Given a 32-bit signed integer, reverse digits of an integer.

Note:

Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [?231,  231 ? 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

Solution:

 1 int reverse(int x)
 2 {
 3     int ans = 0;
 4     int tmp = 0;
 5     while (x) {
 6         tmp = ans * 10 + x % 10;
 7         if (tmp / 10 != ans) {
 8             return 0;
 9         }
10         ans = tmp;
11         x /= 10;
12     }
13     return ans;
14 }

tmp = ans * 10 + x % 10 = ans * 10 + (x - x / 10 * 10)

tmp / 10 = ans + x / 10  - x / 10 = ans

if integer tmp doesn‘t overflow.

 

Reverse Integer

标签:bsp   eal   dea   function   color   span   store   margin   UNC   

原文地址:https://www.cnblogs.com/ouch/p/9710919.html

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