【LeetCode算法】Reverse Integer

LeetCode第7题:

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 store integers within the 32-bit signed integer range: [?231,  231 ? 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

解题思路:

假如:x=1234,y=0

1)y乘以10,x把末位4移除掉,加给y(x=123,y=4)

2)y乘以10,x把末位3移除掉,加给y(x=12,y=43)

3)y乘以10,x把末位2移除掉,加给y(x=1,y=432)

4)y乘以10,x把末位1移除掉,加给y(x=0,y=4321)

代码:

int y = 0;
while(x/10!=0){
    y*=10;
    y+=x%10;
    x/=10;
}
y=y*10 +x;

结果报错

原因是int的范围是-2147483647~2147483648

leetcode给的input是1534236469,倒转之后是9646324351,导致溢出了。所以要加上溢出判断

最后的代码

class Solution {
    public int reverse(int x) {

        int negative = 1;
        if(x <= Integer.MIN_VALUE)
            return 0;
        if(x < 0){
            x = -x;
            negative = -1;
        }  

        long y = 0;
        while(x/10!=0){
            y*=10;
            y+=x%10;
            x/=10;
        }
        y=y*10 +x;

        if(y > Integer.MAX_VALUE)
            return 0;
        else
            return (int)y * negative;
    }
}

这也是很坑啊,溢出就输出0,鬼知道啊

原文地址:https://www.cnblogs.com/anni-qianqian/p/9057725.html

时间: 2024-10-08 00:15:55

【LeetCode算法】Reverse Integer的相关文章

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 数 Reverse Integer

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Reverse Integer Total Accepted: 17472 Total Submissions: 43938 Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to show spoilers. Have you thought

【LeetCode】Reverse Integer (2 solutions)

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][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 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(7): Reverse Integer 源码实现 runtime: 8ms

题目 :Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 题目分析及部分代码解析: 1.需要考虑一位数,比如1,2,3等特殊情况,返回本身. 2.需要考虑0,返回0. 3.需要考虑如123000,45600等末尾有若干零的情况,正确结果应为321.654,不应该出现000321,00654等情况. 4.需要4字节int类型数据的取值范

Leetcode 7. Reverse Integer(水)

7. Reverse Integer Easy 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 on

[LeetCode][Algorithms]Reverse Integer

# -*- coding: utf8 -*-'''__author__ = '[email protected]'https://oj.leetcode.com/problems/reverse-integer/ Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Have you thought about this?Here are some good quest

[LeetCode] 007. Reverse Integer (Easy) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 007.Reverse_Integer (Easy) 链接: 题目:https://oj.leetcode.com/problems/Reverse-Integer/ 代码(github):https://github.com/illuz/leetcode 题意: 反转一个数. 分析: 注意读入和返回的数都是 in