【LeetCode7】Reverse Integer★
题目描述:

解题思路:
反转的方法很简单,重点在于判断溢出的问题,下面给出了两种方法。
Java代码:
方法一:
判断溢出方法:在执行完int newResult=result*10+tail语句后,紧接着进行逆运算result=(newResult-tail)/10,如果出现溢出,那么逆运算后result和newResult必然不相等,反之,如果没有溢出,则逆运算后result=newResult。
public class LeetCode7 {
public static void main(String[] args) {
int x1=123;
System.out.println(x1+"的反转结果是:"+new Solution().reverse(x1));
int x2=1534236469;
System.out.println(x2+"的反转结果是:"+new Solution().reverse(x2));
}
}
class Solution {
public int reverse(int x) {
int result=0;
while(x!=0){
int tail=x%10;
int newResult=result*10+tail;
//判断溢出
if((newResult-tail)/10!=result)
return 0;
result=newResult;
x=x/10;
}
return result;
}
}
程序结果:

方法二:
判断溢出方法:采用long类型存储翻转后的数,再与 Integer.MAX_VALUE 和 Integer.MIN_VALUE 比较,判断是否溢出。
public class LeetCode7 {
public static void main(String[] args) {
int x1=123;
System.out.println(x1+"的反转结果是:"+new Solution().reverse(x1));
int x2=1534236469;
System.out.println(x2+"的反转结果是:"+new Solution().reverse(x2));
}
}
class Solution {
public int reverse(int x) {
long result=0;
while(x!=0){
int tail=x%10;
long newResult=result*10+tail;
result=newResult;
x=x/10;
}
//根据是否溢出返回结果
return (result > Integer.MAX_VALUE || result < Integer.MIN_VALUE)?0:(int)result;
}
}
程序结果:

结论:
本人比较倾向第一种方法,因为第一种方法不需要借助语言本身的常量值。
【LeetCode7】Reverse Integer★的更多相关文章
- LeetCode 【2】 Reverse Integer --007
六月箴言 万物之中,希望最美:最美之物,永不凋零.—— 斯蒂芬·金 第二周算法记录 007 -- Reverse Integer (整数反转) 题干英文版: Given a 32-bit signed ...
- 【leetcode】Reverse Integer(middle)☆
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 总结:处理整数溢出 ...
- 【leetcode】Reverse Integer
题目描述: Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 很简单 ...
- 【Leetcode】【Easy】Reverse Integer
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Have you ...
- 【Leetcode-easy】Reverse Integer
思路:取绝对值,反转,并判断反转的结果是否大于最大整数,需要注意的细节:判断时需要这样:result > (Integer.MAX_VALUE - v) / 10 否则result * 10 + ...
- 【LeetCode】Reverse Integer(整数反转)
这道题是LeetCode里的第7道题. 题目描述: 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321 示例 2: 输入: -123 ...
- 【LeetCode】397. Integer Replacement 解题报告(Python)
[LeetCode]397. Integer Replacement 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/inte ...
- LeetCode【7】.Reverse Integer--java实现
Reverse Integer 题目要求:给定一个int 类型值,求值的反转,例如以下: Example1: x = 123, return 321 Example2: x = -123, ...
- 【LeetCode算法-7】Reverse Integer
LeetCode第7题: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Outp ...
随机推荐
- CPA理论与Base理论
CPA理论: 由于对系统或者数据进行了拆分,我们的系统不再是单机系统,而是分布式系统,针对分布式系统的CAP原理包含如下三个元素. C:Consistency,一致性.在分布式系统中的所有数据 备份, ...
- react单页面应用的Nginx配置问题
项目中多数使用react单页面开发,路由使用react-router的browser-router,这样页面访问路径看起来像是真实的,如http://xx.xxx.xxx/a/b.但当项目访问路径为多 ...
- Python+Selenium笔记(十三):Page Object设计模式
(一) 前言 简单的说就是分为2层,页面class 和测试class. 页面class:分为父类和子类(子类指具体的页面,每一个页面都创建一个类),父类中定义公有的属性和方法(操作). #对面向对象有 ...
- 机器学习实战(Machine Learning in Action)学习笔记————09.利用PCA简化数据
机器学习实战(Machine Learning in Action)学习笔记————09.利用PCA简化数据 关键字:PCA.主成分分析.降维作者:米仓山下时间:2018-11-15机器学习实战(Ma ...
- 【three.js练习程序】创建地球贴图
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- Pytest+Allure环境的搭建
参考博客 测试报告解释 pytest+allurre进阶 1. pytest的安装: 1.1. windows下: pip install pytest 1.2. linux下: pip instal ...
- LeetCode题解之 Letter Case Permutation
1.题目描述 2.问题分析 可以使用递归的方法解决,参考了别人的答案才写出来的. 3.代码 vector<string> letterCasePermutation(string S) { ...
- svn其它
参考地址: http://www.cnblogs.com/mymelon/p/5483215.html
- Oracle EBS CST 成本请求报错
(文档 ID 430533.1) When running CMCPAW, Periodic Actual Cost Worker, an error is received in the logf ...
- Oracle EBS AP 取消付款
--取消付款 created by jenrry 20170425 declare l_return_status varchar2(50); l_msg_count number; l_msg_da ...