LeetCode之Easy篇 ——(7)Reverse Integer
7、Reverse Integer
Given a 32-bit signed integer, reverse digits of an integer.
Example 1:
Input:
Output:
Example 2:
Input: -
Output: -
Example 3:
Input:
Output:
Note:
Assume we are dealing with an environment which could only hold integers within the 32-bit signed integer range. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
思路:
1、是否考虑正负号?
通过调用java.lang.Math类中取绝对值的abs方法,四种情况:
static double abs(double a);//返回 double 值的绝对值。
static float abs(float a);//返回 float 值的绝对值。
static int abs(int a);//返回 int 值的绝对值。
static long abs(long a);//返回 long 值的绝对值。
2、32位int型数据范围?
最小值:Integer.MIN_VALUE:-2147483648
最大值:Integer.MAX_VALUE:2147483647
3、代码第6行为什么只需跟214748364(记为num)比较?
首先,只有当输入值为十位数,末尾数字记为a(第一位必定是1或者2,因为输入值也必须在最大值与最小值之间,否则报错),res跟num才显得有意义,尽管之前也一直在比较。
为了方便叙述,下面的res都是指最后一次跟 num比较的情况。
a为0时,res是8位数,肯定小于num。最后输出9位数,肯定不会越界。
a为1时,res是以1开头的9位数,肯定小于num。最后输出10位数,也不会越界。
a为2时,res是以24开头的9位数,肯定大于num,不必进行也不能进行下一次赋值,因为再加一位肯定超过范围,而且也没有跟num比较,return 0的机会了,因为此时x的值为0,直接跳出循环,return res,结果肯定报错,因为超过最大值。
a大于2时,res肯定大于num,直接return 0即可,理由同上。
4、怎么表示输出值?
res = res * 10 + x % 10;完美解决了输入值尾数为0的情况。
Java代码如下:
import static java.lang.Math.abs;
class Solution {
public int reverse(int x) {
int res = 0;
while(x != 0){
if(abs(res) > Integer.MAX_VALUE / 10) return 0;
res = res * 10 + x % 10;
x /= 10;
}
return res;
}
}
LeetCode之Easy篇 ——(7)Reverse Integer的更多相关文章
- 【LeetCode每天一题】Reverse Integer(反转数字)
Given a 32-bit signed integer, reverse digits of an integer. Example 1: ...
- 【leetcode刷题笔记】Reverse Integer
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 解题:设定一个变量 ...
- LeetCode之Easy篇 ——(1)Two Sum
1.Two Sum Given an array of integers, return indices of the two numbers such that they add up to a s ...
- LeetCode 7. 反转整数(Reverse Integer)
题目描述 给定一个 32 位有符号整数,将整数中的数字进行反转. 示例 1: 输入: 123 输出: 321 示例 2: 输入: -123 输出: -321 示例 3: 输入: 120 输出: 21 ...
- C# 写 LeetCode easy #7 Reverse Integer
7.Reverse Integer Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 ...
- Leetcode解题思路总结(Easy篇)
终于刷完了leetcode的前250道题的easy篇.好吧,其实也就60多道题,但是其中的套路还是值得被记录的. 至于全部code,请移步github,题目大部分采用python3,小部分使用C,如有 ...
- LeetCode 7 Reverse Integer(反转数字)
题目来源:https://leetcode.com/problems/reverse-integer/ Reverse digits of an integer. Example1: x = 123, ...
- LeetCode第[7]题(Java):Reverse Integer 标签:数学
题目:Reverse Integer 难度:Easy 题目内容: Given a 32-bit signed integer, reverse digits of an integer. Note:A ...
- LeetCode 【2】 Reverse Integer --007
六月箴言 万物之中,希望最美:最美之物,永不凋零.—— 斯蒂芬·金 第二周算法记录 007 -- Reverse Integer (整数反转) 题干英文版: Given a 32-bit signed ...
随机推荐
- python并发编程之多进程(三):共享数据&进程池
一,共享数据 展望未来,基于消息传递的并发编程是大势所趋 即便是使用线程,推荐做法也是将程序设计为大量独立的线程集合 通过消息队列交换数据.这样极大地减少了对使用锁定和其他同步手段的需求, 还可以扩展 ...
- [翻译] 编写高性能 .NET 代码--第二章 GC -- 减少大对象堆的碎片,在某些情况下强制执行完整GC,按需压缩大对象堆,在GC前收到消息通知,使用弱引用缓存对象
减少大对象堆的碎片 如果不能完全避免大对象堆的分配,则要尽量避免碎片化. 对于LOH不小心就会有无限增长,但LOH使用的空闲列表机制可以减轻增长的影响.利用这个空闲列表,我们可以在两块分配区域中间找到 ...
- angular4升级angular5问题记录之No NgModule metadata found for 'AppModule'
在将项目从angular4升级到angular5的过程中,出现No NgModule metadata found for 'AppModule'问题,网上查找答案将app.module.ts进行再次 ...
- Spring-Security+Freemarker 开启跨域请求伪造防护功能
CSRF简介--摘抄自<Spring实战(第4版)> 我们可以回忆一下,当一个POST请求提交到"/spittles"上时,SpittleController ...
- Eclipse 安装 SVN 插件的两种方法
eclipse里安装SVN插件,一般来说,有两种方式: 直接下载SVN插件,将其解压到eclipse的对应目录里 使用eclipse 里Help菜单的“Install New Software”,通过 ...
- Node.js,commonjs,require
环境: Node应用由模块组成,采用CommonJS模块规范. node的全局对象是global,没有window这个对象. process表示当前执行的进程,挂在global之下. CommonJS ...
- SpringBoot SpringSecurity4整合,灵活权限配置,弃用注解方式.
SpringSecurity 可以使用注解对方法进行细颗粒权限控制,但是很不灵活,必须在编码期间,就已经写死权限 其实关于SpringSecurity,大部分类都不需要重写,需要的只是妥善的配置. 每 ...
- shell脚本基础 循环机构
循环结构 for循环格式一格式:for 变量 in 值1 值2 ........(值不一定是数字,可以是命令或者其他的)do 命令done [root@ceshiji ~]# vim a.sh #!/ ...
- 阿里舆情︱舆情热词分析架构简述(Demo学习)
本节来源于阿里云栖社区,同时正在开发一个舆情平台,其中他们发布了一篇他们所做的分析流程,感觉可以作为案例来学习.文章来源:觉民cloud/云栖社区 平台试用链接:https://prophet.dat ...
- android技术晋升之道
写一篇文章记录一下我看到的几个特别常见的误区,希望对团队晋升的同学能有帮助. 误区1:把特质当成案例 工作非常努力,连续一个月加班到12点,解决了问题 喜欢学习新技术和分享,团队同学都很喜欢 善于钻研 ...