class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
if(nums.length == 0){
return result;
}
ValueRecord[] valueRecords = new ValueRecord[nums.length];
for(int i = 0; i<nums.length;i++){
ValueRecord valueRecord = new ValueRecord();
valueRecord.value = nums[i];
valueRecords[i] = valueRecord;
}
List<Integer> path = new ArrayList<>();
dfs(nums, path, valueRecords, result);
return result;
}
/**
*
**/
private void dfs(int[] nums, List<Integer> path, ValueRecord[] valueRecords, List<List<Integer>> result ){
if(path.size() == nums.length){
result.add(new ArrayList(path));
return;
}
for(int currIndex = 0; currIndex<nums.length;currIndex++){
ValueRecord valueRecord = valueRecords[currIndex];
if(!valueRecord.isValid){
continue;
}
valueRecord.isValid = false;
path.add(valueRecord.value);
dfs(nums, path, valueRecords, result);
path.remove(path.size()-1);
valueRecord.isValid = true;
}
}
public static class ValueRecord{
int value;
boolean isValid = true;
}
}