๐ 46. ๅ จๆๅ
2022ๅนด6ๆ9ๆฅ
- algorithm
๐ 46. ๅ จๆๅ
้พๅบฆ: ๐
้ฎ้ขๆ่ฟฐ
่งฃๆณ
class Solution {
List<List<Integer>> res = new LinkedList<>();
public List<List<Integer>> permute(int[] nums) {
// ๆ่ทฏ๏ผ
// ๆ ้ๅคๆฐ็ป & ๅ
จๆ
LinkedList<Integer> path = new LinkedList<>();
int len = nums.length;
int[] used = new int[len];
mySol(nums, len, used, path);
return res;
}
private void mySol(int[] nums, int len, int[] used, LinkedList<Integer> path) {
// ้ๅฝ็ปๆญขๆกไปถ
if(path.size() == len) {
res.add(new LinkedList<>(path));
return;
}
for(int i = 0; i < len; i ++) {
if(used[i] == 1) {
continue;
}
path.addLast(nums[i]);
used[i] = 1;
mySol(nums, len, used, path);
used[i] = 0;
path.removeLast();
}
}
}