Permutation & Combination
来源:互联网 发布:淘宝网店经营技巧 编辑:程序博客网 时间:2024/06/06 17:43
1.Permutation:
1.1 Recursion
Perm(vec, beg, end)
1.When beg = end, print;
2.Otherwise swap the first element denoted by beg with following elements, then do Perm(vec, beg + 1, end):
for(i = beg; i <= end; ++i)
swap(beg, i);
Perm(vec, beg+1, end);
swap(beg,i);
vector<vector<int>> permutation(vector<int> &nums) { sort(nums.begin(), nums.end()); vector<vector<int>> ret; permutation(nums, 0, ret); return ret; } void permutation(vector<int> nums, int beg, vector<vector<int>> &ret) { if(beg == nums.size()-1) { ret.push_back(nums); return; } for(int i = beg; i < nums.size(); ++i) { vector<int> tmp = nums; if(i == beg || nums[i] != nums[i-1]) //handle duplicates { swap(nums[beg], nums[i]); permutation(nums, beg + 1, ret); nums = tmp; } } }
1.2 Non-recursion
vector<vector<int>> permuteUnique(vector<int>& nums) { sort(nums.begin(), nums.end()); vector<vector<int>> ret; ret.push_back(nums); int k = -1, l = -1; while(true) { k = -1, l = -1; for(int i = 0; i < nums.size()-1; ++i) if(nums[i] < nums[i + 1]) k = i; if(k == -1) break; for(int i = k + 1; i < nums.size(); ++i) if(nums[i] > nums[k]) l = i; swap(nums[k], nums[l]); reverse(nums.begin() + k + 1, nums.end()); ret.push_back(nums); } return ret; }
2.Combination
The result is stored in vector<vector<int>> &v. The helper function take vec as the first parameter which stores the integers that will be used to perform the combination. k is the number of integers that will be used.
class Solution <span style="font-family: Arial, Helvetica, sans-serif;">{</span>
public:void Comb(int k, vector<vector<int>> &v){<span style="white-space:pre"></span>vector<int> vec, tmp;<span style="white-space:pre"></span>for(int i = 0; i < 9; ++i)<span style="white-space:pre"></span>vec.push_back(i+1);<span style="white-space:pre"></span>Comb(vec, 0, tmp, k, v);}void Comb(vector<int> &vec, int beg, vector<int> &tmp, int k, vector<vector<int>> &v){<span style="white-space:pre"></span>if(k == 0 )<span style="white-space:pre"></span>{<span style="white-space:pre"></span>v.push_back(tmp);<span style="white-space:pre"></span>return;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>for(int i = beg; i <= vec.size() - k; ++i)<span style="white-space:pre"></span>{<span style="white-space:pre"></span>tmp.push_back(vec[i]);<span style="white-space:pre"></span>Comb(vec, i+1, tmp, k-1, v);<span style="white-space:pre"></span>tmp.pop_back();<span style="white-space:pre"></span>}};
0 0
- Permutation & Combination
- Subsets, Combination, Permutation, Combination Sum
- [LeetCode] Combination, Permutation, subsets
- Permutation,Subset,Combination…
- Combination and Permutation
- Java版排列组合工具类 - Java Permutation and Combination Tools
- combination
- combination
- Combination
- LinkedIn 面试题:字符串的全排列(Permutation)和组合(Combination)
- Wobble Hypothesis - Combination or Permutation of Array of strings in ruby
- java 实现排列组合Permutation and Combination和链式不相邻排列组合和环形不相邻排列
- Permutation
- Permutation
- permutation
- Permutation
- Permutation
- Permutation
- ios 地图与定位(用户位置)
- vim文档编辑,转载自实验楼
- S3C2440的存储器映射(27根地址线如何寻找1G的地址)
- 评估图像质量评价算法性能的几个常用的标准
- UVALive 6067 Bit Magic 通过三个位运算约束求是否有解 2-sat
- Permutation & Combination
- Spring Boot实践应用开发(11)
- hdu5500 Reorder the Books 贪心
- 假期学习方法
- leetcode之H-index II
- 英语语法 完成式的现在时间
- 类方法的使用
- LeetCode----Subsets II
- 串的模式匹配算法