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 ...
随机推荐
- Dynamics 365 Online-多选域
参与过Dynamics CRM相关工作的朋友们都知道,Dynamics 365之前并没有多选域字段,想要实现多选域,需要自己添加WebResource定制,而这也带来了一系列需要考虑的情况,比如额外的 ...
- [记录]Python2.7使用argparse模块
# -*- coding: utf8 -*- import argparse #ArgumentParser.add_argument(name or flags-[, action][, nargs ...
- 在centos 6.8下安装docker
1.检查自己的系统内核是不是64位系统,因为docker只能安装在64位系统中 命令: uname -a 结果 2.6.32-642.6.2.el6.x86_64 2.查看自己centos的版本 ca ...
- js中boolean类型的理解
<html> <head> <script type="text/javascript"> var x="12"; aler ...
- yii2自带的backend,frontend不够用,添加一个后台模块怎么做?
在复杂项目里,高级模板中的fontend.backend application明显不够,可以再添加另外的application. 例如添加一个seller application .步骤如下: 1, ...
- 聚类-K均值
数据来源:http://archive.ics.uci.edu/ml/datasets/seeds 15.26 14.84 0.871 5.763 3.312 2.221 5.22 Kama 14.8 ...
- MongoDB,分组,聚合
使用聚合,db.集合名.aggregate- 而不是find 管道在Unix和Linux中一般用于将当前命令的输出结果作为下一个命令的参数.MongoDB的聚合管道将MongoDB文档在一个管道处理完 ...
- 通讯服务类API调用的代码示例合集:短信服务、手机号归属地查询、电信基站查询等
以下示例代码适用于 www.apishop.net 网站下的API,使用本文提及的接口调用代码示例前,您需要先申请相应的API服务. 短信服务:通知类和验证码短信,全国三网合一通道,5秒内到达,费用低 ...
- 弄懂 JRE、JDK、JVM 之间的区别与联系
其实很多 Java 程序员在写了很多代码后,你问他 jre 和 jdk 之间有什么关系,jvm 又是什么东西,很多人不知所云.本篇不会讲述 jvm 底层是如何与不同的系统进行交互的,而主要理清楚三者之 ...
- R︱Rstudio 1.0版本尝鲜(R notebook、下载链接、sparkR、代码时间测试profile)
每每以为攀得众山小,可.每每又切实来到起点,大牛们,缓缓脚步来俺笔记葩分享一下吧,please~ --------------------------- 2016年11月1日,RStudio 1.0版 ...