问题:

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. JS添加删除DIV

    function addDiv(w,h){     //如果原来有“divCell”这个图层,先删除这个图层    deleteDiv();    //创建一个div       var my = d ...

  2. centos7.0 安装vsftp实录

    安装VSFTP # 使用yum安装 yum -y install ftp vsftpd # 或者使用rpm安装以下两个包 .el7.x86_64 vsftpd--.el7.x86_64 # 另外需要安 ...

  3. 搭建Solr集群的推荐方案

    之前介绍过2篇SolrCloud的部署流程,第一个是使用安装脚本的方式进行抽取安装,启动比较方便,但是会创建多个目录,感觉比较乱:第二个是官方教程上提供的方法,使用比较简单,直接释放压缩包即可,并且启 ...

  4. yii框架详解 之 国际化 (I18N)

    我们要开启组件中们关于语言的配置,默认的就是CPhpMessageSource,也可以改为其他的方式. #组件配置中  'messages' => array(     'class'=> ...

  5. codeforces 425B Sereja and Table (枚举、位图)

    输入n*m的01矩阵.以及k. n,m<=100,k<=10 问修改至多k个,使得矩阵内的各连通块(连着的0或1构成连通块)都是矩形,且不含另外的数字(边界为0(1)的矩形内不含1(0)) ...

  6. 深入浅出JMS(一)——JMS简介

    如果手机只能进行实时通话,没有留言和短信功能会怎么样?一个电话打过来,正好没有来得及接上,那么这个电话要传递的信息肯定就收不到了.为什么不能先将信息存下来,当用户需要查看信息的时候再去获得信息呢?伴随 ...

  7. 【leetcode】Single Number II (medium) ★ 自己没做出来....

    Given an array of integers, every element appears three times except for one. Find that single one. ...

  8. 如何手动添加Windows服务和如何把一个服务删除

    windows 手动添加服务方法一:修改注册表 在注册表编辑器,展开分支"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services" ...

  9. jquery[siblings]取得一个包含匹配的元素集合中每一个元素的所有唯一同辈元素的元素集合

    取得一个包含匹配的元素集合中每一个元素的所有唯一同辈元素的元素集合,用于筛选同辈元素的表达式 $("#pageList").click(function(){ $(this).pa ...

  10. 如何获得images.xcassets 中图片的路径?

    UIImage加载图片的方式以及Images.xcassets对于加载方法的影响 重点: Images.xcassets中的图片资源只能通过imageNamed:方法加载,通过NSBundle的pat ...