zoukankan      html  css  js  c++  java
  • Reverse Integer 2015年6月23日

    题目:

    Reverse digits of an integer.
    
    Example1: x = 123, return 321
    Example2: x = -123, return -321

    思路:递归

    解答:

    1032 / 1032 test cases passed.
    Status: Accepted
    Runtime: 340 ms
    Submitted: 0 minutes ago

    这个方法比较糟糕,时间太长用到递归但又没利用函数的返回值,中间还需要借助字符串过渡。

    public class Solution {
        StringBuilder result = new StringBuilder("");
         public int reverse(int x) {
             //Integer.MIN_VALUE 会引起各种麻烦
             if(x == Integer.MIN_VALUE){
                 return 0;
             }
             if(x >= 0){
                 if ( x == 0)
                        return 0;
                 else
                 {
                    result.append(x%10);
                    reverse(x/10);
                 } 
                              
             }else{            
                 if ( x == 0)
                        return 0;
                 else
                 {
                    result.append(Math.abs(x)%10);
                    reverse(x/10);
                 } 
             }
            
             if( Long.parseLong(result.toString())-Integer.MAX_VALUE>0){
                 return 0;
             }else{
                 if(x<0){
                     return 0-Integer.parseInt(result.toString());
                 }else{
                     return Integer.parseInt(result.toString());
                 }
             }
                
         }
    }

    看到一个8ms的C++程序

    1032 / 1032 test cases passed.
    Status: Accepted
    Runtime: 8 ms
    Submitted: 0 minutes ago
    class Solution {
    public:
        int reverse(int x) {
            long result = 0;
            while(x != 0)
            {
                result = result*10 + x % 10;
                x /= 10;
            }
            return (result > INT_MAX || result < INT_MIN)? 0 : result;
        }
    };

    改成JAVA版

    public class Solution {
        public int reverse(int x) {    
            long result = 0;
            while(x != 0)
            {
                result = result*10 + x % 10;
                x /= 10;
            }
            return (int) ((result > Integer.MAX_VALUE || result < Integer.MIN_VALUE)? 0 : result);          
         }
        
    }

    效果:

    1032 / 1032 test cases passed.
    Status: Accepted
    Runtime: 284 ms
    Submitted: 0 minutes ago

    分析:

    Reverse Integer :
    以整形数字12345为例:
    
    第1次循环:
    x=12345
    result = 5
    
    第2次循环:
    x=1234
    result = 5*10+4 = 54
    
    第3次循环:
    x=123
    result = (5*10+4)*10+3 = 543
    
    第4次循环:
    x=12
    result = ((5*10+4)*10+3)*10 + 2 = 5432
    
    第5次循环:
    x=1
    result = (((5*10+4)*10+3)*10 + 2)*10 + 1 = 54321

    该算法无需区分正符号,显然转化成字符串是一种比较low的想法

    可以看到几乎同样的代码,java运行时间要长很多,知乎上给出的解释是java统计时间时把虚拟机的启动时间也考虑在内,所以不同语言之间通过时间来衡量算法优劣是不可取的,用java语言也没必要纠结于此

    另外,本可不用字符串的一定要杜绝字符串的使用,因为其它语言的字符串并不像java这么方便,要考虑代码的通用性

    另外还发现java语言的代码重复运行,时间波动也比较大,这个波动有时都接近100ms!!!

  • 相关阅读:
    Mybatis中Log4j日志的使用
    Mybatis结果集ResultMap映射
    Mybatis中的基本对象的生命周期和作用域
    IAR瑞萨单片机开发加入printf调试函数
    【转】C语言mem.h中的函数介绍
    【转】c语言位域操作—_结构体内冒号:的使用
    串口数据传输当中的共用体和结构体转换
    【转】printf格式串中的%f的输出格式和内容
    【转】缓冲区设计--环形队列(C++)
    【转】环形队列理论(C语言)
  • 原文地址:https://www.cnblogs.com/hixin/p/4595815.html
Copyright © 2011-2022 走看看