L1:06 - Permutations II
来源:互联网 发布:淘宝联盟高级账户佣金 编辑:程序博客网 时间:2024/06/13 21:08
https://oj.leetcode.com/problems/permutations-ii/
Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example, [1,1,2] have the following unique permutations: [1,1,2], [1,2,1], and [2,1,1].
/** * Copyright: NineChapter * - Algorithm Course, Mock Interview, Interview Questions * - More details on: http://www.ninechapter.com/ */public class Solution { public ArrayList<ArrayList<Integer>> permuteUnique(int[] num) { ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>(); if(num == null || num.length == 0) return result; ArrayList<Integer> list = new ArrayList<Integer>(); int[] visited = new int[num.length]; Arrays.sort(num); helper(result, list, visited, num); return result; } public void helper(ArrayList<ArrayList<Integer>> result, ArrayList<Integer> list, int[] visited, int[] num) { if(list.size() == num.length) { result.add(new ArrayList<Integer>(list)); return; } for(int i = 0; i < num.length; i++) { if (visited[i] == 1 || (i != 0 && num[i] == num[i - 1] && visited[i - 1] == 0)){ continue; } visited[i] = 1; list.add(num[i]); helper(result, list, visited, num); list.remove(list.size() - 1); visited[i] = 0; } } }
0 0
- L1:06 - Permutations II
- L1:05 - Permutations
- 【LeetCode】Permutations && Permutations II
- leetcode: Permutations/Permutations II
- leetcode Permutations & Permutations II
- [Leetcode]Permutations && Permutations II
- Permutations and Permutations II
- Permutations && Permutations ii
- Permutations II
- Permutations II
- Permutations II
- Permutations II
- Permutations II
- Permutations II
- Permutations II
- Permutations II
- Permutations II
- Permutations II
- 循环的角度求均值
- L1:05 - Permutations
- OpenCV基础篇之使用CMake管理工程
- OpenCV基础篇之查找表
- CentOS上编译安装OpenCV-2.3.1与ffmpeg-2.1.2
- L1:06 - Permutations II
- OpenCV基础篇之读取显示图片
- OpenCV基础篇之像素操作对比度调节
- DSP/BIOS使用之初窥门径——滴答时钟及烧写Flash
- PCB设计资料:看到最后才知道是福利
- L1:Combination Sum
- LabVIEW上位机与串口通信
- IOS开发--第一个IOS开发程序(简易的计算器)
- CentOS配置smaba与Windows共享文件