Created
January 27, 2017 06:49
-
-
Save Chen-tao/b2e6aacef458e33fdb5c327e3105d5ba to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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