Write a method reverse that takes an integer value and returns the number with its digits reversed. For example, given the number 76030, the method should return 3067.
...全文
787打赏收藏
can anyone give me an example for the answer of this question?
Write a method reverse that takes an integer value and returns the number with its digits reversed. For example, given the number 76030, the method should return 3067.
/* this is my answer. how do you think? */
static int reverse(int n)
{
String numString = String.valueOf(n);
int a = numString.length();
int num = 0;
for(int i=1, j=(int)Math.pow(10, a-1); i<=a; i++, j/=10)
{
num += n%10*j;
n/=10;
}
return num;
}
The method reverse() StringBuffer can satify you.
StringBuffer number = new StringBuffer("123450");
StringBuffer reverseNubmer = number.reverse();
这样就行了吧,如果你要去掉0的话使用String的方法找到第一个非0所在位置
然后用substring(index);就行了。