反转数字、字符串

 1 public class Test
 2 {
 3     final static int[] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999, 99999999, 999999999, Integer.MAX_VALUE };
 4 
 5     static int sizeOfInt(int x)
 6     {
 7         for (int i = 0;; i++)
 8             if (x <= sizeTable[i])
 9                 return i + 1;
10     }
11 
12     public static String reverse(String originStr)
13     {
14         if (originStr == null || originStr.length() <= 1)
15             return originStr;
16         return reverse(originStr.substring(1)) + originStr.charAt(0);
17     }
18 
19     public static Integer reverse(int value)
20     {
21         if (value < 10)
22         {
23             return value;
24         }
25         return (value % 10) * (int)Math.pow(10, sizeOfInt(value) - 1) + reverse(value / 10);
26     }
27 
28     public static void main(String[] args)
29     {
30         System.out.println(reverse("helloworld"));
31         System.out.println(reverse(123456789));
32 
33     }
34 }

 

posted @ 2017-05-16 14:20  挨踢淫才  阅读(393)  评论(0编辑  收藏  举报