锁定老帖子 主题:一道面试排序题
精华帖 (0) :: 良好帖 (0) :: 新手帖 (4) :: 隐藏帖 (3)
|
|
---|---|
作者 | 正文 |
发表时间:2010-06-04
不用那么复杂吧,看看这行代码
public int reverse(int a){ return Integer.parseInt(new StringBuilder(String.valueOf(a)).reverse().toString()); } |
|
返回顶楼 | |
发表时间:2010-06-04
mouer 写道 public class Test { public static int reverse(int i) { return new Integer(new StringBuffer(new String().valueOf(i)).reverse().toString()).intValue(); } public static void main(String[] args) { System.out.println(Test.reverse(12345)); } } 这个简单多了。。 |
|
返回顶楼 | |
发表时间:2010-06-04
问题在于,要用递归吧
|
|
返回顶楼 | |
发表时间:2010-06-04
最后修改:2010-06-04
public static void main(String[] args) { System.out.println(Number.resver(1023450)); } private static String resver(int high ) { if(high==0) return ""; return (high%10)+resver(high/10); } 8行之内. |
|
返回顶楼 | |
发表时间:2010-06-04
public class Test6 { public static void main(String args[]){ Test6 test = new Test6(); int i = 123456; test.print(i); } public void print(int i){ Integer integer = new Integer(i); String s = integer.toString(); for(int j = s.length()-1; j >= 0; j--){ System.out.print(s.charAt(j)); } } } 用这个,可以搞定:
|
|
返回顶楼 | |
发表时间:2010-06-04
抛出异常的爱 写道 public static void main(String[] args) { System.out.println(Number.resver(1023450)); } private static String resver(int high ) { if(high==0) return ""; return (high%10)+resver(high/10); } 8行之内. 浏览一遍,这个最赞。莫非题是你家出的? |
|
返回顶楼 | |
发表时间:2010-06-04
最后修改:2010-06-04
Alrale 写道 抛出异常的爱 写道 public static void main(String[] args) { System.out.println(Number.resver(1023450)); } private static String resver(int high ) { if(high==0) return ""; return (high%10)+resver(high/10); } 8行之内. 浏览一遍,这个最赞。莫非题是你家出的? private static String resver(int high ) { 大多人没想到这点而已.... 我写的主要是为8行.里面至少两个bug |
|
返回顶楼 | |
发表时间:2010-06-04
有问题,为什么一定要用递归呢??
是不是会写递归的人他认为特别厉害呢? 百思不得其解。 |
|
返回顶楼 | |
发表时间:2010-06-04
soton_dolphin 写道 public class Reverse{ public static void reverse(int number){ if(number<10){ System.out.print(number); } else{ int num = number%10; System.out.print(num); reverse(number/10); } } public static void main(String[] args){ reverse(123); } } 负数性不啊? |
|
返回顶楼 | |
发表时间:2010-06-04
最后修改:2010-06-04
public static String reverseInt(int x) { String string = ""; if(x != 0) { string += x % 10 + reverseInt(x / 10); } return string; } 对于 x=0 不适用 仔细看了其他的回复,原来有了, |
|
返回顶楼 | |