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‘s last digit is 0, what should the output be? ie, cases such as 10, 100.

 

Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases?

Throw an exception? Good, but what if throwing an exception is not an option? You would then have to re-design the function (ie, add an extra parameter).

分析:

简单的分析逆置一个整数比较简单,代码如下:

public class Solution {
    public int reverse(int x) {
        int result = 0;
        while(x != 0){
            result = result*10+x%10;
            x = x/10;
        }
        return result;
    }
}

上面的代码对于类似10100这样的数逆置之后就变为了101,也算可以接受,但是没有考虑溢出的问题。

 

在考虑溢出这个问题上,

我们可以先把原数x的符号位记录下来,在java中x的符号位可以获取为 int sign=x>>31,当sign为-1表示为负数,否则为正。

然后按照上面的方法求逆置的数result,再判断result的符号位是否和原数x相同,若相同则没有溢出;否则溢出。

实现的代码如下:

public int  reverse2(int x) {
        int result = 0;
        int sign = x>>31;
        while (x != 0) {
            result = result * 10 + x % 10;
            x = x / 10;
        }
//        System.out.println(sign+", "+(result>>31));
        if(sign!=(result>>31)){
            System.out.println("overflow..");
            System.exit(1);
        }
        return result;
    }

 



以下是我用于测试的完整代码:

public class ReverseInt {
    public static void main(String[] args) {
        ReverseInt r = new ReverseInt();
        System.out.println(r.reverse2(123));
        System.out.println(r.reverse2(1230));
        System.out.println(r.reverse2(-123));
        System.out.println(r.reverse2(1000000003));

    }
    public int reverse(int x) {
        int result = 0;
        while (x != 0) {
            result = result * 10 + x % 10;
            x = x / 10;
        }
        return result;
    }
    public int  reverse2(int x) {
        int result = 0;
        int sign = x>>31;
        while (x != 0) {
            result = result * 10 + x % 10;
            x = x / 10;
        }
//        System.out.println(sign+", "+(result>>31));
        if(sign!=(result>>31)){
            System.out.println("overflow..");
            System.exit(1);
        }
        return result;
    }
}
时间: 2024-10-11 17:33:35

LeetCode——Reverse Integer(逆置一个整数)的相关文章

leetcode——Reverse Integer 反转整数数字(AC)

Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 这个题比较简单,考虑特殊情况如12000,注意检查反转后数字是否会越界溢出.代码如下: class Solution { public: int reverse(int x) { bool minus = false; short int splitNum[10]; int i = 0, j = 0; unsign

[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——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'

LeetCode Reverse Integer 反转整数

1 class Solution { 2 public: 3 int reverse(int x) { 4 int e,s,num,k=0; 5 num=x; 6 e=0; 7 if(x<0) 8 num=-1*x; 9 while( num!=0 ){ 10 s=num%10; 11 e=e*10+s; 12 num=num/10; 13 k++; 14 } 15 if(x<0) 16 return -e; 17 else 18 return e; 19 } 20 }; 题意: Exampl

[leetcode]273. Integer to English Words 整数转英文单词

Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 1. Example 1: Input: 123Output: "One Hundred Twenty Three"Example 2: Input: 12345Output: "Twelve Thousand Three Hundred Fort

LeetCode: Reverse Integer 解题报告

Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 click to show spoilers. SOLUTION 1: 注意越界后返回0.先用long来计算,然后,把越界的处理掉. public class Solution { public int reverse(int x) { long ret = 0; while (x !

[LeetCode] Reverse Integer 翻转整数

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 throu

leetcode——7 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'

LeetCode Reverse Bits 反置位值

题意:给定一个无符号32位整数,将其二进制形式左右反置,再以整型返回. 思路:循环32轮,将n往右挤出一位就补到ans的尾巴上. 1 class Solution { 2 public: 3 uint32_t reverseBits(uint32_t n) { 4 if( !n ) return 0; 5 uint32_t ans = 0; 6 int i; 7 for(i=0; i<32; i++ ) 8 { 9 ans <<= 1; 10 if( n & 1 ) 11 ans