LeetCode记录之——Reverse Integer

Reverse digits of an integer.

Example1: x = 123, return 321
Example2: x = -123, return -321

Note:
The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows.



反转数字的整数。

示例1:x = 123,返回321
示例2:x = -123,返回-321

注意:

假定输入为32位有符号整数。 当反转的整数溢出时,你的函数应该返回0。



  做LeetCode我个人最大的缺点就是上来就干,因为英语毕竟没有中文看着方便,导致好多提示根本没有注意。如本题溢出时返回0。


class Solution {
    public int reverse(int x) {
      boolean isAbove=true;
        StringBuffer sBuffer=new StringBuffer();
        int newX=x;
        if (x < 0) {
            isAbove = false;
            x = Math.abs(x);
        }
        while (x > 0) {
            sBuffer.append(x % 10);
            x = x / 10;
        }
        if (newX!=0) {
            try {
                if (isAbove)
                    return Integer.parseInt(sBuffer.toString());
                else
                    return -Integer.parseInt(sBuffer.toString());
            } catch (Exception e) {
                return 0;
            }

        }
        else
            return 0;
    }
}

  顺道来复习下JAVA基本数据类型的数值范围:

byte的取值范围为-128~127,占用1个字节(-2的7次方到2的7次方-1) 
short的取值范围为-32768~32767,占用2个字节(-2的15次方到2的15次方-1) 
int的取值范围为(-2147483648~2147483647),占用4个字节(-2的31次方到2的31次方-1) 
long的取值范围为(-9223372036854774808~9223372036854774807),占用8个字节(-2的63次方到2的63次方-1)

  

时间: 2024-08-27 11:26:26

LeetCode记录之——Reverse Integer的相关文章

Leetcode练习题 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 o

[LeetCode OJ]7. Reverse Integer

Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 个人的大体思路: 先判断是整数还是负数,flag记录符号(-1或1),把数字取绝对值然后转换成字符串,字符串逆序,把字符串再转换成数字,再乘以flag添加符号.这样就解决了数字末尾是0,翻转过来0成为开头,还要想办法去掉开头0的问题.代码如下 1 int reverse(int x) { 2 char s[100],

【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 int

LeetCode题解 #7 Reverse Integer

上一次做完一个中等题觉得还挺简单,做一下简单题看看. 题目大意:给定一个整型(即int),将它的数位反过来,如321变为123,-321变为-123. 做了很久,因为简单题耗时的地方在于恶心!! 最后才看见溢出的时候应该返回0!! 一点都不难,特别是对于java. 我的思路: 1.将读入的int型包装成Integer,然后在转换成String.(看看前面有没有+-号) 2.将String 倒置,再转换为long.(一定要是long,否则无法判断溢出) 3.如果没溢出,再将long转换为int.

LeetCode题目1 - Reverse Integer

Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321Discuss: 1.If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100.   2.Reversed integer overflow. public static int ReverseIntege

【leetcode】7. Reverse Integer

题目描述: Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 解题思路: 这道题比较简单,只要注意两个问题:1,输入可能有123,-123两种情况.2,可能会出现值溢出的情况,所以先用long类型处理,决定没有溢出后再转换为int 具体代码: 1 public static int reverse(int x) { 2 String s =""+x; 3

【Leetcode】 #7 Reverse Integer

Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 测试用例: 123………………321 -123………………-321 1200………………21 45134543545…………0(overflow) 2147483646………………0(虽然它小于最大值2147483647,但是调转过来之后溢出!) 最后两个陷阱是不容易发现的. int类型的最大值是2147483647

LeetCode(7) - Reverse Integer

题目的要求就是要反转一个Integer,例如输入123,则输出321,这一题比较tricky的地方就是它有可能越界,就是说1234567899,反过来是9987654321是一个越界的Integer,按照题目要求,碰到越界返回0,就好.关键的地方就在于,怎么判断它是否越界呢?一开始为了处理这个越界的问题,我才用了一个大小为10的int数组来存每一位,通过处理最高位来判断是否越界,后来发现有更为简单的方法,代码量要小很多,但是两者在速度上并没有什么太大的区别,代码如下: 1 //思路:用数组去存位

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