【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 ...
随机推荐
- js相关判断
//正则表达式判断是否为空 /^\s*?$/.test(obj) //批量转换方法 function sort(s){return s.match(/\d/g).sort()+'' } //判断是否相 ...
- css3的calc()属性
1.calc()是css3的一个新增的功能,用来指定元素的长度,你可以使用calc()给元素的border.margin.pading.font-size和width等属性动态的设置值. 2.calc ...
- OSGI企业应用开发(七)细说Blueprint & Gemini Blueprint(二)
上篇文章介绍了标准的Blueprint 规范与 Gemini Blueprint如何自定义Bean配置文件路径,本文接着上篇文章继续介绍Blueprint的使用. 一.Bean的配置 前面提到过,Ge ...
- mac下的tcp抓包
所需工具: 1.所需工具wireshark软件 操作步骤: 1.打开wireshark添加手机端口监测. 在控制台中输入命令:rvictl -s iPhoneDeviceId 添加成功之后,wires ...
- Retrofit+OKHttp忽略https证书验证
记录这个的原因,是因为很多时候,因为后台配置的证书不正确导致APP访问不到服务器数据,导致影响自身的开发进度.没几行代码,逻辑也清晰,所以下面就直接贴出工具类吧: package huolongluo ...
- 准备开发一个运行在Android上的JavaME模拟器
在一个虚拟机A上运行另外一个虚拟机B看起来是挺不靠谱的一件事,在手机上运行某个虚拟机也不怎么靠谱.并且如果虚拟机A运行在手机上这个听起来就更不靠谱了.但是很多人就在做这样的事.比如在在手机上运行DOS ...
- plsql 导出查询结果
点击青色按钮即可 说明: 会将查询到的所有数据导出到指定文件,并不是只导出下面列表显示的几行数据: 也不用点击"获取最后页"那个按钮. 注意: 当你选择导出为excel文件时, ...
- [Android] 布局优化技巧
看了一些关于优化布局的资料,了解了很多平时不怎么注意的问题,于是把资料整理了一下,一部分内容是翻译来的,一部分是自己理解加上的.每部分内容都有demo,有些资料里的demo比较好的,我就直接拿来用了: ...
- Azure 中虚拟机的区域和可用性
Azure 在中国的两个数据中心运行. 这些数据中心分组到地理区域,让用户可灵活选择构建应用程序的位置. 请务必了解 Azure 中虚拟机 (VM) 运行的方式和位置,以及最大化性能.可用性和冗余的选 ...
- pt-osc全解pt-online-schema-change
MySQL 大字段的DDL操作:加减字段.索引.修改字段属性等,在5.1之前都是非常耗时耗力的,特别是会对MySQL服务产生影响.在5.1之后随着Plugin Innodb的出现在线加索引的提高了很多 ...