Skip to content

Instantly share code, notes, and snippets.

@Chen-tao
Created January 27, 2017 06:49
Show Gist options
  • Save Chen-tao/b2e6aacef458e33fdb5c327e3105d5ba to your computer and use it in GitHub Desktop.
Save Chen-tao/b2e6aacef458e33fdb5c327e3105d5ba to your computer and use it in GitHub Desktop.
//https://leetcode.com/problems/permutations/
/**
* 深度优先搜索框架
*/
public class Solution {
public static List<List<Integer>> ans = new ArrayList<List<Integer>>();
public static int[] path = new int[100];
public static boolean[] v = new boolean[100];
public static void robot(int idx, int[] nums){
int n = nums.length;
if(idx >= n){
//record ans
List<Integer> tmp = new ArrayList<Integer>();
for(int i = 0 ; i < n ; i++){
tmp.add(nums[path[i]]);
}
ans.add(tmp);
return;
}
for(int i = 0 ; i < n ; i++){
if(!v[i]){
path[idx] = i;
v[i] = true;
robot(idx + 1, nums);
v[i] = false;
}
}
}
public List<List<Integer>> permute(int[] nums) {
ans.clear();
robot(0, nums);
return ans;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment