return
}
publicint
int
int
if
return
}
return
}
96
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].
96.1 Java Solution 1
We can get all permutations by the following steps:
[1]
[2, 1]
[1, 2]
[3, 2, 1]
[2, 3, 1]
[2, 1, 3]
[3, 1, 2]
[1, 3, 2]
[1, 2, 3]
Loop through the array, in each iteration, a new number is added to different loca-
tions of results of previous iteration. Start from an empty List.
publicint[] num) {
ArrayList<ArrayList<Integer>> result =
//start
169|181