Given an integer array nums, return all the different possible non-decreasing subsequences of the given array with at least two elements. You may return the answer in any order.
Example 1:
Input: nums = [4,6,7,7]
Output: [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
Example 2:
Input: nums = [4,4,3,2,1]
Output: [[4,4]]
Constraints:
1 <= nums.length <= 15
-100 <= nums[i] <= 100
Original Page
public List> findSubsequences(int[] nums) { List
> list = new ArrayList<>(); List
seq = new LinkedList<>(); Set set = new HashSet<>(); backTracking(list, seq, nums, 0, set); return list; } public void backTracking(List >list, List
seq, int[] nums, int start, Set set){ if(start == nums.length){ return; } for(int i=start; i = 1 && (nums[i] < seq.get(seq.size()-1))){ continue; } // the first element set.add(nums[i]); seq.add(nums[i]); // evaluate non-decreasing if(seq.size() > 1 && nums[i]>= seq.get(seq.size()-2)){ list.add(new ArrayList<>(seq)); } if(seq.size() == 1 || nums[i]>= seq.get(seq.size()-1)){ backTracking(list,seq, nums, i+1, new HashSet<>()); } // backTracking seq.remove(seq.size()-1); } } 46. Permutations
Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order.
Example 1:
Input: nums = [1,2,3]
Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
Example 2:Input: nums = [0,1]
Output: [[0,1],[1,0]]
Example 3:Input: nums = [1]
Output: [[1]]Constraints:
1 <= nums.length <= 6
-10 <= nums[i] <= 10
All the integers of nums are unique.
public List> permute(int[] nums) { List
> list = new ArrayList<>(); List
permutation = new LinkedList<>(); Integer[] numsI = Arrays.stream(nums).boxed().toArray(Integer[]::new); List numList = new ArrayList(Arrays.asList(numsI)); backTracking(list, permutation, numList); return list; } public void backTracking(List > list, List
permutation, List nums){ if(nums.size()==0){ list.add(new ArrayList<>(permutation)); return; } for(int i=0; i workNums = new ArrayList<>(nums); workNums.remove(Integer.valueOf(nums.get(i))); backTracking(list, permutation, workNums); permutation.remove(permutation.size()-1); } } Copy after login
Given a collection of numbers, nums, that might contain duplicates, return all possible unique permutations in any order.
Example 1:
Input: nums = [1,1,2]
Output:
[[1,1,2],
[1,2,1],
[2,1,1]]
Example 2:
Input: nums = [1,2,3]
Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
Constraints:
1 <= nums.length <= 8
-10 <= nums[i] <= 10
Original Page
List> list = new ArrayList<>(); public List
> permuteUnique(int[] nums) { List
permutation = new LinkedList<>(); int[] flags = new int[nums.length]; backTracking(permutation, nums, flags); return list; } public void backTracking(List permutation, int[] nums, int[] flags){ if(permutation.size() == nums.length){ list.add(new ArrayList<>(permutation)); return; } Set set = new HashSet<>(); for(int i=0; i Copy after login
The above is the detailed content of LeetCode DayBackTracking Part 4. For more information, please follow other related articles on the PHP Chinese website!