Given a 32-bit signed integer, reverse digits of an integer.

Example 1:

Input: 123
Output: 321

Example 2:

Input: -123
Output: -321

Example 3:

Input: 120
Output: 21

Note:
Assume we are dealing with an environment which could only hold integers within the 32-bit signed integer range. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.


题目标签:Math

  题目给了我们一个int 数字,让我们倒转它。
  利用 % 10 来拿到最右边数字,然后每次把res * 10 加上新拿到的数字,利用 x / 10 来遍历剩下的数字。
  这一题关键在于,如何检查 overflow,可以利用long,但是如果题目给你的是long,那你如何检查long 是否overflow呢。所以要在不使用更大的type的情况下来检查。
 
  新的res 是如何产生的:
    newRes = res * 10 + x % 10;
  那么如果新的res 没有overflow 的话,把newRes 倒推回去应该是和旧的 res 相等的:
    (newRes - x % 10) / 10 == res
  利用这一点,如果overflow的话,那么倒退回去肯定是 不相等的。
 
 
 
 

Java Solution:

Runtime beats 80.84%

完成日期:06/12/2017

关键词:reverse int

关键点:% 10; / 10

 class Solution
{
public int reverse(int x)
{
int res = 0; while(x != 0)
{
int tail = x % 10;
int newRes = res * 10 + tail; if((newRes - tail) / 10 != res) // check overflow
return 0; res = newRes;
x = x / 10;
} return res;
}
}

参考资料:https://discuss.leetcode.com/topic/6104/my-accepted-15-lines-of-code-for-java

LeetCode 题目列表 - LeetCode Questions List

题目来源:https://leetcode.com/

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(反转数字)

    Given a 32-bit signed integer, reverse digits of an integer. Example 1:                              ...

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

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

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

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

  8. 【leetcode】Reverse Integer(middle)☆

    Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 总结:处理整数溢出 ...

  9. 【JAVA、C++】LeetCode 007 Reverse Integer

    Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 解题思路:将数字 ...

随机推荐

  1. Wrapping calls to the Rational Functional Tester API——调用Rational Functional Tester封装的API

    转自:http://www.ibm.com/developerworks/lotus/library/rft-api/index.html The Rational GUI automation to ...

  2. IBatis的分页研究

    IBatis的分页研究 博客分类: Ibatis学习   摘自: http://cpu.iteye.com/blog/311395 yangtingkun   Oracle分页查询语句 ibaits. ...

  3. .ai域名注册已经极具投资价值进入火爆期

    最近G.ai以六位数的天价被国内域名收藏家收入囊中,间接说明了.ai域名的价值不断攀升,自从2016年AlphaGo胜利以来,人工智能几乎成为人人谈资,而由于.com域名被挖掘待尽,一些聪明的人工智能 ...

  4. Windows开源Web服务器性能和压力测试工具

    linux有很多开源工具用来测试服务器负载,而windows上非常少,几乎没有除了几个复杂的JMeter WET等 将两个好用的工具是Linux版本通过Cygwin移植过来,方便广大windows人员 ...

  5. JSON字符串的生成

    public class Corporation { public string remark { get; set; } public string version { get; set; } pu ...

  6. Iframe用法精析

    String.prototype.match()中正则表达式的g标识存在的时候,函数不会捕获子表达式中的内容,不存在的时候可以. RegExp.prototype.exec()中g的存在只会影响,Re ...

  7. UVA - 808 Bee Breeding (建立坐标系&找规律)

    题目: 输入两个格子的编号a和b(a,b≤10000),求最短距离.例如,19和30的距离为5(一条最短路是19-7-6-5-15-30). 思路: 如图建立坐标系,然后看两个点的向量如果位于二四象限 ...

  8. Servlet的说明及使用案例

    Servlet的说明及使用案例 制作人:全心全意 Servle的基础介绍 Servlet结构体系 Servlet对象.ServletConfig对象与Serializable对象是接口对象,其中Ser ...

  9. 洛谷 2176 [USACO14FEB]路障Roadblock

    [题意概述] 修改图中任一一条边的边权,使其加倍,问怎样使修改后图中的1~n的最短路最大.输出最短路的增量. [题解] 先跑一遍dijkstra求出1~n的路径长度,记录下经过的边.枚举这些边进行修改 ...

  10. 洛谷 4246 BZOJ 1018 [SHOI2008]堵塞的交通

    [题解] 原来线段树还可以这么玩.. 我们用线段树维护连通性.对于一个矩形,我们用4个标记维护4个点的联通情况,再用两个标记维护右边两个点与它们右边的与它们在同一行的点的联通情况. 画图表示,就是 另 ...