问题:

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?

Throw an exception? Good, but what if throwing an exception is not an option? You would then have to re-design the function (ie, add an extra parameter).

分析:

简单的分析逆置一个整数比较简单,代码如下:

public class Solution {
public int reverse(int x) {
int result = 0;
while(x != 0){
result = result*10+x%10;
x = x/10;
}
return result;
}
}

上面的代码对于类似10100这样的数逆置之后就变为了101,也算可以接受,但是没有考虑溢出的问题。

 

在考虑溢出这个问题上,

我们可以先把原数x的符号位记录下来,在java中x的符号位可以获取为 int sign=x>>31,当sign为-1表示为负数,否则为正。

然后按照上面的方法求逆置的数result,再判断result的符号位是否和原数x相同,若相同则没有溢出;否则溢出。

实现的代码如下:

public int  reverse2(int x) {
int result = 0;
int sign = x>>31;
while (x != 0) {
result = result * 10 + x % 10;
x = x / 10;
}
// System.out.println(sign+", "+(result>>31));
if(sign!=(result>>31)){
System.out.println("overflow..");
System.exit(1);
}
return result;
}

 


以下是我用于测试的完整代码:

public class ReverseInt {
public static void main(String[] args) {
ReverseInt r = new ReverseInt();
System.out.println(r.reverse2(123));
System.out.println(r.reverse2(1230));
System.out.println(r.reverse2(-123));
System.out.println(r.reverse2(1000000003)); }
public int reverse(int x) {
int result = 0;
while (x != 0) {
result = result * 10 + x % 10;
x = x / 10;
}
return result;
}
public int reverse2(int x) {
int result = 0;
int sign = x>>31;
while (x != 0) {
result = result * 10 + x % 10;
x = x / 10;
}
// System.out.println(sign+", "+(result>>31));
if(sign!=(result>>31)){
System.out.println("overflow..");
System.exit(1);
}
return result;
}
}

LeetCode——Reverse Integer(逆置一个整数)的更多相关文章

  1. leetcode:Integer to Roman(整数转化为罗马数字)

    Question: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the rang ...

  2. [leetcode]273. Integer to English Words 整数转英文单词

    Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...

  3. LeetCode: Reverse Integer 解题报告

    Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, retur ...

  4. [LeetCode] Reverse Integer 翻转整数

    Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to ...

  5. [Leetcode] reverse integer 反转整数

    Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 click to ...

  6. LeetCode Reverse Bits 反置位值

    题意:给定一个无符号32位整数,将其二进制形式左右反置,再以整型返回. 思路:循环32轮,将n往右挤出一位就补到ans的尾巴上. class Solution { public: uint32_t r ...

  7. C++ leetcode::Reverse Integer

    第一天上课,数据库老师说对于计算机系的学生,凡是在课本上学到的专业知识都是过时的.深以为然,感觉大学两年半真的不知道学了什么,为未来感到担忧,C++也不敢说是精通,入门还差不多.最近丧的不行,不管怎么 ...

  8. [LeetCode] 273. Integer to English Words 整数转为英文单词

    Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...

  9. Leetcode: Reverse Integer 正确的思路下-要考虑代码简化

    题目: Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Have ...

随机推荐

  1. linux自动定时备份web程序和mysql数据库

    前些天受朋友说linux定时备份不知道怎么搞,叫帮忙处理一下.由于这段时间正闲着,所以也就欣然答应.由于朋友对linux不懂也希望我将操作的过程记录下来,也就是越详细越好.所以写得比较$%^& ...

  2. Light OJ 1032

    数位dp,许多数位dp需要统计某种模式(子串)出现的数量,这种题通常需要在递归参数中加入高位已经出现过的模式的数量. #include <cstdio> #include <cstr ...

  3. ios 在storyboard 和 xib中,显示自定义view的预览效果

    发现FSCalendar这个控件能在xib中显示预览效果,是怎么实现的呢?其中涉及的知识又有哪些? 主要就是IBInspectable 和 IB_DESIGNABLE 先看 IBInspectable ...

  4. struts.xml配置

    1. package标签 package:完成有业务相关的Action(应用控制器的)管理 name:给包起的名字(反映该包中Action的功能),用来完成包和包之间的继承.默认继承struts-de ...

  5. IntelliJ IDEA 15.0.4常用快捷键整理

    一.背景 最近刚转了IDEA,感觉真是爽的一逼,太智能了,回不去Eclipse了,还有些淡淡的忧伤呢~在使用中很多的快捷键帮了开发的大忙,让我可以达到事半功倍的效果,下面就罗列出来,与大家共同分享. ...

  6. 添加thrust的库后出错

    在添加thrust库中的host_vector.h等头文件时 C:\NVIDIA\cudatoolkit\include\thrust\detail\config中的debug.h一直出问题,因此注释 ...

  7. sqlplus 设置

    set heading offset line 40001.设置页面显示总行数show pagesize; //首先查看目前的pagesize,默认是14set pagesize 100; //将pa ...

  8. cocospod 安装和使用 podfile 问题解决

    Podfile 不识别 usr_framework!,系本地Pods版本太低,要在0.36以上. 以下转自:http://blog.csdn.net/eqera/article/details/393 ...

  9. PullToRefreshScrollView嵌套SwipeMenuListView冲突问题解决

    参考: http://blog.csdn.net/u012255016/article/details/46048797 public class NoScrollSwipeMenuListView ...

  10. ecgcd(解二元不定方程)

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=775 关于扩展欧几里得算法还是推一遍好啦: 有方程:a*x+b*y=d=gcd(a, b) ...