Simple application of recursive algorithm

In order to ensure a recursive function does not cause an infinite loop, it should have the following attributes:

  • A simple 基本案例(basic case)(or some cases) - can not use recursion to generate answers to terminate the program.
  • A set of rules, also referred to 递推关系(recurrence relation), can be split into the base case in all other cases.

1, print string in reverse order

/**
   输入:["h","e","l","l","o"]
   输出:["o","l","l","e","h"]
 */
public class PrintReverseDemo {

    public static void main(String[] args) {
        printReverse("apple".toCharArray());
    }

    private static void printReverse(char[] str) {
        helper(0, str);
    }

    private static void helper(int index, char[] str) {

        if (null == str || index >= str.length) {
            return;
        }
        helper(index + 1, str);
        System.out.print(str[index]);
    }
}

 

2, between two switching nodes in the list

/**
 * 给定 1->2->3->4, 你应该返回 2->1->4->3.
 */
public class SwapNodeDemo {

    public static void main(String[] args){

        ListNode head = makeListNode();

        System.out.println(head);
        System.out.println(swapPairs2(head));
}


    /**
     * 递归
     * @param head
     * @return
     */
    public static ListNode swapPairs(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        
        ListNode p = head.next;
        head.next = swapPairs(head.next.next);
        p.next = head;
        return p;
    }

    /**
     * 非递归
     * @param head
     * @return
     */
    public static ListNode swapPairs2(ListNode head) {
        ListNode dummy = new ListNode(-1);
        dummy.next = head;
        ListNode curr = dummy;
        while (curr.next != null && curr.next.next != null) {
            ListNode first = curr.next;
            ListNode second = curr.next.next;

            // swap two nodes
            first.next = second.next;
            second.next = first;
            curr.next = second;

            // update to next iteration
            curr = curr.next.next;
        }
        return dummy.next;
    }

    public static ListNode makeListNode() {
        ListNode one = new ListNode(1);
        ListNode two = new ListNode(2);
        ListNode three = new ListNode(3);
        ListNode four = new ListNode(4);

        one.next = two;
        two.next = three;
        three.next = four;

        return one;
    }

 

3, Pascal's Triangle

Given a non-negative integer  numRows, generated before Pascal's Triangle of  numRows  line. In Pascal's Triangle, each number is the number of its top left and top right and.

 

public List<List> generate(int numRows) {
        List<List> result = new ArrayList<>();
        if(numRows <= 0){
            return result;
        }

        for(int i=1;i<= numRows; i++){
            List innerList = new ArrayList<>();
            for(int j=1;j<=i;j++){
                innerList.add(f(i,j));
            }
            result.add(innerList);
        }
        return result;
    }

    public int f(Integer i,Integer j){
        if(j == 1 || i == j){
            return 1;
        }else{
            return f(i-1,j-1) + f(i-1,j);
        }
    }

 

Guess you like

Origin www.cnblogs.com/kaleidoscope/p/11407032.html