题目:整数反转

题目来源:https://leetcode-cn.com/problems/reverse-integer

给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转。

示例 1:

输入: 123
输出: 321

示例 2:

输入: -123
输出: -321

示例 3:

输入: 120
输出: 21

注意:

假设我们的环境只能存储得下 32 位的有符号整数,则其数值范围为 [−2^31,  2^31 − 1]。请根据这个假设,如果反转后整数溢出那么就返回 0。

解题思路

题目中对数字反转的结果有最大最小限制,这个限制实际上在 Java 中也是 int 类型的大小限制,把 [−2^31,  2^31 − 1] 这个公式计算一下,结果就是 [-2147483648, 2147483647] 。

接下来的就只剩下一个问题了,如何把一个整数进行反转。

我这里提供一个思路,我们对原数字除以 10 以后进行取模运算(取余数):

大体思路就是上面这样,然后注意边缘值判断,代码基本上就按照这个思路来:

public int reverse(int x) {
int res = 0;
while (x != 0) {
// 先获取末尾数字
int tmp = x % 10; if (res < -214748364 || (res == -214748364 && tmp < -8)) {
return 0;
} if (res > 214748364 || (res == 214748364 && tmp > 7)) {
return 0;
} res = res * 10 + tmp;
x /= 10;
}
return res;
}

这种边界判断方式稍显笨重,我看了看别人的答案,看到一种溢出的判断思路,感觉不错分享下:

public int reverse_1(int x) {
int res = 0;
while (x != 0) { if (res > 214748364 || res < -214748364) {
return 0;
} res = res * 10 + x % 10;
x /= 10;
}
return res;
}

这个结果提交 LeetCode 以后,直接看到 LeetCode 说执行耗时超过 100% 的用户。

简直意外的惊喜,这里的判断极限值的含义如下: 1463847412

极限最大值是 2147483648 ,除以 10 以后是 214748364 ,这里当 res 是 214748364 时,输入的 x 只能是 1463847412 ,因为 2463847412 、 3463847412 这些数字本身已经 int 溢出了。

思路非常巧妙,利用了 int 本身的溢出范围,限制了输入数据的大小,减少了需要判断的可能性。

每日一道 LeetCode (2):整数反转的更多相关文章

  1. 每日一道 LeetCode (3):回文数

    前文合集 每日一道 LeetCode 文章合集 题目:回文数 题目来源:https://leetcode-cn.com/problems/palindrome-number/ 判断一个整数是否是回文数 ...

  2. 每日一道 LeetCode (14):数组加一

    每天 3 分钟,走上算法的逆袭之路. 前文合集 每日一道 LeetCode 前文合集 代码仓库 GitHub: https://github.com/meteor1993/LeetCode Gitee ...

  3. 每日一道 LeetCode (15):二进制求和

    每天 3 分钟,走上算法的逆袭之路. 前文合集 每日一道 LeetCode 前文合集 代码仓库 GitHub: https://github.com/meteor1993/LeetCode Gitee ...

  4. 每日一道 LeetCode (19):合并两个有序数组

    每天 3 分钟,走上算法的逆袭之路. 前文合集 每日一道 LeetCode 前文合集 代码仓库 GitHub: https://github.com/meteor1993/LeetCode Gitee ...

  5. 每日一道 LeetCode (41):阶乘后的零

    每天 3 分钟,走上算法的逆袭之路. 前文合集 每日一道 LeetCode 前文合集 代码仓库 GitHub: https://github.com/meteor1993/LeetCode Gitee ...

  6. 前端与算法 leetcode 7. 整数反转

    目录 # 前端与算法 leetcode 7. 整数反转 题目描述 概要 提示 解析 解法 算法 传入测试用例的运行结果 执行结果 GitHub仓库 # 前端与算法 leetcode 7. 整数反转 题 ...

  7. 每日一道 LeetCode (5):最长公共前缀

    前文合集 每日一道 LeetCode 前文合集 代码仓库 GitHub: https://github.com/meteor1993/LeetCode Gitee: https://gitee.com ...

  8. 每日一道 LeetCode (6):有效的括号

    每天 3 分钟,走上算法的逆袭之路. 前文合集 每日一道 LeetCode 前文合集 代码仓库 GitHub: https://github.com/meteor1993/LeetCode Gitee ...

  9. 每日一道 LeetCode (8):删除排序数组中的重复项和移除元素

    每天 3 分钟,走上算法的逆袭之路. 前文合集 每日一道 LeetCode 前文合集 代码仓库 GitHub: https://github.com/meteor1993/LeetCode Gitee ...

  10. 每日一道 LeetCode (9):实现 strStr()

    每天 3 分钟,走上算法的逆袭之路. 前文合集 每日一道 LeetCode 前文合集 代码仓库 GitHub: https://github.com/meteor1993/LeetCode Gitee ...

随机推荐

  1. web前端开发_文件/目录/样式/函数等命名规范

    页面的命名规则 统一用翻译的英文命名(推荐) 统一用拼音命名(拼音的简化也可) 如果文件名过长,企业要提前约定一份缩写的规范,如pro—product 例如: 首页—index 产品列表—prolis ...

  2. 从零开始学Electron笔记(三)

    在之前的文章我们介绍了一下Electron的菜单创建和事件绑定,其中提到了一个remote模块,接下来我们继续说一下Electron的这个remote模块. 官方关于remote模块的地址:https ...

  3. 如何让元素支持 height:100%效果

    如何让元素支持 height:100%效果? 有两种方法.(1) 设定显式的高度值. 这个没什么好说的,例如,设置 height:600px,或者可以生效的百分比值高度.例如,我们比较常见的:html ...

  4. web 部署专题(九):Nginx 前后端分离中csrf_token 认证的实现

    1. 思路 参考:https://stackoverflow.com/questions/20826201/simple-csrf-protection-using-nginx-alone?r=Sea ...

  5. 数据分析06 /pandas高级操作相关案例:人口案例分析、2012美国大选献金项目数据分析

    数据分析06 /pandas高级操作相关案例:人口案例分析.2012美国大选献金项目数据分析 目录 数据分析06 /pandas高级操作相关案例:人口案例分析.2012美国大选献金项目数据分析 1. ...

  6. Linux03 /shell命令

    Linux03 /shell命令 目录 Linux03 /shell命令 1. xshell快捷键 2. 修改linux的命令提示符,通过变量PS1控制 3. yum安装/卸载 4. cat命令 5. ...

  7. Web For Pentester靶场(xss部分)

    配置 官网:https://pentesterlab.com/ 下载地址:https://isos.pentesterlab.com/web_for_pentester_i386.iso 安装方法:虚 ...

  8. C# 人脸识别库 0.2

    ViewFaceCore 0.2 超简单的 C# 人脸识别库 前言: 首先谢谢大家对这个库的关注,前一篇博文得到了大家的 支持 和 Star,十分开心.本想尽快实现大家的期待的活体检测功能,但是前段时 ...

  9. OSCP Learning Notes - Post Exploitation(3)

    Post-Exploit Password Attacks 1. Crack using the tool - john (Too slow in real world) Locate the roc ...

  10. day10 python之函数的参数

    函数的基本属性 1.1 函数的含义 # 1.功能 :包裹代码,实现功能,达到目的 # 2.特点 :反复调用,提高开发效率,便于代码维护 1.1.2 函数的基本格式 # 函数名 :变量命名规则 # 函数 ...