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 ...
随机推荐
- maven The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path错误
对于这个问题的话,请在pom文件中加入 <dependency> <groupId>javax.servlet</groupId> <artifactId&g ...
- Activiti中的log4j(slf4j)的配置
今天试了一下在Activiti中使用log4j来进行配置发现这个会出现问题,其实Activiti中的日志系统是采用的是slf4j而不是log4j 然后使用slf4j驱动log4j来做的 通过Proce ...
- 报错信息 The jsp:param action must not be used outside the jsp:include, jsp:forward, or jsp:params elements 的原因及解决办法
如果你的代码是这样的话就会报错 <jsp:forward page="02.jsp"></jsp:forward> <jsp:param value= ...
- Angular @HostBinding()和@HostListener()用法
@HostBinding()和@HostListener()在自定义指令时非常有用.@HostBinding()可以为指令的宿主元素添加类.样式.属性等,而@HostListener()可以监听宿主元 ...
- 如何解决使用Gradle时出现的jar包冲突
前言 在我之前使用Gradle的博文中已经提到,Gradle对依赖的管理是比较智能的,如果有两个包依赖于相同的包,而版本不同的时候,Gradle会进行自动的选择,从而避免jar包的冲突. 也就是说,在 ...
- SpringBoot SpringSecurity4整合,灵活权限配置,弃用注解方式.
SpringSecurity 可以使用注解对方法进行细颗粒权限控制,但是很不灵活,必须在编码期间,就已经写死权限 其实关于SpringSecurity,大部分类都不需要重写,需要的只是妥善的配置. 每 ...
- ACL访问控制列表
acl是基于文件系统的,所以支不支持acl在于使用什么文件系统. FAT32文件系统不支持权限,也不区分大小写 如果一个分区不是安装系统时分的分区,是一个新的分区的话,默认是不支持acl CentOS ...
- Json序列化、反序列化
引用 using Newtonsoft.Json; using Newtonsoft.Json.Converters; 把Json字符串反序列化为对象 1.目标对象 = JavaScriptConve ...
- CSRF的本质及防御
本质:产生的原因本质上是参数可知或可预测 防御: 1.加密参数:加密加盐,不可知,不可预测 忧虑,引入其他麻烦:一.数据分析困难 ...
- linux系统/sbin/init执行过程
对于Linux的启动过程,之前一直都是研究到内核运行/sbin/init,启动第一个用户进程为止,因为这部分一直都是在内核态工作,所以对于学习内核还是有帮助的,当时/sbin/init之后的过程也需要 ...