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]. 
  给定一个数组,返回他的所有排列。 
  使用分治法求解。 
算法实现类
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;
    }
}
点击图片,鼠标不释放,拖动一段位置,释放后在新的窗口中查看完整图片。
版权声明:本文为博主原创文章,未经博主允许不得转载。
【LeetCode-面试算法经典-Java实现】【046-Permutations(求排列)】
原文地址:http://blog.csdn.net/derrantcm/article/details/47098351