问题:

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. poj 1276

    一道DP的题目,还是一道多重背包的题目,第一次接触. 题意:有现今cash,和n种钱币,每种钱币有ni个,价值为di,求各种钱币组成的不超过cash的最大钱数 思路:可以转换为0/1背包和完全背包来做 ...

  2. poj 1011

    http://poj.org/problem?id=1011 这是一道POJ的搜索的题目,最开始确实难以理解,但做过一些搜索的题目后,也没那么难了. 大概题意就是,现在有N根木头,要拼成若干根木头,并 ...

  3. 79 两个整数集合A和B,求其交集

    [本文链接] http://www.cnblogs.com/hellogiser/p/ab-intersect.html [题目] 两个整数集合A和B,求其交集. [分析]   1. 读取整数集合A中 ...

  4. Oracle分页语句

    select * from (select A.*,rownum rd from (select * from [tablename] where [condition] order by  [con ...

  5. windows8 安装TortoiseSVN后的反应

    因为工作需要,昨天安装了TortoiseSVN 64位版,没有马上重启. 随后,IIS中打开页面后浏览器一片空白,没有网页,没有地址,什么都没有.这时还没想到可能是TortoiseSVN的问题.后来实 ...

  6. FZU 2165 v11(最小重复覆盖)+ codeforces 417D Cunning Gena

    告诉你若干个(<=100)武器的花费以及武器能消灭的怪物编号,问消灭所有怪物(<=100)的最小花费...当然每个武器可以无限次使用,不然这题就太水了╮(╯▽╰)╭ 这题当时比赛的时候连题 ...

  7. UIScrollView控件实现图片缩放功能

    转发自:http://www.cnblogs.com/wendingding/p/3754268.html 一.缩放 1.简单说明: 有些时候,我们可能要对某些内容进行手势缩放,如下图所示 UIScr ...

  8. MVC3.0 EF增删改查的封装类

    本人亲身使用EF CodeFirst,因为增删改查都是使用EF内置的一些方法,我想把它封装到一个类调用就行了.结合网上的资料和自己的整理,若有不对的地方望斧正,感激不尽.直接上代码吧.我就用新闻的增删 ...

  9. string int 转换

    int转stringint n = 0;std::stringstream ss;std::string str;ss<<n;ss>>str;string转intstd::st ...

  10. 局域网聊天Chat(马士兵视频改进版)

    Github地址: https://github.com/BenDanChen/Chat Chat 小小的聊天系统,主要是跟着网上的马士兵老师的公开视频然后再自己反思有什么地方需要改进的地方,然后大体 ...