7、Reverse Integer

Given a 32-bit signed integer, reverse digits of an integer.

Example 1:

  1. Input:
  2. Output:

Example 2:

  1. Input: -
  2. Output: -

Example 3:

  1. Input:
  2. 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方法,四种情况:

  1. static double abs(double a);//返回 double 值的绝对值。
  2. static float abs(float a);//返回 float 值的绝对值。
  3. static int abs(int a);//返回 int 值的绝对值。
  4. 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代码如下:

  1. import static java.lang.Math.abs;
  2. class Solution {
  3. public int reverse(int x) {
  4. int res = 0;
  5. while(x != 0){
  6. if(abs(res) > Integer.MAX_VALUE / 10) return 0;
  7. res = res * 10 + x % 10;
  8. x /= 10;
  9. }
  10. return res;
  11. }
  12. }

LeetCode之Easy篇 ——(7)Reverse Integer的更多相关文章

  1. 【LeetCode每天一题】Reverse Integer(反转数字)

    Given a 32-bit signed integer, reverse digits of an integer. Example 1:                              ...

  2. 【leetcode刷题笔记】Reverse Integer

    Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 解题:设定一个变量 ...

  3. 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 ...

  4. LeetCode 7. 反转整数(Reverse Integer)

    题目描述 给定一个 32 位有符号整数,将整数中的数字进行反转. 示例 1: 输入: 123 输出: 321  示例 2: 输入: -123 输出: -321 示例 3: 输入: 120 输出: 21 ...

  5. C# 写 LeetCode easy #7 Reverse Integer

    7.Reverse Integer Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 ...

  6. Leetcode解题思路总结(Easy篇)

    终于刷完了leetcode的前250道题的easy篇.好吧,其实也就60多道题,但是其中的套路还是值得被记录的. 至于全部code,请移步github,题目大部分采用python3,小部分使用C,如有 ...

  7. LeetCode 7 Reverse Integer(反转数字)

    题目来源:https://leetcode.com/problems/reverse-integer/ Reverse digits of an integer. Example1: x = 123, ...

  8. LeetCode第[7]题(Java):Reverse Integer 标签:数学

    题目:Reverse Integer 难度:Easy 题目内容: Given a 32-bit signed integer, reverse digits of an integer. Note:A ...

  9. LeetCode 【2】 Reverse Integer --007

    六月箴言 万物之中,希望最美:最美之物,永不凋零.—— 斯蒂芬·金 第二周算法记录 007 -- Reverse Integer (整数反转) 题干英文版: Given a 32-bit signed ...

随机推荐

  1. Linux系统软件安装的几种方式

    Linux系统,一个文件能不能执行看的是有没有可执行权限x,不过真正的可执行文件是二进制文件(binary file),举例来说Linux上的c语言源码编写完后,通过gcc程序编译后就可以创建一个可执 ...

  2. 深入js正则

    开题 我们常常有正则的各种需求,普通的正则匹配符虽然够用,但是满足不了我们一些很奇怪的需求,所以我们需要更多的关于正则的知识点. 需求 比如我们有一个这样的需求,匹配出字符串里的第一个div标签:aa ...

  3. TzObjectInspector 一例

    TzObjectInspector Github上的一个开源组件!可以做到类似Delphi IDE属性,事件面板的样式!作者持续更新中... 看起来是这个样子: 这个东西用起来并不像想象的那样可以直接 ...

  4. React——diff算法

    react的diff算法基于两个假设: 1.不同类型的元素会产生不同的树 2.通过设置key,开发者能够提示那些子组件是稳定的 diff算法 当比较两个树时,react首先会比较两个根节点,接下来具体 ...

  5. shell脚本中 杀死可能成为僵尸进程的方法

    交互式 Bash Shell 获取进程 pid 在已知进程名(name)的前提下,交互式 Shell 获取进程 pid 有很多种方法,典型的通过 grep 获取 pid 的方法为(这里添加 -v gr ...

  6. 【视频编解码·学习笔记】8. 熵编码算法:基本算法列举 & 指数哥伦布编码

    一.H.264中的熵编码基本方法: 熵编码具有消除数据之间统计冗余的功能,在编码端作为最后一道工序,将语法元素写入输出码流 熵解码作为解码过程的第一步,将码流解析出语法元素供后续步骤重建图像使用 在H ...

  7. 将vue的项目打包后通过百度的BAE发布到网上的流程

    经过两天的研究终于将VUE打包后的项目通过BAE发布到了网上.虽然接口方面还有一下问题但是自己还是很高兴的. 首先说一下这个项目需要用到的技术,vue+express+git+百度的应用引擎BAE. ...

  8. Hadoop 错误归档库

    在hive中操作任意mapreduce相关语句 The size of Container logs revealed the below error: 2015-04-24 11:41:41,858 ...

  9. CSS3的[att$=val]选择器

    1.实例源码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www. ...

  10. SQL面试题收录

    1.用一条SQL语句 查询出每门课都大于80分的学生姓名 SELECT DISTINCT NAME FROM t_score_tab WHERE NAME NOT IN (SELECT DISTINC ...