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, ...
随机推荐
- 【TCP/IP网络编程】:01理解网络编程和套接字
1.网络编程和套接字 网络编程与C语言中的printf函数和scanf函数以及文件的输入输出类似,本质上也是一种基于I/O的编程方法.之所以这么说,是因为网络编程大多是基于套接字(socket,网络数 ...
- CRF keras代码实现
这份代码来自于苏剑林 # -*- coding:utf-8 -*- from keras.layers import Layer import keras.backend as K class CRF ...
- oracle里面查询重复数据的方法
一张person表,有id和name的两个字段,id是唯一的不允许重复,id相同则认为是重复的记录. select id from group by id having count(*) > 1 ...
- [反汇编] 获取上一个栈帧的ebp
使用代码 lea ecx, [ebp+4+参数长度] 就可以实现. 如下图,理解栈帧的结构,很好理解. 虽然也是 push param的,但这部分在恢复时被调用函数会恢复的,因此这并不算esp的值. ...
- Ajax的快速入门
1.什么是ajax ajax是技术名词的缩写: Asynchronous:异步: JavaScript:JavaScript语言: And:和.与: XML:数据传输格式 ajax是客户端通过HTTP ...
- QP编码详解
- 原理 QP编码是一种使用可打印的ASCII字符 (如字母.数字与"=")表示各种编码格式下的字符.其方法是将一个8bit的字符表示成两个16进制数,并在前面加一个"= ...
- .net core使用百度webupload上传图片
后端代码: /// <summary> /// 图片上传,上传路径: "/Uploads/Images/" + folder + "/" + sav ...
- [PHP] 持续交付Jenkins安装
1.下载并运行 Jenkins下载 Jenkins.http://mirrors.jenkins.io/war-stable/latest/jenkins.war 打开终端进入到下载目录.运行命令 j ...
- 4-8 pie与布局
In [1]: %matplotlib inline import matplotlib.pyplot as plt 1.pie简单参数:plt.pie(x, explode=None, labe ...
- IDEA运行单个Java文件
对于某些Java示例可能是只有单个文件,并不是完整的Java工程,那么要如何运行单个Java文件呢,以IDEA为例. 我的环境: IDEA 2017.3.2 jdk 1.8.0.73 操作步骤: 1. ...