zoukankan      html  css  js  c++  java
  • 字符串全排列(递归)

    题目:打印一个字符串的全部排列

    比如:


    import java.io.BufferedInputStream;
    import java.util.Scanner;
    
    public class test {
    
        public static void arrange(char[] str, int i) {
            if (i == str.length) {
                System.out.println(str);
            } else {
                for (int j = i; j < str.length; ++j) {
                    swap(str, i, j); // 交换下标为i,j的字符串
                    arrange(str, i + 1); // 第二个参数是该区间的起点,划分为一个个小区间解决
                    // 起点不断递归,最后全排列打印时也是最后的小区间先交换的
                    swap(str, i, j); // 再换回来,保持原有序列
                }
            }
        }
    
        public static void swap(char[] str, int i, int j) {
            char c = str[i];
            str[i] = str[j];
            str[j] = c;
        }
    
        public static void main(String[] args) {
            Scanner cin = new Scanner(new BufferedInputStream(System.in));
            String str = cin.next();
            arrange(str.toCharArray(), 0);
            cin.close();
        }
    }
    ========================================Talk is cheap, show me the code=======================================
    CSDN博客地址:https://blog.csdn.net/qq_34115899
  • 相关阅读:
    穷举 迭代 while
    for 循环
    switch case
    if else 语句
    数据类型
    语句的输入、输出
    控件——DataGridview
    mysql-bin.000001文件的来源及处理方法
    /var/log目录下的20个Linux日志文件功能详解
    CountDownLatch
  • 原文地址:https://www.cnblogs.com/lcy0515/p/9179779.html
Copyright © 2011-2022 走看看