Algorithmic thinking mode--thought of by permutation combination

Source: Internet
Author: User

Recently the algorithm has brushed a lot of problems, small have sentiment.

I think the general way of thinking when brushing a question is iterative thinking. Is that we're always thinking about iterating through the loop, solving the whole problem in order to solve the next iteration.

Typical examples are: 2Sum, 3Sum, sorting problems, and problems solved with double pointers or fast and fast pointer methods.

Iterative thinking is a straightforward way of thinking, but it is never easy, because finding the right loop is not an easy task.

However, some problems are difficult to solve with iterative thinking, or they are not suitable for recycling.

For example, the combinatorial number problem. C (n, 2) can also be solved with a 2-layer loop, but C (n, m)? It is difficult to solve with iterations, unnatural. This is a recursive way of thinking but it is natural.

Take one first, turn the problem into C (n, m-1), then take one, and turn the problem into C (n, m-2), so that it can be recursive.

    public static int combinationnum = 0;    public static void combine (int[] target, int[] result, int st, int index,int m) {        if (m = = 0) {            combinationnum++;
   for (int i:result) {                System.out.printf (i + "");            }            System.out.println ();            System.out.println (Combinationnum);        } else {for            (int i = index; i < target.length-m + 1; i++) {                result[st] = target[i];                Combine (target, result, st+1, i+1,m-1);}}}    

The same is true of the permutation number, which turns a (n,n) into a (n-1,n-1) until it becomes a (a).

    public static void Swap (int[] result, int st, int ele) {        int tmp = result[st];        RESULT[ST] = Result[ele];        Result[ele] = tmp;    }    Take turns to put the elements in the first place, and then recursive solution. Note the reset. Public    static void arrange (int[] result, int st) {        if (st = = result.length-1) {for            (int i:result) {                syste m.out.printf (i + "");            }            System.out.println ();        } else {            for (int i = st; i < result.length; i++) {                Swap (result, I, ST);                Arrange (result, st+1);                Swap (result,i, ST);}}}    

The feeling of recursion for this type of problem is clean, concise, and has a logical aesthetic.

However, it is not easy to pass the question to naturalization. One common technique is "a son moves the world". is a discussion of whether or not an element is to be categorized, and this element is often the last. At this point, the problem can often be divided into two sub-naturalization.

The above two examples are actually discussed for an element, but they divide the problem much, so the outer layer has loops to traverse. Here is an example of a two-part recursive naturalization.

Gives an array with an int number that is not duplicated. The combination of all numbers that satisfy and are S. (Array [2, 3, 6, 7], and for 7 , the result is: [[7], [2, 2, 3]]). That's the problem of changing coins.

At this point we can divide the problem by the inclusion of the last element. Then recursively traverse the entire solution space:

    Private list<list<integer>> result = new arraylist<> (); A typical example of a child moving the world, a recursive way of thinking typical. DFS traverses the entire solution space, naturally resulting in results.    Clean and concise. public void combinations (int[] candidates, int target, list<integer> combination, int limit) {if (Limit <        0) {return;        }else if (target = = 0) {Result.add (combination);                }else {if (Target-candidates[limit] >= 0) {combination.add (candidates[limit]);                Combinations (candidates, Target-candidates[limit], new arraylist<> (combination), limit);            Combination.remove (Combination.size ()-1);        } combinations (candidates, target, combination, limit-1); }} public list<list<integer>> combinationsum (int[] candidates, int target) {Arrays.sort (candida        TES);        int limit = candidates.length-1;        Combinations (candidates, target, new arraylist<integer> (), limit);    return result; }

  

Algorithmic problems are changeable and complex. If the iteration doesn't work, you might want to try a recursive approach. "A son moves the world" is a kind of good discussion way.

Algorithmic thinking mode--thought of by permutation combination

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.