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 ...
随机推荐
- java-redis列表数据操作示例(二)
接上篇博文<java-redis字符类数据操作示例(一)>,redis连接管理类的代码请跳转查看. 一.列表类型缓存测试类 public class ListTest { /** * 主测 ...
- Windows系统上Redis的安装
Redis 安装 Window 下安装 下载地址:https://github.com/MSOpenTech/redis/releases. Redis 支持 32 位和 64 位.这个需要根据你系统 ...
- PHP结合Redis来限制用户或者IP某个时间段内访问的次数
$redis = new Redis(); $redis->connect('127.0.0.1', 6379); //获取客户端真实ip地址 function get_real_ip(){ s ...
- PHP封装的一个单例模式Mysql操作类
掌握满足单例模式的必要条件----三私一公. ①私有的构造方法-为了防止在类外使用new关键字实例化对象. ②私有的成员属性-为了防止在类外引入这个存放对象的属性. ③私有的克隆方法-为了防止在类外通 ...
- Linux sftp 安全文件传输命令
sftp 是一个交互式文件传输程式.它类似于 ftp, 但它进行加密传输,比FTP有更高的安全性. 1.常用登陆方式: 格式:sftp <user>@<host> 通过sftp ...
- Linux的软链接和硬链接
1.Linux链接概念Linux链接分两种,一种被称为硬链接(Hard Link),另一种被称为符号链接(Symbolic Link).默认情况下,ln命令产生硬链接. [硬连接]硬连接指通过索引节点 ...
- 最简化搭建yum仓库
在使用rpm安装软件包时,时常会遇到一些软件依赖性问题,如果是简单的一两个依赖性还是可以手动解决.要是出现大量的依赖性问题的话会让增大工作量.yum是一个很好的前端程序,可解决软件包相关依赖性,可在多 ...
- linux Cacti监控服务器搭建
搭建Cacti监控服务器 部署安装环境(lamp) [root@zhuji1 ~]# yum -y install httpd [root@zhuji1 ~]# yum -y install php ...
- 创建基于MailKit和MimeKit的.NET基础邮件服务
邮件服务是一般的系统都会拥有和需要的功能,但是对于.NET项目来说,邮件服务的创建和使用会较为的麻烦..NET对于邮件功能提供了System.Net.Mail用于创建邮件服务,该基础服务提供邮件的基础 ...
- 2_Add Two Numbers --LeetCode
原题如下: 思路:在一个while中遍历两个链表,直到最长的链表为空,或者没有进位.每一步获取两个链表对应的结点的值a,b,然后相加a+b.如果上一步又进位,那就加a+b+1,若由于进位加1后还产生进 ...