给定3个唯一字母:您是否可以使用递归函数打印字母的六个可能的非重复组合。“ cat”应输出:cat,act,atc,tac,tca和cta。这是我的程序,找不到递归算法。这是我的尝试:
static void findWords(StringBuilder string, int start, int stride) {
//1. iterate through all possible combinations of the chars recursively
System.out.println(string);
if (stride < string.length() && start < string.length())
{
char temp = string.charAt(stride);
string.setCharAt(stride, string.charAt(start));
string.setCharAt(start, temp);
findWords(string, start, stride + 1);
findWords(string, start + 1, stride + 1 );
}
}
public static void main(String[] args)
{
StringBuilder word = new StringBuilder("cat");
findWords(word,0,1);
}
慕尼黑的夜晚无繁华
相关分类