zoukankan      html  css  js  c++  java
  • 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.

  • 相关阅读:
    When You Get Troubles
    CentOS 6.8升级到7+
    Tomcat服务器搭建
    Percona Server 安装
    VirtualBox中如何使虚拟机能够上网?
    CentOS
    xen安装
    SSH免密码设置
    打造绿色版的RobotFramework
    零散知识记录-Jira的安装
  • 原文地址:https://www.cnblogs.com/catcoding/p/4707339.html
Copyright © 2011-2022 走看看