您的位置:首页 > 职场人生

Reverse Integer

2015-07-16 20:20 489 查看
题目:
Reverse digits of an integer.

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

Have you thought about this?
Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!

If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100.

Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases?

For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

Update (2014-11-10):

Test cases had been added to test the overflow behavior.

解体思路:

      1.题目是要将一个整形数进行反转;

      2.首先需要考虑负数的符号问题,先判断数的正负性,如果是负数将其变为正数利于计算,为了数不超过内存,重新用long定义一个x1变量存储数;

      3.运用“%”和“/”进行反转。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息