美文网首页
有一种问题叫全排列

有一种问题叫全排列

作者: 闫依琳2021强化班 | 来源:发表于2022-04-03 22:06 被阅读0次

全排列问题:(非字典序)

public class Main

{

  public static void main(String arsg[])

  {

      perm(new int[]{1,2,3,4,5},0,4)

  }

  public static void perm(int[] arr,int start,int end)

  {

      if(start == end)

      {

          for(int i = 0;i<arr.length;i++)

          {

              System.out.print(arr[i]+" ");

          }

          System.out.println();

      }

      else

      {

for(int i = start;i<=end;i++)

{

    swap(arr,start,i);

    perm(arr,start+1,end);

    swap(arr,start,i);

}

   

  }

  public static vodi swap(int[] arr,int i,int j)

  {

      int temp;

      temp = arr[i];

      arr[i] = arr[i];

      arr[j] = temp;

  }

}

字典序:洛谷1706全排列问题

import java.util.Scanner;

public class 全排列 {

    public static void main(String args[])

    {

        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();

        int arr[] = new int[n];

        for(int i = 0;i<arr.length;i++)

        {

            arr[i] = i+1;

        }

        perm(arr,0,n-1);

    }

    public static void perm(int[] arr,int start,int end)

    {

        if(start == arr.length-1)

        {

            for(int i = 0;i<=end;i++)

            {

                System.out.print(arr[i]+" ");

            }

            System.out.println();

        }

        else

        {

            for(int i = start;i<=end;i++)

            {

                swap(arr,start,i);

                perm(arr,start+1,end);

                swapback(arr,start,i);//回溯

            }

        }

    }

    public static void swap(int[] arr,int i,int j)

    {

        int temp = arr[j];

    for(int a = j;a>i;a--)

      {

      arr[a] = arr[a-1];

                    }

      arr[i] = temp;

    }

    public static void swapback(int[] arr,int i,int j)

    {

        int temp = arr[i];

        for(int a = i;a<j;a++)

        {

            arr[a] = arr[a+1];

        }

        arr[j] = temp;

    }

}

从n个数字里面任意取r个数字排序:(洛谷P1157)

public class P1157 {

  static int n,r,ay[];

  static boolean arr[];

public static void main(String args[])

{  Scanner sc = new Scanner(System.in);

      n = sc.nextInt(); //输入n个总的需要排列的元素

      r = sc.nextInt();//输入r个排列个数

    arr = new boolean[n+1];//标记数字是否使用过

    ay = new int[r+1];//存放输出数组

    perm(1);

}

static void perm(int x)

{

    if(x > r)//递归出口,满r个数字输出

    {

        for(int i = 1;i<=r;i++)//从第一位开始存放

        {

          System.out.printf("%3d",ay[i]);//输出为3个字符

        }

        System.out.println();

    }

    else

    {

        for(int i = 1;i<=n;i++)//从第一位开始存放

        {

            if(arr[i])//boolean型的数据初始值为false

            {

                continue;

            }

            if(x>1&&ay[x-1]>i)//去重

            {

                continue;

            }

            arr[i] = true;

            ay[x] = i;

            perm(x+1);

            arr[i] = false;

        }

    }

}

}

相关文章

  • 有一种问题叫全排列

    全排列问题:(非字典序) public class Main { public static void mai...

  • 全排列问题

    中午看到的一个题目。求一个不重复字符串的全排列。 主要有递归,字典序等解决方案。然后想到stl里的next_per...

  • 全排列问题

    题目:给定一个 没有重复 数字的序列,返回其所有可能的全排列。 示例:输入: [1,2,3] 输出:[ [1,2,...

  • 全排列问题

    // 全排列.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。/*解题:1、递归式:填好怕...

  • 深度优先搜索

    全排列问题。 迷宫问题。

  • 八皇后问题

    最近接触到八皇后问题,看了下其中一种思路还挺简单,就是全排列然后筛选出斜边也满足的排列方法。比较麻烦的是全排列,特...

  • leetcode全排列问题

    1.leetcode47 题目: 给定一个可包含重复数字的序列,返回所有不重复的全排列。 示例: 输入:[1,1,...

  • 递归--全排列问题

    前置文章:递归算法:www.jianshu.com/p/703069f3ba3f . 递归问题有两个经典的...

  • 数组全排列问题

    最近看到剑指offer上一道数组全排列的题目,看似很简单,仔细分析一下,还是有点难以理解,特此在这拆解下,希望能够...

  • 剑指 Offer 第38题:字符串的排列

    1、前言 2、思路 这道题就是全排列,全排列分两种,一种是无重复数字,那么其实不用考虑去重问题,直接把所有结果都放...

网友评论

      本文标题:有一种问题叫全排列

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