• Reverse Integer


    Reverse digits of an integer.

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

    注意考虑几个情况,x=1534236460 1534236469 -2147483647等等。

    写的代码有点冗长了。

     1 public class Solution {
     2     public int reverse(int x) {
     3
     7     
     8         if(x<0){//负数
     9             
    10             long xx = x;
    11             xx=xx*(-1);
    12             if(xx>Integer.MAX_VALUE){
    13                 //    System.out.println("///////");
    14                     return 0;
    15                 }
    16         
    17             Integer num = new Integer(x);
    18             num=-num;
    19             String s = num.toString();
    20             StringBuilder sb = new StringBuilder(s);
    21             sb.reverse();
    22             String ss = sb.toString();
    23             
    24             if(Long.parseLong(ss)>Integer.MAX_VALUE){
    25             //    System.out.println("///////");
    26                 return 0;
    27             }
    28             int i = -Integer.parseInt(ss);
    29             return i;
    30             }
    31         
    32         else{//正数
    35                 Integer num = new Integer(x);
    36                 String s = num.toString();
    37                 StringBuilder sb = new StringBuilder(s);
    38                 sb.reverse();
    39                 String ss = sb.toString();
    40         
    41         
    42             if(Long.parseLong(ss)>Integer.MAX_VALUE){
    43             //    System.out.println("///////");
    44                 return 0;
    45             }
    46     
    48             return Integer.parseInt(ss);
    49             }
    50         
    51     }
    52 
    53 }

     312 ms.

  • 相关阅读:
    Python通过多线程实现 `异步`
    Linux(六) 处理用户输入
    Linux(五) 更多结构化命令
    Linux(四) 使用结构化命令
    Linux(三) 科学计算
    Linux(二) Shell脚本
    python 登陆接口
    学习的小建议
    干货
    ThinkPhp5 自定义异常处理类
  • 原文地址:https://www.cnblogs.com/catcoding/p/4707339.html
Copyright © 2020-2023  润新知