首页 > 学院 > 开发设计 > 正文

Permutations

2019-11-06 08:50:39
字体:
来源:转载
供稿:网友

Given a list of numbers, return all possible permutations.

class Solution { /** * @param nums: A list of integers. * @return: A list of permutations. */ public List<List<Integer>> permute(int[] nums) { List<List<Integer>> results = new ArrayList<>(); if (nums == null) { return results; } if (nums.length == 0) { results.add(new ArrayList<Integer>()); return results; } List<Integer> result = new ArrayList<>(); dfsHelper(nums, results, result); return results; } PRivate void dfsHelper(int[] nums, List<List<Integer>> results, List<Integer> result) { if (result.size() == nums.length) { results.add(new ArrayList<Integer>(result)); } for (int i = 0; i < nums.length; i++) { if (result.contains(nums[i])) { continue; } result.add(nums[i]); dfsHelper(nums, results, result); result.remove(result.size() - 1); } }}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表