从n个字符(字母,数字)中选取m个并对其进行全排列,字符串不可以重复

package lianxi;
import java.util.ArrayList;
import java.util.List;
public class e {
 
    public static void doit(char[] chars, int n) {
    	//判断非法输入
        if (n <= 0 || chars == null) {
            return;
        }
        //新建一个集合,里面存放字符数组
        List<Character> charList = new ArrayList<>();
       //初始化要输出的序列长度
        for (int i = 0; i < n; i++) {
            charList.add('#');
        }
        listAll(charList, chars, n);
    }
   
    public static void listAll(List<Character> list, char[] chars, int n) {
        if (n == 0) {
            System.out.println(list);                  // 输出一种可能的排列
            return;
        }
        
        for (char aChar : chars) {                      // 暴力尝试
            if (!list.contains(aChar)) {                // 若List中不包含这一位元素
                list.set(list.size() - n, aChar);       // 将当前元素加入
            } else {                                    // 否则跳到下一位
                continue;
            }
            listAll(list, chars, n - 1);                // 下一位
            list.set(list.size() - n, '#');             // 还原
        }
    }
    public static void main(String[] args) {
        //  以字符数组承载总的字符集合
        char[] chars = {'a', 'b', 'c', 'd'};
        e.doit(chars, 1);
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值