Reverse Integer

/*

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;

    }

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 在C语言中,五种基本数据类型存储空间长度的排列顺序是: A)char B)char=int<=float C)ch...
    夏天再来阅读 8,722评论 0 2
  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 13,143评论 0 13
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,337评论 19 139
  • 黄昏的时候,我开始起程 从一片荒野走向另一片荒野 寻找最大的落日 那个稻草人化为的夜星石 被我埋藏在相思子藤蔓的下...
    人在旅途小朋阅读 1,618评论 0 0