Leetcode练习题 7. Reverse Integer
7. Reverse Integer
题目描述:
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.
问题解法 一:
class Solution {
public int reverse(int x) {
int reversed = 0;
int pop = 0;
while(x!=0)
{
pop = x%10;
x = x/10;
if(reversed>Integer.MAX_VALUE/10 || (reversed==Integer.MAX_VALUE/10 && pop>7)){
return 0;
}
if(reversed<Integer.MIN_VALUE/10 || (reversed==Integer.MIN_VALUE/10 && pop<-8)){
return 0;
}
reversed = reversed*10+pop;
}
return reversed;
}
}
在本题中,难点主要是有限整数的翻转和防止值溢出。
- 对于有限整数的翻转,本题采用的方法是用循环,取余,再除以10的方法
- 而对于栈溢出
根据如下公式:

采用:
if (reversed>Integer.MAX_VALUE || (reversed==Integer.MAX_VALUE && pop>7)
if(reversed<Integer.MIN_VALUE || (reversed==Integer.MIN_VALUE && pop<-8))
问题解法 二:
当然还有一种解法就是使用long型号数组,再转化成(int), 这里省去了复杂的公式判断;因为在int型中,如果不按照公式进行判断的话,就会溢出,缺点是由于测试数据并未超过long型号的长度,所以也能通过。
public int reverse(int x) {
long res = 0;
while (x != 0) {
res = res * 10 + x % 10;
x = x / 10;
}
if (res < Integer.MIN_VALUE || res > Integer.MAX_VALUE) {
return 0;
} else {
return (int)res;
}
}
Leetcode练习题 7. Reverse Integer的更多相关文章
- 《LeetBook》leetcode题解(7): Reverse Integer[E]——处理溢出的技巧
我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 书的地址:https://hk029.gitbooks.io/leetboo ...
- C# 写 LeetCode easy #7 Reverse Integer
7.Reverse Integer Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 ...
- 【一天一道LeetCode】#7. Reverse Integer
一天一道LeetCode系列 (一)题目 Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, ...
- 【算法】LeetCode算法题-Reverse Integer
这是悦乐书的第143次更新,第145篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第2题(顺位题号是7),给定32位有符号整数,然后将其反转输出.例如: 输入: 123 ...
- leetcode:7. Reverse Integer
这题简单,也花了我好长时间,我自己写的code比较麻烦,也没啥技巧:按正负性分类执行,先转化成字符串,用stringbuilder进行旋转,如果超出范围了就用try catch public int ...
- 【LeetCode】7. Reverse Integer 整数反转
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:整数,反转,题解,Leetcode, 力扣,Python, ...
- 【LeetCode】#7 Reverse Integer
[Question] Reverse digits of an integer. Example: x = 123, return 321 x = -123, return -321 [My Solu ...
- 【LeetCode】7. Reverse Integer 整型数反转
题目: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 思路:不 ...
- LeetCode(7) - Reverse Integer
题目的要求就是要反转一个Integer,例如输入123,则输出321,这一题比较tricky的地方就是它有可能越界,就是说1234567899,反过来是9987654321是一个越界的Integer, ...
随机推荐
- 跳出"低水平勤奋陷阱"
"低水平勤奋陷阱":摘记更多的知识 读书是获得知识的最基本,最重要的方式,但读书需要方法 所谓"低水平勤奋陷阱",就是花费了大量的时间和精力,但得到的结果却微乎 ...
- MySQL应用之CROSS JOIN用法简介教程
目录 2. cross join用法 @ 本博客翻译自两篇博客的: http://www.mysqltutorial.org/mysql-cross-join/ https://www.w3resou ...
- 13-scrapy中selenium的应用
一. 引入 在通过scrapy框架进行某些网站数据爬取的时候,往往会碰到页面动态数据加载的情况发生,如果直接使用scrapy对其url发请求,是绝对获取不到那部分动态加载出来的数据值.但是通过观察我们 ...
- poj-3682 King Arthur's Birthday Celebration
C - King Arthur's Birthday Celebration POJ - 3682 King Arthur is an narcissist who intends to spare ...
- python处理sqlserver数据库的返回数据
上代码: import SqlHelper.MSSQL as MS import pandas as pd if __name__ == '__main__': #连接数据库 ms = MS.MSSQ ...
- maven新建项目的几种方式和启动
方式一: 第1步:转到 New 菜单 Other.. -> Maven -> Maven Project ,然后单击 Next .如下图所示 - 第2步:在New Maven Projec ...
- 2-1-动态方法:ByTagName()
动态方法:ByTagName() <ul id="list"> <li></li> <li></li> <li&g ...
- Linux中LVM逻辑卷管理
一.简介 LVM是逻辑盘卷管理(Logical Volume Manager)的简称,它是Linux环境下对磁盘分区 进行管理的一种机制,LVM是建立在硬盘和分区之上的一个逻辑层,来提高磁盘分区管理的 ...
- [转]HotSpot VM GC 的种类
原文地址:http://www.cnblogs.com/redcreen/archive/2011/05/04/2037029.html collector种类 GC在 HotSpot VM 5.0里 ...
- mysql判断是否包含某个字符的方法和修改表中指定字段
用locate 是最快的,like 最慢.position一般实战例子:select * from historydatawhere locate('0',opennum) and locate('1 ...