• 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!!!

  • 相关阅读:
    MySQL根据父ID排序类别
    isNotBlank的用法
    IDEA中的.iml文件和.idea文件夹
    java组件:获取查询月份的第一天和最后一天,默认取当前月份
    MyBatis:条件构造器QueryWrapper方法详解
    querywrapper条件构造器
    mybatisplus主键策略选择
    mybatis-plus 高级搜索分页功能的实现 sql语句 QueryWrapper 条件判断
    QueryWrapper查询
    MySQL根据父ID排序类别(mysql sort category according to parent id)
  • 原文地址:https://www.cnblogs.com/hixin/p/4595815.html
Copyright © 2020-2023  润新知