/
githubmirror
/
interviews
Обзор
Документация
Войти
/
githubmirror
/
interviews
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
leetcode/backtracking/Permutations.java
33 строки
881 B
Kevin Naughton Jr
adding additional problems
02 апр 2018, 04:43
02 апр 2018, 04:43
f847f89
Код
Авторство
О чём код?
//Given a collection of distinct 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], //[3,2,1] //] class Permutations { public List<List<Integer>> permute(int[] nums) { LinkedList<List<Integer>> result = new LinkedList<List<Integer>>(); result.add(new ArrayList<Integer>()); for (int n: nums) { int size = result.size(); while(size > 0) { List<Integer> current = result.pollFirst(); for (int i = 0; i <= current.size(); i++) { List<Integer> temp = new ArrayList<Integer>(current); temp.add(i, n); result.add(temp); } size--; } } return result; } }