LeetCode-Permutations && unique && next
来源:互联网 发布:wind炒股软件 编辑:程序博客网 时间:2024/06/08 11:25
Permutation
按照全排列的方式,每次生成后,下一个数字来插孔生成所有的情况,不然使用递归还要标记每个位置,麻烦。
class Solution(object): def permute(self, nums): perms = [[]] for n in nums: new_perms = [] for perm in perms: for i in xrange(len(perm)+1): new_perms.append(perm[:i] + [n] + perm[i:]) ###insert n perms = new_perms return perms
Next Permutation
从后往前找第一个非逆序数i,即不上升数字(联想到峰值问题,只需要比较紧挨的两个即可,不用每次比较两边),找到后再从后往前找第一个大于非逆序数i的数字j,这两个交换位置,即i这个位置还能继续往后迭代。需要的是一个大于他的数字j放到i,然后后面的转置即可(之前都是逆序,转为正序)
class Solution {public: void nextPermutation(vector<int> &num) { int i, j, n = num.size(); for (i = n - 2; i >= 0; --i) { if (num[i + 1] > num[i]) { for (j = n - 1; j >= i; --j) { if (num[j] > num[i]) break; } swap(num[i], num[j]); reverse(num.begin() + i + 1, num.end()); return; } } reverse(num.begin(), num.end()); }};
Permutations II
对于有重复数字的情况,其实仅需要考虑重复的数字不要越过之前相同数字的位置。
从排列的角度来看,将有重复的数字摆好,其他的数字在其中插入,本质是重复数字不越位
def permuteUnique(self, nums): ans = [[]] for n in nums: new_ans = [] for l in ans: for i in xrange(len(l)+1): new_ans.append(l[:i]+[n]+l[i:]) if i<len(l) and l[i]==n: break #handles duplication ans = new_ans return ans
public class Solution { public List<List<Integer>> permuteUnique(int[] nums) { List<List<Integer>> res = new ArrayList<List<Integer>>(); if(nums==null || nums.length==0) return res; boolean[] used = new boolean[nums.length]; List<Integer> list = new ArrayList<Integer>(); Arrays.sort(nums); dfs(nums, used, list, res); return res; } public void dfs(int[] nums, boolean[] used, List<Integer> list, List<List<Integer>> res){ if(list.size()==nums.length){ res.add(new ArrayList<Integer>(list)); return; } for(int i=0;i<nums.length;i++){ if(used[i]) continue; if(i>0 &&nums[i-1]==nums[i] && !used[i-1]) continue; used[i]=true; list.add(nums[i]); dfs(nums,used,list,res); used[i]=false; list.remove(list.size()-1); } }}
阅读全文
0 0
- LeetCode-Permutations && unique && next
- 【LeetCode】Permutations & Permutations II & Next Permutations & Permutation Sequence
- 【LeetCode】31. Next Permutations 下一个全排列
- 【C++】【LeetCode】31. Next Permutation && 46. Permutations
- [LeetCode] Next Permutation、Permutations、Permutations II、Permutation Sequence
- 【leetcode 排列组合问题】Next Permutation | Permutations | Permutations II | Permutation Sequence
- Unique Permutations
- Unique Permutations
- 【LeetCode】Permutations && Permutations II
- leetcode: Permutations/Permutations II
- leetcode Permutations & Permutations II
- [Leetcode]Permutations && Permutations II
- LeetCode: Permutations
- LeetCode: Permutations
- [Leetcode] Permutations
- [LeetCode] Permutations
- Leetcode: Permutations
- [Leetcode] Permutations
- CodeForces 489D Unbearable Controversy of Being
- 使用组函数
- 了解Python
- hibernate 组件映射
- UART设备驱动探究1
- LeetCode-Permutations && unique && next
- handler 弱引用
- hive-03-hive的分区
- (三)JavaScript关于浏览器--操作表单
- 根据文本内容自动计算文本高度
- webpack上手攻略
- easyui刷新指定tab页里面的数据
- leetcode-204-质数的个数
- CF853A Planning【贪心】