题目描述:

解题思路:

  反转的方法很简单,重点在于判断溢出的问题,下面给出了两种方法。

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★的更多相关文章

  1. LeetCode 【2】 Reverse Integer --007

    六月箴言 万物之中,希望最美:最美之物,永不凋零.—— 斯蒂芬·金 第二周算法记录 007 -- Reverse Integer (整数反转) 题干英文版: Given a 32-bit signed ...

  2. 【leetcode】Reverse Integer(middle)☆

    Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 总结:处理整数溢出 ...

  3. 【leetcode】Reverse Integer

    题目描述: Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 很简单 ...

  4. 【Leetcode】【Easy】Reverse Integer

    Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Have you ...

  5. 【Leetcode-easy】Reverse Integer

    思路:取绝对值,反转,并判断反转的结果是否大于最大整数,需要注意的细节:判断时需要这样:result > (Integer.MAX_VALUE - v) / 10 否则result * 10 + ...

  6. 【LeetCode】Reverse Integer(整数反转)

    这道题是LeetCode里的第7道题. 题目描述: 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321  示例 2: 输入: -123 ...

  7. 【LeetCode】397. Integer Replacement 解题报告(Python)

    [LeetCode]397. Integer Replacement 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/inte ...

  8. LeetCode【7】.Reverse Integer--java实现

    Reverse Integer 题目要求:给定一个int 类型值,求值的反转,例如以下: Example1: x = 123, return 321      Example2: x = -123, ...

  9. 【LeetCode算法-7】Reverse Integer

    LeetCode第7题: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Outp ...

随机推荐

  1. 【代码笔记】iOS-UILabel根据内容自动调整高度

    一,效果图. 二,代码. - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the ...

  2. 登录PeopleTools 提示ora-00942表视图不存在 select xxx from sysadm.psoprdefn

    起因:本来跟DBA说了把生产的库同步到CFG环境,还跟她说了,dev tst cfg在一台机器上,结果她还是把dev给覆盖了,幸好及时发现,一部分对象被删除了(序列,视图,有可能也有表). 视图和一部 ...

  3. 使用Picasso将加载的图片变成圆形

    http://blog.it985.com/14794.html,感谢该作者 Picasso的GITHUB地址:https://github.com/square/picasso. 怎么实现各种各样的 ...

  4. 第一篇 Windows docker 概述

    本人行业属于智能制造,偏向工厂应用,客户端程序全部是.Net 的 WinForm:本系统的后台是.Net,多系统交互的有java的:因系统发布效率问题,想采用docker Windows 的生产力环境 ...

  5. HTML 5 Web Workers

    什么是Web Worker? web worker 是运行在后台的 JavaScript,不会影响页面的性能. Web Worker有什么用? JavaScript语言采用的是单线程模型,也就是说,所 ...

  6. LeetNode 题解之Reverse Nodes in k-Group

    1.题目描述 2.问题分析 这个题本质上还是按照链表翻转的思路来解,只是需要添加一些细节判断. 3.代码 class Solution { public: ListNode* reverseKGrou ...

  7. innodb 表锁和行锁

    表锁  表锁相关结构: table->locks:数据字典table保存这个表上的所有表锁信息 trx->lock.table_locks:每个事务trx保存该事务所加的所有表锁信息 tr ...

  8. Oracle EBS OPM reshedule batch

    --reschedule_batch --created by jenrry DECLARE x_message_count NUMBER; x_message_list VARCHAR2 (2000 ...

  9. Oracle EBS AR 收款取数

    -- 收款核销,贷项通知单核销也是通过ar_receivable_applications_all表 SELECT cr.receipt_number ,ad.amount_dr ,ad.amount ...

  10. 调用百度翻译API接口功能

    public string appid = "自己的APPID"; public string q = "要翻译的文本"; "; public str ...