Combinations & Permutations (1.II)

Combinations

Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.

For example,
If n = 4 and k = 2, a solution is:

[
  [2,4],
  [3,4],
  [2,3],
  [1,2],
  [1,3],
  [1,4],
]

思路一: 典型的DFS,遍历所有的路径,找到满足要求的结果

代码一:
  1. ArrayList<ArrayList<Integer> > res = new ArrayList<ArrayList<Integer> >();
  2. ArrayList<Integer> cur = new ArrayList<Integer>();
  3. public void helper(int start,int n,int k,int len) {
  4. if(len>=k) {
  5. if(len==k) {
  6. res.add(new ArrayList<Integer>(cur));
  7. }
  8. return;
  9. }
  10. for(int i=start;i<=n;i++) {
  11. cur.add(i);
  12. helper(i+1,n,k,len+1);
  13. cur.remove(cur.size()-1);
  14. }
  15. }
  16. public ArrayList<ArrayList<Integer> > combine(int n, int k) {
  17. if(k>n) return res;
  18. if(n==0) return res;
  19. if(k==0) return res;
  20. helper(1,n,k,0);
  21. return res;
  22. }

C++实现代码:

  1. vector<vector<int> > res;
  2. void combineHelper(vector<int> & cur,int start,int end,int n) {
  3. if(cur.size()==end) {
  4. res.push_back(cur);
  5. return;
  6. }
  7. for(int i=start;i<=n;i++) {
  8. cur.push_back(i);
  9. combineHelper(cur,i+1,end,n);
  10. cur.pop_back();
  11. }
  12. }
  13. vector<vector<int> > combine(int n, int k) {
  14. vector<int> cur;
  15. combineHelper(cur,1,k,n);
  16. return res;
  17. }

思路二: 用递归的思想求解,类似于Permutations

代码:

  1. vector<vector<int> > combine(int n, int k) {
  2. vector<vector<int> > res;
  3. vector<vector<int> > vec(1);
  4. vector<int> tmp;
  5. int len=0;
  6. for(int i=1;i<=n;i++) {
  7. len=vec.size();
  8. for(int j=0;j<len;j++) {
  9. tmp=vec[j];
  10. tmp.push_back(i);
  11. if(tmp.size()==k) res.push_back(tmp);
  12. else vec.push_back(tmp);
  13. }
  14. }
  15. return res;
  16. }

 

Permutations 

     在前面几种方法的基础上,可以用递归的思想求解,要学会选用合适的数据结构,(开始选用了ArrayList,没有合理的结合permute(int[] num)函数,导致了int[]和ArrayList之间返回转换。

  1. public ArrayList<ArrayList<Integer> > permute(int[] num) {
  2. Arrays.sort(num);
  3. ArrayList<ArrayList<Integer> > res = new ArrayList<ArrayList<Integer> >();
  4. ArrayList<ArrayList<Integer> > subres = new ArrayList<ArrayList<Integer> >();
  5. int n = num.length;
  6. ArrayList<Integer> newList = new ArrayList<Integer>();
  7. if(n==0) return res;
  8. if(n==1) {
  9. newList.add(num[0]);
  10. res.add(newList);
  11. return res;
  12. }
  13. for(int i=0;i<n;i++) {
  14. if(i>=1 && num[i]==num[i-1]) continue;  // 注意去除重复项
  15. int tmp = num[i];
  16. num[i]=num[0];
  17. num[0]=tmp;
  18. int[] new_num = Arrays.copyOfRange(num,1,num.length); // 数组复制
  19. subres = permute(new_num);
  20. for(int j=0;j<subres.size();j++) {
  21. subres.get(j).add(num[0]);
  22. }
  23. res.addAll(subres);
  24. tmp = num[i];
  25. num[i]=num[0];
  26. num[0]=tmp;
  27. }
  28. return res;
  29. }

 

Permutations II (和Permutations 基本一致,去除重复项即可)

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].

posted @ 2014-06-28 00:37  purejade  阅读(106)  评论(0)    收藏  举报