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。



整数的反转并不容易,但是字符串的反转比较简单,甚至有函数可以直接实现字符串的反转。所以我们考虑首先将整数转换为字符串,然后转换字符串,再转换为整数。

由于String类中并没有对应的反转字符串的API,但是StringBuffer类中有反转字符传API:reverse().

需要注意:当字符串转换为整数时可能会出错,有许多原因,包括格式不匹配、转换后的数超过int的界限等。所以应该加入异常的捕捉和处理。而题目中也要求超出界限返回0.

【Java代码】如下:

public class Solution {
    public int reverse(int x) {
        String s = x + "";
        String symbol = "";
        //判断是否有负号
        if(s.charAt(0) == ‘-‘){
            symbol="-";
            s = s.substring(1, s.length());
        }
        StringBuilder str = new StringBuilder(s);
        //反转字符串
        s = str.reverse().toString();
        s = symbol + s;
        try{
            return Integer.parseInt(s);
        }catch(Exception e){
            //如果转换int出错,则返回0
            return 0;
        }
    }
}

  

如果有任何问题,欢迎跟我联系:[email protected]

我的github地址:github.com/WXRain

时间: 2024-10-10 20:58:20

LeetCode---------Reverse Integer解法的相关文章

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 反转整数数字(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 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 : 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 already thought throug

leetcode:Reverse Integer【Python版】

1.在进入while之前,保证x是非负的: 2.符号还是专门用flag保存 =================== 3.另一思路:将integer转换成string,然后首位swap,直至中间: 1 class Solution: 2 # @return an integer 3 def reverse(self, x): 4 ret = 0 5 flag = 1 6 if x < 0: 7 flag = -1 8 x *= -1 9 while(x!=0): 10 ret = ret*10+x

Leetcode: 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 questions to ask before coding. Bonus points for you if you have already thought through this! If the integ

LeetCode&mdash;&mdash;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 in

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