LeetCode第7题:

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.

解题思路:

假如:x=1234,y=0

1)y乘以10,x把末位4移除掉,加给y(x=123,y=4)

2)y乘以10,x把末位3移除掉,加给y(x=12,y=43)

3)y乘以10,x把末位2移除掉,加给y(x=1,y=432)

4)y乘以10,x把末位1移除掉,加给y(x=0,y=4321)

代码:

int y = 0;
while(x/10!=0){
y*=10;
y+=x%10;
x/=10;
}
y=y*10 +x;

结果报错

原因是int的范围是-2147483647~2147483648

leetcode给的input是1534236469,倒转之后是9646324351,导致溢出了。所以要加上溢出判断

最后的代码

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

这也是很坑啊,溢出就输出0,鬼知道啊

【LeetCode算法-7】Reverse Integer的更多相关文章

  1. 【算法】LeetCode算法题-Reverse Integer

    这是悦乐书的第143次更新,第145篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第2题(顺位题号是7),给定32位有符号整数,然后将其反转输出.例如: 输入: 123 ...

  2. LeetCode算法题-Reverse Bits(Java实现)

    这是悦乐书的第185次更新,第187篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第44题(顺位题号是190).给定32位无符号整数,求它的反转位.例如: 输入:4326 ...

  3. LeetCode算法题-Reverse Words in a String III(Java实现)

    这是悦乐书的第259次更新,第272篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第126题(顺位题号是557).给定一个字符串,您需要反转句子中每个单词中的字符顺序,同 ...

  4. LeetCode算法题-Reverse String II(Java实现)

    这是悦乐书的第256次更新,第269篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第123题(顺位题号是541).给定一个字符串和一个整数k,你需要反转从字符串开头算起的 ...

  5. LeetCode算法题-Reverse String(Java实现)

    这是悦乐书的第205次更新,第217篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第73题(顺位题号是344).编写一个以字符串作为输入并返回字符串的函数.例如: 输入: ...

  6. LeetCode算法题-Reverse Linked List(Java实现)

    这是悦乐书的第192次更新,第195篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第51题(顺位题号是206).反转单链表.例如: 输入:1-> 2-> 3- ...

  7. LeetCode算法题-Reverse Vowels of a String(Java实现-四种解法)

    这是悦乐书的第206次更新,第218篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第74题(顺位题号是345).编写一个函数,它将一个字符串作为输入,并仅反转一个字符串的 ...

  8. 《LeetBook》leetcode题解(7): Reverse Integer[E]——处理溢出的技巧

    我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 书的地址:https://hk029.gitbooks.io/leetboo ...

  9. C# 写 LeetCode easy #7 Reverse Integer

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

  10. Leetcode练习题 7. Reverse Integer

    7. Reverse Integer 题目描述: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Inp ...

随机推荐

  1. FormData中delete方法在ios不兼容

    1.移动端直接用的input的file上传图片(name=“file”必填) <input type="file" id="exampleInputFile1&qu ...

  2. swift 实践- 01 -- UItableView的简单使用

    import UIKit class ViewController: UIViewController ,UITableViewDelegate,UITableViewDataSource{ over ...

  3. Modbus库开发笔记之四:Modbus TCP Client开发

    这一次我们封装Modbus TCP Client应用.同样的我们也不是做具体的应用,而是实现TCP客户端的基本功能.我们将TCP客户端的功能封装为函数,以便在开发具体应用时调用. 对于TCP客户端我们 ...

  4. Confluence 6 嵌入的 H2 数据库

    为了让你的 Confluence 在安装成功后就可以使用而不需要使用任何外部的数据库,Confluence 使用一个嵌入的 H2 数据库. 当你选择对 Confluence 进行评估和测试的时候,H2 ...

  5. android studio 包名冲突解决

    Error: Execution failed for task ': app: packageAllDebugClassesForMultiDex'. > Java.util.zip.ZipE ...

  6. CommonJs、AMD、CMD模块化规范

    /** * CommonJS 模块化规范 * CommonJS规范加载模块是同步的,也就是说,只有加载完成,才能执行后面的操作 */ /*-------Node.js遵循Commonjs规范----- ...

  7. wireless Penetration Testing & Honeypot and Mis-Association attacks

    重新记一遍 ,在捕获握手数据包的时候不容易获取,所以使用ARP请求.使用自己的无线网卡的地址发送请求,会容易使得无线开启端掉线,迫使重新连接. 1.使用命令   aireplay-ng -3 -b a ...

  8. jacoco + eclipse单元测试覆盖率

    概念 Jacoco:JaCoCo是一个开源的覆盖率工具,它针对的开发语言是java,其使用方法很灵活,可以嵌入到Ant.Maven中:可以作为Eclipse插件,可以使用其JavaAgent技术监控J ...

  9. Python集合(set)

    Python中的集合同数学中的集合概念类似,也是用于保存不重复的元素.他有可变集合(set),和不可变集合(frozenset);可变集合(set)是无序的可变的. 创建集合 直接使用{}创建 set ...

  10. axure--轮播图

    1.使用动态面板的循环实现图片轮播的要点:1)当鼠标移出动态面板的范围时才显示左右两边的方向按钮,否则该两个按钮都是隐藏的.则思路如下:且四个条件之间是“or”的关系,不是“and”[[Cursor. ...