Reverse|Reverse Integer
/*
while循环 一直到x = 0;
拆分: temp = modint;
modint = int % 10;
int = int / 10;
【Reverse|Reverse Integer】合并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;
}
}
推荐阅读
- codility|codility 之 MissingInteger
- Shell脚本中的while和for循环
- Products|Products of Binghamton
- Python|Python 为什么不设计 do-while 循环结构()
- Integer常量池结合源码解析
- while循环打印
- Codeforces Round #609 (Div. 2)——C. Long Beautiful Integer(思维)
- C++自学计划-循环-09
- python使用while、for及循环嵌套实现直角三角形及正、倒金字塔
- Python的while循环——输出等腰三角形