LeetCode之Easy篇 ——(7)Reverse Integer

7、Reverse Integer

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

Example 1:

Input: 123
Output:  321

Example 2:

Input: -123
Output: -321

Example 3:

Input: 120
Output: 21

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;
    }
}

原文地址:https://www.cnblogs.com/promiseslc/p/8585627.html

时间: 2024-08-14 18:09:17

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

LeetCode之Easy篇 ——(13)Roman to Integer

Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 思路分析: 1.熟悉罗马数字的规则.见LeetCode之Easy篇 --(12)Integer to Roman 2.将输入的罗马数字转化成数组,并逐一通过case比对,然后根据其规则进行运算. Java代码示例: class Solution { public int romanT

LeetCode之Easy篇 ——(12)Roman to Integer

Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. 罗马数字: 基本字符 I V X L C D M 相应的阿拉伯数字表示为 1 5 10 50 100 500 1000 1.相同的数字连写.所表示的数等于这些数字相加得到的数.如:Ⅲ=3: 2.小的数字在大的数字的右边.所表示的数等于这些数字相加得到的数. 如:Ⅷ=8.Ⅻ=12: 3

[LeetCode] Reverse Integer [8]

题目 Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have already thought th

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

题目:Reverse Integer 难度:Easy 题目内容: Given a 32-bit signed integer, reverse digits of an integer. 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, assum

[LeetCode][JavaScript]Reverse Integer

Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have alread

LeetCode:Reverse Integer - 翻转数字

1.题目名称 Reverse Integer(翻转数字) 2.题目地址 https://leetcode.com/problems/reverse-integer/ 3.题目内容 英文:Reverse digits of an integer. 中文:翻转一个正整数的各位,形成一个新数字 例如:x = 123, return 321:x = -123, return -321 4.一个有瑕疵的方法(不能AC) 一个比较好想到的方法,是先将输入的数字转换为字符串,再将字符串翻转后转换为数字.这个方

LeetCode 007 Reverse Integer

[题目] Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 [题意] 反转int型整数,输出的也是int型的整数 [思路] 如要考虑两种特殊情况: 1. 类似100这样的整数翻转之后为1 2. 翻转之后的值溢出该如何处理, 本题的测试用例中似乎没有给出溢出的情况 在实际面试时需要跟面试官明确这种情况的处理方法. 基于这点事实,本题规定如果超出正边界返回INT_MA

LeetCode 7 Reverse Integer(反转数字)

题目来源:https://leetcode.com/problems/reverse-integer/ Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 解题思路: 其实这道题看起来非常简单,要实现也是几行代码的事.但是有个小问题容易被忽略,就是边界问题.什么意思呢?如果我们输入的整数超出了int的表达范围,这个问题要怎么解决呢? 用比int更大的数据类型存储我们转

LeetCode——Reverse Integer

Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have already thought through this! If the integer'