LeetCode-7

 

概述

Reverse Integer 简单题,要求将不带符号位的整数反转,如果遇到溢出问题,则输出0。

分析

题目的特别之处在于溢出的处理,数字反转之后很可能超过了 Integer.MAX_VALUE 或者Integer.MIN_VALUE 的范围,应对这种情况,可以将溢出整数范围的的结果认定为0。

参见 SO 上的问题 How does Java handle integer underflows and overflows and how would you check for it?, Java的处理方式是在两个极值之间轮转,超过最大值(2147483647),则从最小值开始逐步趋近最小值(-2147483648),如:

输出结果为:

反之亦然。

解法先用字符串操作的方式进行,还需要探究更高效的算法。

解法

发表评论

邮箱地址不会被公开。 必填项已用*标注

*

您可以使用这些HTML标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

This site uses Akismet to reduce spam. Learn how your comment data is processed.