Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
public class Solution {
public int reverse(int x) {
int sum = 0;
while (Math.abs(x) != 0)
{
if(Math.abs(sum) > Integer.MAX_VALUE / 10)
{
return 0;
}
sum = sum * 10 + x % 10;
x = x / 10;
}
return sum;
}
}public class Solution {
public int reverse(int x) {
if(Math.abs(x)>100){
return 0;
}
else{
String x_str=Integer.toString(x);
char[] x_char = x_str.toCharArray();
String x_reverse_str="";
//System.out.print(Character.isDigit(x_char[0]));
if(Character.isDigit(x_char[0])){
for(int i=(x_str.length()-1);i>=0;i--){
x_reverse_str+=x_char[i];
}
}
else{
x_reverse_str+=x_char[0];
for(int i=(x_str.length()-1);i>=1;i--){
x_reverse_str+=x_char[i];
}
}
return Integer.parseInt(x_reverse_str);}
}
}
/********************************
* 本文来自博客 “李博Garvin“
* 转载请标明出处:http://blog.csdn.net/buptgshengod
******************************************/
【LeetCode从零单排】No.7 Reverse Integer
原文地址:http://blog.csdn.net/buptgshengod/article/details/43563767