字符串组合全排序 以及一串数的字串

按照顺序 以及加入选与不选的问题 采用递归方法表达 并在每一条执行函数中加入参数保存当前字符串的情况
1648545-20190708110212988-515623133.png
当判断完毕时 输出该字符串
小范围还好 大范围则可能会溢出
求字符串子串 模板 里面会多一种"" 空字符串

import java.util.*;
public class Main {
    public static void funcation(String s, int num, String res) {
        if (num == s.length())
            System.out.println(res);
        else {
            funcation(s, num + 1, res + s.charAt(num));
            funcation(s, num + 1, res);
        }
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        String res = "";
        int num = 0;
        funcation(s, num, res);
    }
}

栈的写法

public static void combine(char chs[]){  
    if(chs.length == 0) return ;  
      
    Stack<Character> stack = new Stack<Character>();  
    for(int i = 1; i <= chs.length; i++){  
        combine(chs, 0, i, stack);  
    }  
}  
//从字符数组中第begin个字符开始挑选number个字符加入stack中  
public static void combine(char []chs, int begin, int number, Stack<Character> stack){  
       if(number == 0){  
        System.out.println(stack.toString());  
        return ;  
       }  
       if(begin == chs.length){  
        return;  
       }  
       stack.push(chs[begin]);  
       combine(chs, begin + 1, number - 1, stack);  
       stack.pop();  
       combine(chs, begin + 1, number, stack);  
}  

搜索中看到一些文章 插在这里把https://blog.csdn.net/morewindows/article/details/7370155
1648545-20190708110307139-195448128.png
这种思路好像行得通 不过截图的具体实现有误
来自https://www.cnblogs.com/lifegoesonitself/p/3225803.html
里面有介绍其他的方法 http://zhedahht.blog.163.com/blog/static/2541117420114172812217/
这篇是用二进制来的https://blog.csdn.net/fxkcsdn/article/details/81328089

转载于:https://www.cnblogs.com/cznczai/p/11149911.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值