【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 ...
随机推荐
- AJAX的基本操作
AJAX即“Asynchronous Javascript And XML”(异步JavaScript和XML),是指一种创建交互式网页应用的网页开发技术. AJAX = 异步 JavaScript和 ...
- MediaPlayer音乐播放器、上一首、下一首、播放、停止、自动下一首、进度条
本文介绍MediaPlayer的使用.MediaPlayer可以播放音频和视频,另外也可以通过VideoView来播放视频,虽然VideoView比MediaPlayer简单易用,但定制性不如用Med ...
- LeetNode 题解之Reverse Nodes in k-Group
1.题目描述 2.问题分析 这个题本质上还是按照链表翻转的思路来解,只是需要添加一些细节判断. 3.代码 class Solution { public: ListNode* reverseKGrou ...
- 使用 PowerShell 将数据磁盘附加到 Windows VM
本文介绍如何使用 PowerShell 将新磁盘和现有磁盘附加到 Windows 虚拟机. 在开始之前,请查看以下提示: 虚拟机的大小决定了可以附加多少个磁盘. 有关详细信息,请参阅虚拟机大小. 若要 ...
- Java计算大整数
import java.util.*; import java.math.*; //BigInteger类型在这个包里 public class Gcc_test { public static vo ...
- Shell 脚本合集
0. 说明 Shell 脚本合集 1. xcall.sh xcall.sh 编写为了为了同时对多台服务器进行操作,编写完成之后,将其发送到 /usr/local/bin 下 #!/bin/bash ...
- VS网站开发的发布部署的不同情况说明
VS网站开发有两种模式: 1.网站模式 2.应用模式 其中,网站模式的发布,要考虑勾选“使用固定命名和单页程序集” 如下图 网站模式: 新建网站的网站模式 新建网站的网站模式第二步 应 ...
- MySQL基础之---mysqlimport工具和LOAD DATA命令导入文本文件
1.mysqlimport工具的使用 看一下命令的使用方法: shell > mysqlimport -u root -p [--LOCAL] DBname File [option] --f ...
- sql注入--access
access数据库结构: 表名 --> 列名 --> 数据 access注入攻击片段 联合查询法: (1) 判断注入点: ?id=1 and 1=1 ; ?id=1 and 1 ...
- 面向对象程序设计__Task6_Calculator1.6.2
The 4th part of the Calculator program _ Interface 题目链接:第六次作业(计算器第四步) github链接:Calculator_1.6.2 第六次作 ...