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.

Note:
The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows.

解法:

  翻转数字问题需要注意的就是溢出问题,为什么会存在溢出问题呢,我们知道int型的数值范围是 -2147483648~2147483647, 那么如果我们要翻转 1000000009 这个在范围内的数得到 9000000001,而翻转后的数就超过了范围。

  因此,可以采用double类型存储翻转后的数,再与 Integer.MAX_VALUE 和 Integer.MIN_VALUE 比较,判断是否溢出。

  此处并不需要在意正负数的情况,对结果没有影响,因为:12 % 10 = 2;  -12 % 10 = -2

public class Solution {
public int reverse(int x) {
double res = 0;
while (x != 0) {
res = res * 10 + x % 10;
x /= 10;
}
if (res > Integer.MAX_VALUE || res < Integer.MIN_VALUE)
return 0;
else
return (int)res;
}
}

[LeetCode] 7. Reverse Integer ☆的更多相关文章

  1. LeetCode 7 Reverse Integer(反转数字)

    题目来源:https://leetcode.com/problems/reverse-integer/ Reverse digits of an integer. Example1: x = 123, ...

  2. leetcode:Reverse Integer 及Palindrome Number

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

  3. LeetCode 7 Reverse Integer & int

    Reverse Integer 想用余10直接算,没想到 -123%10 是 7, 原因 -123-(-123//10*10) r=a-n*[a/n] 以上,r是余数,a是被除数,n是除数. 唯一不同 ...

  4. Leetcode 7. Reverse Integer(水)

    7. Reverse Integer Easy Given a 32-bit signed integer, reverse digits of an integer. Example 1: Inpu ...

  5. leetcode:Reverse Integer(一个整数反序输出)

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

  6. [LeetCode][Python]Reverse Integer

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/reverse ...

  7. 【LeetCode】Reverse Integer(整数反转)

    这道题是LeetCode里的第7道题. 题目描述: 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321  示例 2: 输入: -123 ...

  8. LeetCode 7. Reverse Integer (倒转数字)

    Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...

  9. [LeetCode] 7. Reverse Integer 翻转整数

    Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...

  10. LeetCode 7. Reverse Integer

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

随机推荐

  1. .Net并行编程 - 并行任务基础知识

    在微软的.NET Framework中,任务是通过System.Threading.Tasks命令空间中的Task类来实现的.它的静态属性Task.Factory是TaskFactory类的一个实例, ...

  2. MongoDB3.2 集群搭建

    一.集群的理论知识 1.1 集群成员 MongoDB的集群类似于GreenPlum集群,由一个入口节点负责任务分发与结果统计,分片结节负责执行任务.不同GP,多了一个config servers. 集 ...

  3. 2018-8-29安装Jitamin过程实录

    2018-8-29安装Jitamin过程实录 新建 模板 小书匠 欢迎走进zozo的学习之旅. 简介 安装 nginx + php + mysql 安装composer 安装Jitamin 简介 在考 ...

  4. 针对“来用”团队项目之NABC分析

    本项目特点之一:扩展性强 NABC分析: N(need):我们这个开发的这个软件主要是集娱乐软件和实用工具于一身的大容器,这里面有很多应用程序,针对不同用户需要,至少有一款应用程序能够满足用户的需要, ...

  5. databales详解(一)

    学习可参考:http://www.guoxk.com/node/jquery-datatables http://yuemeiqing2008-163-com.iteye.com/blog/20069 ...

  6. lintcode-148-颜色分类

    148-颜色分类 给定一个包含红,白,蓝且长度为 n 的数组,将数组元素进行分类使相同颜色的元素相邻,并按照红.白.蓝的顺序进行排序. 我们可以使用整数 0,1 和 2 分别代表红,白,蓝. 注意事项 ...

  7. udf.dll 源码

    一点关于UDF的发散思路 Author:mer4en7y Team:90sec 声明:UDF源码作者langouster 相信各位牛对UDF都不会陌生,看论坛叶总共享了一份UDF源码,以前一直没看过, ...

  8. Windows SDK 非模态对话框的消息处理

    在SDK中使用非模态对话框时的几个问题: 1.为什么要调用IsDialogMessage?? 2.非模态对话框与主窗口有什么区别? 3.如果不调用IsDialogMessage,消息能不能传递到对话框 ...

  9. iOS- 移动端Socket UDP协议广播机制的实现

    1.前言    什么是UDP协议广播机制?      举一个例, 例如在一群人群中,一个人要找张三,于是你向人群里大喊一声(广播):“谁是张三”   如果它是张三,它就会回应你,在网络中也是一样的. ...

  10. alpha阶段个人总结(201521123034陈凯欣)

    一.个人总结 第 0 部分:基本数据结构和算法问题 大二的时候上过数据结构课,感觉自己没有学的太深入,就如之前结对编程时候四则运算有用到的二叉树来解决问题,对于二叉树就有个模糊的概念,实际动手操作起来 ...