• 翻转正数--简单题


    var reverse = function (x) {
        x = '' + x;
        var ret = '';
        for (var i  in x) {
            ret = x[i] + ret;
        }
        ret = ret.replace(/[^d]/g, '')
        if (-1 * x > 0) {
            ret = -ret;
        }
        if (ret < -Math.pow(2, 31) || ret > Math.pow(2, 31) - 1) {
            ret = 0;
        }
        return +ret;
    };

    或者low(me)

    window.onload=function(){
    function reverse(x) {
    if(x<-(Math.pow(2,31))){return 0;}
    if(x>(Math.pow(2,31)-1)){return 0;}

    var zero=0;
    var reverseNumber=[];
    var nstring=Math.abs(x)+"";
    var result='';
    var fushu=false;
    var resultNumber=0;
    var abs=Math.abs(x);
    if(abs>x){fushu=true;}else{fushu=false;}
    reverseNumber=nstring.split("");
    zero=reverseNumber.length;
    for(var i=reverseNumber.length-1;i>0;i--){
    if(parseInt(reverseNumber[i])==0){
    zero=i;
    }else{
    break;
    }
    }
    result=reverseNumber.slice(0,zero+1);
    var strings=result.reverse();
    resultNumber=strings.join("")-0
    if(!fushu){ resultNumber=resultNumber;}else{resultNumber=-resultNumber;}
    if(resultNumber<-(Math.pow(2,31))){return 0;}
    if(resultNumber>(Math.pow(2,31)-1)){return 0;}
    };
    reverse(1534236469);
    }

  • 相关阅读:
    POJ 1611 The Suspects 并查集
    POJ A Simple Problem with Integers 线段树的成段更新
    POJ 2367 Genealogical tree 拓扑排序
    《大道至简》读后感
    周总结报告
    周学习进度总结
    周总结报告
    周总结报告
    周总结报告
    教室派评价
  • 原文地址:https://www.cnblogs.com/Ting-log/p/9405587.html
Copyright © 2020-2023  润新知