递归(一)

第一列:*
第二列:**
第三列:***
第四列:****
。。。
求N列中 * 的个数?

我们发现:
1)最后一列,它的值是N。
2)总个数 = N + 所有剩余列的和。

public int triangle(int c) {
  return c + getCount(c - 1);
}
以上的代码是错误的,递归中必须有一个终止条件,否则会无限地递归下去,引发程序崩溃。

 1 public class Triangle {
 2 
 3     public int triangle(int c) {
 4         if (c > 0) {
 5             int i = c - 1;
 6             return c + triangle(i);
 7         }
 8         return 0;
 9     }
10 
11     public static void main(String[] args) {
12         Triangle t = new Triangle();
13         System.out.println(t.triangle(4));
14     }
15 
16 }

打印结果:
10

递归方法的特征:
1)调用自身
2)当它调用自身的时候,它这么做是为了解决更小的问题。
3)存在某个足够简单的问题的层次,在这一层的算法中不需要调用自己就可以直接解答,且返回结果。

 

变位字:

 1 public class Anagram {
 2 
 3     static int size;
 4 
 5     static char[] arrChar;
 6 
 7     static int count;
 8 
 9     public static void main(String[] args) {
10         String input = "cats";
11         arrChar = input.toCharArray();
12         size = arrChar.length;
13         doAnagram(size);
14     }
15 
16     public static void doAnagram(int newSize) {
17         if (newSize == 1) {
18             return;
19         }
20         for (int i = 0; i < newSize; i++) {
21             doAnagram(newSize - 1);
22             if (newSize == 2)
23                 display();
24             rotate(newSize);
25         }
26     }
27 
28     public static void display() {
29         System.out.print(++count + " : ");
30         for (int i = 0; i < size; i++) {
31             System.out.print(arrChar[i]);
32         }
33         if (count % 6 == 0) {
34             System.out.println();
35         }
36         else {
37             System.out.print("\t");
38         }
39     }
40 
41     public static void rotate(int newSize) {
42         int j;
43         int position = size - newSize;
44         char temp = arrChar[position];
45         for (j = position + 1; j < size; j++) {
46             arrChar[j - 1] = arrChar[j];
47         }
48         arrChar[j - 1] = temp;
49     }
50 
51 }

打印结果:
1 : cats     2 : cast     3 : ctsa      4 : ctas     5 : csat     6 : csta 
7 : atsc     8 : atcs     9 : asct    10 : astc   11 : acts   12 : acst
13 : tsca   14 : tsac   15 : tcas   16 : tcsa   17 : tasc   18 : tacs  
19 : scat   20 : scta   21 : satc   22 : sact   23 : stca   24 : stac

 

递归二分查找:递归的二分查找和非递归二分查找同样是大O效率 O(logN) ,递归的二分查找比较简洁一些,但速度可能会慢一些。

 1 public class OrderArray {
 2 
 3     private long[] array;
 4 
 5     private int maxLength;
 6 
 7     public OrderArray(int size) {
 8         array = new long[size];
 9         maxLength = 0;
10     }
11 
12     //插入时判断大小,保证有序。
13     public void insert(long l) {
14         int i;
15         for (i = 0; i < maxLength; i++) {
16             if (array[i] > l) {
17                 break;
18             }
19         }
20         for (int j = maxLength; j > i; j--) {
21             array[j] = array[j - 1];
22         }
23         array[i] = l;
24         maxLength++;
25     }
26 
27     //二分查找
28     public int find(long key) {
29         int lower = 0;
30         int upper = maxLength - 1;
31         int index;
32         while (true) {
33             index = (lower + upper) / 2;
34             if (array[index] == key) {
35                 return index;
36             }
37             else if (lower > upper) {
38                 return -1;
39             }
40             else {
41                 if (array[index] < key) {
42                     lower = index + 1;
43                 }
44                 else {
45                     upper = index - 1;
46                 }
47             }
48         }
49     }
50 
51     public int recFind(long key) {
52         return recFind(key, 0, maxLength - 1);
53     }
54 
55     //递归二分查找
56     private int recFind(long key, int lower, int upper) {
57         int index = (lower + upper) / 2;
58         if (array[index] == key) {
59             return index;
60         }
61         else if (lower > upper) {
62             return -1;
63         }
64         else {
65             if (array[index] < key)
66                 lower = index + 1;
67             else
68                 upper = index - 1;
69             return recFind(key, lower, upper);
70         }
71     }
72 
73     public void display() {
74         for (int i = 0; i < maxLength; i++) {
75             System.out.println("NO." + (i + 1) + " --> " + array[i]);
76         }
77     }
78 
79 }
 1     public static void main(String[] args) throws ParseException, InterruptedException {
 2         OrderArray array = new OrderArray(100);
 3 
 4         array.insert(49);
 5         array.insert(158);
 6         array.insert(81);
 7         array.insert(93);
 8         array.insert(18);
 9         array.insert(1);
10         array.insert(9);
11         array.insert(7);
12         array.insert(81);
13         array.insert(669);
14         
15         array.display();
16         
17         int key = 18;
18         System.out.println(key + " is NO " + (array.recFind(key)+1));
19     }

打印结果:
NO.1 --> 1
NO.2 --> 7
NO.3 --> 9
NO.4 --> 18
NO.5 --> 49
NO.6 --> 81
NO.7 --> 81
NO.8 --> 93
NO.9 --> 158
NO.10 --> 669
18 is NO 4

posted @ 2012-12-03 11:16  Kyle_Java  阅读(234)  评论(0编辑  收藏  举报