import java.util.ArrayList; import java.util.linkedList; import java.util.List; public class Main { List> res = new ArrayList<>(); List
> permute(int[] nums){ List
options = new ArrayList<>(); if(nums == null || nums.length == 0) return res; for (int num : nums) { options.add(num); } place(new linkedList (),options); return res; } private void place(linkedList path, List options){ if(path.size() == options.size()){ res.add(new linkedList(path)); //这里必须是new一个list,因为path是地址,内容在变化 return; } for (int i = 0; i < options.size(); i++) { if(path.contains(options.get(i))){ continue; } path.add(options.get(i)); place(path,options); // 1 3 4 ; 1 3 4; path.removeLast(); // 1 3 ; 1 3 4; } } public static void main(String[] args) { Main main = new Main(); main.permute(new int[]{1,3,4}); for (List re : main.res) { for (Integer integer : re) { System.out.print(integer + " "); } System.out.println(); } } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)