黑马程序员技术交流社区
标题:
一段很棒的递归,编程列出一个字符串的全字符组合情况
[打印本页]
作者:
云际水流
时间:
2015-5-7 15:18
标题:
一段很棒的递归,编程列出一个字符串的全字符组合情况
package com.itheima;
/*
* 编程列出一个字符串的全字符组合情况,原始字符串中没有重复字符,例如:
* 原始字符串是"abc",打印得到下列所有组合情况:
* "a" "b" "c"
* "ab" "bc" "ca" "ba" "cb" "ac"
* "abc" "acb" "bac" "bca" "cab" "cba"
*/
public class Test8 {
public static String str = "abc";
public static void main(String[] args){
show(0,new String());
}
// 递归
public static void show(int current_recur,String temp){
if(current_recur < str.length()){
for(int i = 0;i < str.length();i++){
if(!(temp.contains(str.substring(i, i+1)))){
System.out.print(temp + str.substring(i, i+1) + " ");
show(current_recur + 1, new String(temp + str.substring(i, i+1)));
}
}
}
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2