【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 ...
随机推荐
- windows与虚拟机的linux共享一个文件夹
1.安装VMware Tools,在VMware面板上选择“虚拟机-重新安装VMware tools…” 2.使用命令 Ctrl+Alt+T 打开终端: 3.切换用户到root,命令 su 回车,输入 ...
- Python Python-MySQLdb中的DictCursor使用方法简介
Python-MySQLdb中的DictCursor使用方法简介 by:授客 QQ:1033553122 DictCursor的这个功能是继承于CursorDictRowsMixIn,这个Mi ...
- jsp 发布war 包到Tomcat
1.将项目打包成war,打包过程这里不做赘述 2.在linux或者windows下安装xmapp 3.打开Tomcat下conf/server.xml,在host下添加一行 <Co ...
- iOS手机流量抓包rvictl
移动设备抓包主要方式 一.PC上设置网络共享,生成Wi-Fi热点供移动设备使用,PC上再使用tcpdump.Wireshark等捕获分析: 二.PC上开启http代理工具服务器(如Charles.fi ...
- python生成式和生成器
一,生成式和生成器 1,列表生成式 mylist = [ i*i for i in range(3) if i>1 ] print(mylist) 运行结果:[4] 可以发现,快速生成列表的表达 ...
- etc/skel目录介绍
/etc/skel目录的作用: /etc/skel目录是用来存放新用户配置文件的目录,当我们添加新用户时,这个目录下的所有文件会自动被复制到新添加的用户家目录下,默认情况下,/etc/skel 目录下 ...
- 【转】Java学习---深入理解线程池
[原文]https://www.toutiao.com/i6566022142666736131/ 我们使用线程的时候就去创建一个线程,这样实现起来非常简便,但是就会有一个问题: 如果并发的线程数量很 ...
- Spring线程池配置模板设计(基于Springboot)
目录 线程池配置模板 基础的注解解释 常用配置参数 配置类设计 线程池使用 ThreadPoolTaskExecutor源码 线程池配置模板 springboot给我们提供了一个线程池的实现,它的底层 ...
- Stop Bitbucket prompting for password in git
出处:http://qosys.info/485/bitbucket-git-prompt-for-password In some cases after adding public ssh key ...
- PyQt5---firstwindow
# -*- coding:utf-8 -*- ''' Created on Sep 13, 2018 @author: SaShuangYiBing ''' import sys from PyQt5 ...