美文网首页
Reverse Integer

Reverse Integer

作者: 走地牙 | 来源:发表于2018-07-05 02:51 被阅读0次

    /*

    while循环 一直到x = 0;

    拆分: temp = modint;

          modint = int % 10;

          int = int / 10;

    合并  modint = temp * 10 + modint;

    判断  Integer.MIN_VALUE < modint = temp * 10 + modint < Integer.MAX_VALUE

          (modint < -8 && Integer.MIN_VALUE / 10) < temp < (Integer.MAX_VALUE / 10 && modint < 7)

    */

    class Solution {

        public int reverse(int x) {

            int modInt = 0;

            while(x != 0){

                int temp = modInt;

                modInt = x % 10;

                x = x / 10;

                if(temp < Integer.MIN_VALUE / 10 || ((temp == Integer.MIN_VALUE / 10) && (modInt < -8))) return 0;

                if(temp > Integer.MAX_VALUE / 10 || ((temp == Integer.MAX_VALUE / 10) && (modInt > 7))) return 0;

                modInt = temp * 10 + modInt;

            }

            return modInt;

        }

    }

    相关文章

      网友评论

          本文标题:Reverse Integer

          本文链接:https://www.haomeiwen.com/subject/dhluuftx.html