美文网首页
排列组合

排列组合

作者: 哲哲哥 | 来源:发表于2017-11-01 21:08 被阅读0次
import java.util.LinkedList;
import java.util.List;
public class Solution {
    public static List<List<Integer>> ans=new LinkedList<List<Integer>>();
    public static boolean[] v=new boolean[100];
    public static LinkedList<Integer> list=new LinkedList<Integer>();
    public static void robot(int index,int[] nums){
        if (index>=nums.length) {
            System.out.println(list);
            List<Integer> list2=new LinkedList<Integer>();
            for (Integer i : list) {
                list2.add(i);
            }
            ans.add(list2);
            return;
        }
        for(int i=0;i<nums.length;i++){
            if (v[i]==false) {
                v[i]=true;
                list.add(nums[i]);
                robot(index+1, nums);
                list.pollLast();
                v[i]=false;
            }
        }
    }
    
    public static void main(String[] args) {
        int []arr={1,2,3};
        robot(0, arr);
        System.out.println(ans);
    }

}
import java.util.LinkedList;
import java.util.List;

public class Solution2 {
    public static List<List<Integer>> ans = new LinkedList<List<Integer>>();
    public static LinkedList<Integer> list = new LinkedList<Integer>();

    public static void robot(int index, int[] nums,int k) {
        if (k==0) {
            System.out.println(list);
            List<Integer> list2 = new LinkedList<Integer>();
            for (Integer i : list) {
                list2.add(i);
            }
            ans.add(list2);
            return;
        }
        for (int i = index+1; i < nums.length; i++) {
            list.add(nums[i]);
            robot(i, nums, k-1);
            list.pollLast();
        }
    }

    public static void main(String[] args) {
        int []arr={1,2,3,0};
        robot(-1, arr,2);
        System.out.println(ans);
    }

}

相关文章

  • 排列组合-js

    排列组合 数学相关知识:5分钟彻底了解排列组合 参考:程序员必备算法——排列组合 排列有序,组合无序 3选2 :排...

  • 排列组合

    python 实现 排列组合

  • 排列组合公式及排列组合算法

    排列组合公式 排列组合公式/排列组合计算公式 公式P是指排列,从N个元素取M个进行排列。 公式C是指组合,从N个元...

  • Leetcode日记:46&47.排列组合与回溯(backtra

    Leetcode日记:46&47.排列组合与回溯(backtrack) 46排列组合1 题目 Given a co...

  • 排列组合

    高中没有学会的排列组合,大学更不会,现在要拾起来,只能理解一个插空法了,我愿意插空在你周围,可是你不在了,我的排列...

  • 排列组合

    排列(Arrangement/Permutation) 百度百科:从n个不同元素中取出m(m≤n)个元素,按照一定...

  • 『排列组合』

    那些排列组合你舍得解开吗?✨ 细嗅那些生活的气息。温暖绵长,意蕴久远。 清明节回家,还是家的感觉温馨美好。陪妈妈逛...

  • 排列组合

    排列组合在笔试面试中不会太难,一般有以下的特点: 案例1 案例2 案例3 案例4 案例5 案例6 其实还有一些比较...

  • 排列组合

    排列(n>=r) 对有n个元素的集合S中的其中r个元素进行排列(n >= r)可以用如下几种方法来理解: 排列描述...

  • 排列组合

    排列组合,简单的说,就是一个计数问题。 我们从小时候就学过数数,一个苹果,两个苹果,但是现在对于稍微复杂一点的计数...

网友评论

      本文标题:排列组合

      本文链接:https://www.haomeiwen.com/subject/rpfnpxtx.html