排列问题汇总

12 篇文章 0 订阅
4 篇文章 0 订阅

排列问题有两种解决方案:1)基于swap。 2)基于选择。基于swap的方法就是回溯法中解空间树是排列树的情况,candidate分别排(swap)第一位,然后全排列后面的部分,是in place的。基于选择的思路是,依次填每个位置,每次选择都是顺序从全candidate集合选取,如果前面没选过,就是合法的candiate。缺点很明显:一是每次都是尝试从全candidate集合选取,还要到已选的集合查看是否被选过,复杂度比基于swap的方法高,当然可以用引入hashset 的办法优化。空间复杂度方面,不是in place的,需要另外一个集合存放排列的结果。但基于选择的方法有一个好处,就是输出的排列是字典序的。

import java.util.Arrays;

class HelloWorld {
    public static void main(String[] args) {
        dfs(new int[] {1, 2, 3}, new boolean[3], new int[3], 0);
    }
    static void dfs(int[] arr, boolean[] used, int[] res, int t) {
        if (t == arr.length) {
            System.out.println(Arrays.toString(res));
            return;
        }
        for (int i = 0; i < arr.length; ++i) {
            if (!used[i]) {
                used[i] = true;
                res[t] = arr[i];
                dfs(arr, used, res, t + 1);
                used[i] = false;
            }
        }
        /*
        for (int i = t; i < res.length; ++i) {
            res[t] = arr[]
            dfs(arr, t + 1);
            swap(arr, t , i);
        }*/
    }
    static void swap(int[] arr, int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
}

 上面代码可以看出,两种方式在复杂度方面区别不大

有重复的元素的情况:

1)基于swap的方案

对于一个位置的每个candidate,判断在这个位置上是这个值否出现过,如果出现过则忽略,因为二者的排列是一样的:第一个位置一样,后面的集合是一样的,全排列是相同的。

2)基于选择的方法

class Solution {
    List<List<Integer> >  ans = new ArrayList<>();
    public List<List<Integer>> permuteUnique(int[] nums) {
        Arrays.sort(nums);
        dfs(nums, new int[nums.length], new boolean[nums.length], 0);
        return ans;
    }
    void dfs(int[] nums, int[] res, boolean[] used, int t) {
        if (t == res.length) {
            ans.add(IntStream.of(res).boxed().collect(Collectors.toList()));
            return;
        }
        for (int i = 0; i < nums.length; ++i) {
            if (used[i] || i > 0 && nums[i] == nums[i - 1] && !used[i - 1]) continue;
            used[i] = true;
            res[t] = nums[i];
            dfs(nums, res, used, t + 1);
            used[i] = false;
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
11086 排序问题再探讨 时间限制:1000MS 内存限制:65535K 提交次数:0 通过次数:0 题型: 编程题 语言: 无限制 Description 此题以程序填空的形式进行,请将下列程序框架复制到本机,并按下面要求填充完整后再用g++编译器提交, 在不改变程序框架情况下,可以自由添加所需的函数和变量,或修改合适的函数参数。 1,请改写一个"递归"的插入排序,排序a[0…n-1],先递归的排序a[0…n-2],然后再将a[n-1]插入到已排序的a[0…n-2]中去。 2,自然合并排序,书上2.7节最后介绍的算法,请实现它。 3,快速排序,选择"中位数"作为轴值然后进行左右段分区,请实现它。 #include #include "stdlib.h" using namespace std; const int SIZE = 10001; int a[SIZE]; void RecurInsertionSort(int p, int q) //对a[p…q]的递归插入排序,参数可根据自己需要修改。 { …… } void NaturalMergeSort(int n) //对n个元素的自然合并排序,参数可根据自己需要修改。 { …… } int Partition(int x, int p, int q) //以x为基准元素划分a[p…q],返回基准下标. 书上2.8节有。参数可根据自己需要修改。 { …… } int median(int p, int q) //挑出a[p…q]的中位数,并返回中位数,参数可根据自己需要修改。 { …… } void QuickSort(int p,int q) //参数可根据自己需要修改。 { if(p>=q)return; int x = median(p, q); int i=Partition(x,p,q); QuickSort(p,i-1); QuickSort(i+1,q);//递归 } int main() { int i,n; cin >> n; //递归插入排序 for(i=0;i> a[i]; } RecurInsertionSort(0,n-1); cout << "Insert sort: "; for(i=0;i<n;i++) { cout << a[i] << ' '; } //自然合并排序 for(i=0;i> a[i]; } NaturalMergeSort(n); cout << "\nNatural merge sort: "; for(i=0;i<n;i++) { cout << a[i] << ' '; } //快速排序 for(i=0;i> a[i]; } QuickSort(0, n-1); cout << "\nQuick sort: "; for(i=0;i<n;i++) { cout << a[i] << ' '; } return 0; } 输入格式 第一行:n,表示n个元素待排序。n不超过10000个。 第二行:n个数的序列,用来做递归的插入排序; 第三行:n个数的序列,用来做自然合并排序; 第四行:n个数的序列,用来做快速排序。 注意:第二、三、四行之间无联系,可能相同也可能不相同。 输出格式 三行,分别为三种排序算法排序后输出。 注意前面有“Insert sort:”或“Natural merge sort:”或“Quick sort:”等输出前缀,格式以上文程序为准。 输入样例 9 3 5 2 1 7 8 1 5 9 8 5 2 1 7 3 1 5 9 5 9 2 1 7 8 1 3 5 输出样例 Insert sort: 1 1 2 3 5 5 7 8 9 Natural merge sort: 1 1 2 3 5 5 7 8 9 Quick sort: 1 1 2 3 5 5 7 8 9 提示 1,递归的插入排序不难写哈。 void RecurInsertionSort(int p, int q)//递归插入,跟递归求阶乘的思想一样,前n个排好序的数组
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值