"Leetcode-Interview algorithm classic-java Implementation" "046-permutations (Order)" __ Code

Source: Internet
Author: User
"046-permutations (order)" " leetcode-interview algorithm classic-java Implementation" "All topic Directory Index" Original title

Given A collection of numbers, return all possible permutations.
For example,
[1,2,3] have the following permutations:
[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1].
The main effect of the topic

Given an array, returns all of his permutations.
ideas for solving problems

Solved by using the method of divide and conquer.
Code Implementation

Algorithm implementation class

Import java.util.*;

public class Solution {
    private list<list<integer>> result;
    Public list<list<integer>> permute (int[] num) {result

        = new linkedlist<> ();
        if (num!= null) {
            permute (0, num);
        }

        return result;
    }

    private void Permute (int i, int[] num) {

        if (i = = num.length) {
            list<integer> L = new arraylist<> (); for
            (int n:num) {
                l.add (n);
            }

            Result.add (l);
        } else {for

            (int j = i; J < Num.length; J + +) {
                swap (num, j, i);
                Permute (i + 1, num);
                Swap (num, j, i);

    }} private void Swap (int[] A, int x, int y) {
        int tmp = a[x];
        A[X] = A[y];
        A[y] = tmp;
    }
}
Evaluation Results

  Click on the picture, the mouse does not release, drag a position, released in a new window to view the full picture.

Special Notes Welcome reprint, Reprint please indicate the source "http://blog.csdn.net/derrantcm/article/details/47098351"

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.