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 store integers within the 32-bit signed integer range: [−231,  231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

Solution 1: comparison to handle overflow

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

Solution 2: use Long to handle overflow

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

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

  1. Python字符串倒序-7. Reverse Integer

    今天做了下LeetCode上面字符串倒序的题目,突然想Python中字符串倒序都有哪些方法,于是网上查了下,居然有这么多种方法: 个人觉得,第二种方法是最容易想到的,因为List中的reverse方法 ...

  2. [LintCode] Reverse Integer 翻转整数

    Reverse digits of an integer. Returns 0 when the reversed integer overflows (signed 32-bit integer). ...

  3. 65. Reverse Integer && Palindrome Number

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

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

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

  5. No.007 Reverse Integer

    7. Reverse Integer Total Accepted: 153147 Total Submissions: 644103 Difficulty: Easy Reverse digits ...

  6. leetcode第七题Reverse Integer (java)

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

  7. Reverse Integer 2015年6月23日

    题目: Reverse digits of an integer. Example1: x = , return Example2: x = -, return - 思路:递归 解答: / test ...

  8. Reverse Integer - 反转一个int,溢出时返回0

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

  9. LeetCode之Easy篇 ——(7)Reverse Integer

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

随机推荐

  1. 将hello程序作为驱动程序编译进系统内核

    0x00开始 恩,可能是我比较愚钝,一个内核编译搞了一天,各种问题,各种bug,几度无奈,也是因为我突发奇想,并没有按照原来的那种操作,我直接把helloworld程序放到内核模块中编译成了一个驱动程 ...

  2. awk grep sed 的一些问题

    条件   匹配    打印含关键字的行 ps aux  | sort -k 4 -r | awk '$4 ~ /^[0-9]/ && $4>0 {print $4,$11}' z ...

  3. 史上最难PHPer笔试题,40分就能月薪过万!附答案

    请批判性的学习,欢迎大牛指正错误 1.有关PHP字符串的说法,不对的是:A.如果一个脚本的编码是 ISO-8859-1,则其中的字符串也会被编码为 ISO-8859-1.B.PHP的字符串在内部是字节 ...

  4. E - Two Arithmetic Progressions(CodeForces - 710D)(拓展中国剩余定理)

    You are given two arithmetic progressions: a1k + b1 and a2l + b2. Find the number of integers x such ...

  5. 数据分析-Matplotlib:绘图和可视化

    学习路线 简介 简单绘制线形图 plot函数 支持图类型 保存图表 1.简介 Matplotlib是一个强大的Python绘图和数据可视化的工具包.数据可视化也是我们数据分析的最重要的工作之一,可以帮 ...

  6. 独立t检验

    方差相同个数相同的独立t检验 5.某饲料厂要比较A.B两种配合饲料在养猪生产中的效果,选取12头情况相似的猪,随机分成两组,分别饲喂两种配合饲料,其60天增重(单位:kg)见下表. 饲 料 60d增重 ...

  7. 1. 模块化的引入与导出 (commonJS规范 和ES6规范)

    node组件导出模块 node一般用commonJS规范 可以通过module.exports导出自己写的模块 这样其他的js文件就可以引用并使用这个模块 module.exports = { log ...

  8. ZJNU 1542 - 三角形(续)--中高级

    从小到大排序后 先固定一遍,另外两边递增查找 即固定 i,j=i+1,k=j+1 然后让k递增到 a[i]+a[j]<=a[k] 时 此时不能凑成一个三角形 答案增加 k-1-j 组 此时不需要 ...

  9. h5 移动端在阻止touchstart的默认事件时报错

    h5 移动端在阻止touchstart的默认事件时报错 解决办法, 可以添加 *{ touch-action: none;}即可消除错误

  10. ant design for vue select 数据回显问题

    例如: 想要回显id为1的温度, 结果直接在select框中显示了1,而不是选中了温度, 此时因为select中的value是string类型, 而我们设置的id是number类型, 对应不上, 所以 ...